htm-transform 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/index.js +16 -2
- package/jsconfig.json +12 -0
- package/package.json +8 -1
- package/test.js +22 -0
- package/.claude/settings.local.json +0 -10
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ A small alternative to [babel-plugin-htm](https://www.npmjs.com/package/babel-pl
|
|
|
6
6
|
## Quickstart
|
|
7
7
|
|
|
8
8
|
```js
|
|
9
|
-
import transform from "
|
|
9
|
+
import transform from "htm-transform";
|
|
10
10
|
|
|
11
11
|
const result = transform("const hyperscript = html`<h1 id=hello>Hello world!</h1>`;");
|
|
12
12
|
console.assert(result === `const hyperscript = h("h1", { id: "hello" }, "Hello world!")`);
|
package/index.js
CHANGED
|
@@ -364,6 +364,16 @@ function resolvePlaceholders(nodes, placeholders) {
|
|
|
364
364
|
return nodes.map(resolve).flat();
|
|
365
365
|
}
|
|
366
366
|
|
|
367
|
+
/**
|
|
368
|
+
* Checks if a property key needs to be quoted
|
|
369
|
+
* @param {string} key - The property key
|
|
370
|
+
* @returns {boolean} True if the key needs to be quoted
|
|
371
|
+
*/
|
|
372
|
+
function needsQuoting(key) {
|
|
373
|
+
// Check if key is a valid JavaScript identifier
|
|
374
|
+
return !/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key);
|
|
375
|
+
}
|
|
376
|
+
|
|
367
377
|
/**
|
|
368
378
|
* Converts element nodes to AST nodes representing h() calls
|
|
369
379
|
* @param {ElementNode | TextNode | ExpressionNode} node - The node to convert
|
|
@@ -409,7 +419,9 @@ function elementsToAST(node, pragma) {
|
|
|
409
419
|
}
|
|
410
420
|
return {
|
|
411
421
|
type: "Property",
|
|
412
|
-
key:
|
|
422
|
+
key: needsQuoting(key)
|
|
423
|
+
? { type: "Literal", value: key }
|
|
424
|
+
: { type: "Identifier", name: key },
|
|
413
425
|
value: value.type === "expression" ? value.expr : { type: "Literal", value },
|
|
414
426
|
kind: "init",
|
|
415
427
|
method: false,
|
|
@@ -428,7 +440,9 @@ function elementsToAST(node, pragma) {
|
|
|
428
440
|
type: "ObjectExpression",
|
|
429
441
|
properties: propsEntries.map(([key, value]) => ({
|
|
430
442
|
type: "Property",
|
|
431
|
-
key:
|
|
443
|
+
key: needsQuoting(key)
|
|
444
|
+
? { type: "Literal", value: key }
|
|
445
|
+
: { type: "Identifier", name: key },
|
|
432
446
|
value: value.type === "expression" ? value.expr : { type: "Literal", value },
|
|
433
447
|
kind: "init",
|
|
434
448
|
method: false,
|
package/jsconfig.json
ADDED
package/package.json
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "htm-transform",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Transform htm tagged templates into h function calls using acorn",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
7
|
+
"types": "index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.js",
|
|
11
|
+
"default": "./index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
7
14
|
"scripts": {
|
|
8
15
|
"test": "node --test test.js",
|
|
9
16
|
"test:watch": "node --test --watch test.js"
|
package/test.js
CHANGED
|
@@ -358,4 +358,26 @@ const result = myH("div", null, "Test");`;
|
|
|
358
358
|
|
|
359
359
|
assert.strictEqual(normalize(output), normalize(expected));
|
|
360
360
|
});
|
|
361
|
+
|
|
362
|
+
test("handles attributes with hyphens", () => {
|
|
363
|
+
const input = `const result = html\`<div data-value="test" aria-label="label">Content</div>\`;`;
|
|
364
|
+
const output = transform(input);
|
|
365
|
+
const expected = `const result = h("div", {
|
|
366
|
+
"data-value": "test",
|
|
367
|
+
"aria-label": "label"
|
|
368
|
+
}, "Content");`;
|
|
369
|
+
|
|
370
|
+
assert.strictEqual(normalize(output), normalize(expected));
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test("handles dynamic attributes with hyphens", () => {
|
|
374
|
+
const input = `const result = html\`<div data-id=\${id} aria-label=\${label}>Content</div>\`;`;
|
|
375
|
+
const output = transform(input);
|
|
376
|
+
const expected = `const result = h("div", {
|
|
377
|
+
"data-id": id,
|
|
378
|
+
"aria-label": label
|
|
379
|
+
}, "Content");`;
|
|
380
|
+
|
|
381
|
+
assert.strictEqual(normalize(output), normalize(expected));
|
|
382
|
+
});
|
|
361
383
|
});
|