@saykit/transform-jsx 0.1.0 → 0.2.0
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 +2 -0
- package/dist/generator.mjs +5 -1
- package/dist/parser.mjs +17 -8
- package/package.json +8 -10
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
> JSX and TSX transformer for [SayKit](https://saykit.js.org).
|
|
4
4
|
|
|
5
|
+
[](https://codecov.io/gh/k0d13/saykit?flags%5B0%5D=transform-jsx)
|
|
6
|
+
|
|
5
7
|
Extracts and rewrites the `<Say>` macro (and its `<Say.Plural>`, `<Say.Ordinal>`, `<Say.Select>` sub-components) in `.jsx` and `.tsx` files. Used inside a `saykit.config.ts` bucket alongside [`@saykit/transform-js`](https://github.com/k0d13/saykit/tree/main/packages/transform-js).
|
|
6
8
|
|
|
7
9
|
## Install
|
package/dist/generator.mjs
CHANGED
|
@@ -4,7 +4,11 @@ import * as t from "@babel/types";
|
|
|
4
4
|
function generateSayJSXElement(message) {
|
|
5
5
|
const id = message.descriptor.id ?? message.toHashString();
|
|
6
6
|
const children = generateChildExpressions(message.children);
|
|
7
|
-
const attributes = [
|
|
7
|
+
const attributes = [
|
|
8
|
+
t.jsxAttribute(t.jsxIdentifier("id"), t.stringLiteral(id)),
|
|
9
|
+
...message.whitespace === void 0 ? [] : [t.jsxAttribute(t.jsxIdentifier("whitespace"), t.jsxExpressionContainer(t.booleanLiteral(message.whitespace)))],
|
|
10
|
+
...children.map(([k, e]) => t.jsxAttribute(t.jsxIdentifier(Number.isNaN(+k) ? k : `_${k}`), t.jsxExpressionContainer(e)))
|
|
11
|
+
];
|
|
8
12
|
return t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier("Say"), attributes, true), null, []);
|
|
9
13
|
}
|
|
10
14
|
function generateChildExpressions(messages) {
|
package/dist/parser.mjs
CHANGED
|
@@ -20,10 +20,13 @@ function parseJSXContainerElement(element) {
|
|
|
20
20
|
}
|
|
21
21
|
return p;
|
|
22
22
|
}, []);
|
|
23
|
+
const descriptorId = findAttributeValueIfStringLiteralAsString(element.openingElement.attributes, "id");
|
|
24
|
+
const descriptorContext = findAttributeValueIfStringLiteralAsString(element.openingElement.attributes, "context");
|
|
25
|
+
const whitespace = findAttributeValueAsBoolean(element.openingElement.attributes, "whitespace");
|
|
23
26
|
return new CompositeMessage({
|
|
24
|
-
id:
|
|
25
|
-
context:
|
|
26
|
-
}, [], [], children, accessor);
|
|
27
|
+
id: descriptorId,
|
|
28
|
+
context: descriptorContext
|
|
29
|
+
}, [], [], children, accessor, whitespace);
|
|
27
30
|
}
|
|
28
31
|
function parseJSXOpeningElement(element) {
|
|
29
32
|
if (!element.selfClosing) return null;
|
|
@@ -80,13 +83,10 @@ function parseJSXElement(element, fallback) {
|
|
|
80
83
|
if (message) return message;
|
|
81
84
|
if (!fallback) return null;
|
|
82
85
|
if (element.openingElement.selfClosing) return new ElementMessage(AUTO_INCREMENT_IDENTIFIER, [], element);
|
|
83
|
-
|
|
84
|
-
if (wrapped) return new ElementMessage(AUTO_INCREMENT_IDENTIFIER, [wrapped], element);
|
|
85
|
-
return null;
|
|
86
|
+
return new ElementMessage(AUTO_INCREMENT_IDENTIFIER, [parseJSXElement(t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier("Say"), []), t.jsxClosingElement(t.jsxIdentifier("Say")), element.children), true)], element);
|
|
86
87
|
}
|
|
87
88
|
function getAttributeNameAsString(attribute) {
|
|
88
|
-
|
|
89
|
-
if (t.isJSXNamespacedName(attribute.name)) return attribute.name.name.name;
|
|
89
|
+
return t.isJSXIdentifier(attribute.name) ? attribute.name.name : attribute.name.name.name;
|
|
90
90
|
}
|
|
91
91
|
function findAttributeValueIfStringLiteralAsString(attributes, identifier) {
|
|
92
92
|
for (const attribute of attributes) {
|
|
@@ -105,8 +105,17 @@ function findAttributeValueIfExpressionOrStringLiteral(attributes, identifier) {
|
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
|
+
function findAttributeValueAsBoolean(attributes, identifier) {
|
|
109
|
+
for (const attribute of attributes) {
|
|
110
|
+
if (!t.isJSXAttribute(attribute)) continue;
|
|
111
|
+
if (!t.isJSXIdentifier(attribute.name) || attribute.name.name !== identifier) continue;
|
|
112
|
+
if (attribute.value == null) return true;
|
|
113
|
+
if (t.isJSXExpressionContainer(attribute.value) && t.isBooleanLiteral(attribute.value.expression)) return attribute.value.expression.value;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
108
116
|
function getExpressionAsIdentifier(node) {
|
|
109
117
|
if (t.isIdentifier(node)) return node.name;
|
|
118
|
+
/* v8 ignore next */
|
|
110
119
|
if (t.isJSXIdentifier(node)) return node.name;
|
|
111
120
|
return AUTO_INCREMENT_IDENTIFIER;
|
|
112
121
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saykit/transform-jsx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "JSX and TSX source transformer for SayKit",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -43,23 +43,21 @@
|
|
|
43
43
|
"provenance": true
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@babel/generator": "^7.29.
|
|
47
|
-
"@babel/parser": "^7.29.
|
|
48
|
-
"@babel/traverse": "^7.29.
|
|
49
|
-
"@babel/types": "^7.29.
|
|
50
|
-
"@saykit/config": "^0.
|
|
51
|
-
"@saykit/transform-js": "^0.
|
|
46
|
+
"@babel/generator": "^7.29.7",
|
|
47
|
+
"@babel/parser": "^7.29.7",
|
|
48
|
+
"@babel/traverse": "^7.29.7",
|
|
49
|
+
"@babel/types": "^7.29.7",
|
|
50
|
+
"@saykit/config": "^0.2.0",
|
|
51
|
+
"@saykit/transform-js": "^0.2.0"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@babel/core": "^7.29.
|
|
54
|
+
"@babel/core": "^7.29.7",
|
|
55
55
|
"@types/babel__core": "^7.20.5",
|
|
56
56
|
"@types/babel__generator": "^7.27.0",
|
|
57
57
|
"@types/babel__traverse": "^7.28.0"
|
|
58
58
|
},
|
|
59
59
|
"scripts": {
|
|
60
60
|
"check": "tsc --noEmit",
|
|
61
|
-
"test": "vitest",
|
|
62
|
-
"coverage": "vitest run --coverage",
|
|
63
61
|
"build": "tsdown"
|
|
64
62
|
}
|
|
65
63
|
}
|