@saykit/transform-jsx 0.0.0-beta-20260728002240 → 0.0.0-beta-20260729113209
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/dist/generator.mjs +1 -1
- package/dist/index.mjs +5 -5
- package/dist/parser.mjs +42 -14
- package/package.json +3 -3
package/dist/generator.mjs
CHANGED
|
@@ -7,7 +7,7 @@ function generateSayJSXElement(message) {
|
|
|
7
7
|
const attributes = [
|
|
8
8
|
t.jsxAttribute(t.jsxIdentifier("id"), t.stringLiteral(id)),
|
|
9
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(
|
|
10
|
+
...[...new Map(children).entries()].map(([k, e]) => t.jsxAttribute(t.jsxIdentifier(`_${k}`), t.jsxExpressionContainer(e)))
|
|
11
11
|
];
|
|
12
12
|
return t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier("Say"), attributes, true), null, []);
|
|
13
13
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import * as parser from "@babel/parser";
|
|
|
5
5
|
import traverse_ from "@babel/traverse";
|
|
6
6
|
import { assignSequenceIdentifiers } from "@saykit/config/features/messages";
|
|
7
7
|
import { generateSayCallExpression } from "@saykit/transform-js/generator";
|
|
8
|
-
import { parseExpression } from "@saykit/transform-js/parser";
|
|
8
|
+
import { isEquivalentPlaceholder, parseExpression } from "@saykit/transform-js/parser";
|
|
9
9
|
//#region src/index.ts
|
|
10
10
|
const traverse = traverse_.default || traverse_;
|
|
11
11
|
function createProgram(code, id) {
|
|
@@ -40,7 +40,7 @@ function createJsxTransformer() {
|
|
|
40
40
|
path.node.leadingComments = path.node.leadingComments ?? [];
|
|
41
41
|
const message = parseExpression(path.node);
|
|
42
42
|
if (message) {
|
|
43
|
-
assignSequenceIdentifiers(message, { current: 0 });
|
|
43
|
+
assignSequenceIdentifiers(message, { current: 0 }, isEquivalentPlaceholder);
|
|
44
44
|
messages.push(message);
|
|
45
45
|
path.skip();
|
|
46
46
|
}
|
|
@@ -49,7 +49,7 @@ function createJsxTransformer() {
|
|
|
49
49
|
path.node.leadingComments = path.node.leadingComments ?? [];
|
|
50
50
|
const message = parseJSXElement(path.node);
|
|
51
51
|
if (message) {
|
|
52
|
-
assignSequenceIdentifiers(message, { current: 0 });
|
|
52
|
+
assignSequenceIdentifiers(message, { current: 0 }, isEquivalentPlaceholder);
|
|
53
53
|
messages.push(message);
|
|
54
54
|
path.skip();
|
|
55
55
|
}
|
|
@@ -71,7 +71,7 @@ function createJsxTransformer() {
|
|
|
71
71
|
path.node.leadingComments = path.node.leadingComments ?? [];
|
|
72
72
|
const message = parseExpression(path.node);
|
|
73
73
|
if (message) {
|
|
74
|
-
assignSequenceIdentifiers(message, { current: 0 });
|
|
74
|
+
assignSequenceIdentifiers(message, { current: 0 }, isEquivalentPlaceholder);
|
|
75
75
|
const replacement = generateSayCallExpression(message);
|
|
76
76
|
path.replaceWith(replacement);
|
|
77
77
|
path.skip();
|
|
@@ -81,7 +81,7 @@ function createJsxTransformer() {
|
|
|
81
81
|
path.node.leadingComments = path.node.leadingComments ?? [];
|
|
82
82
|
const message = parseJSXElement(path.node);
|
|
83
83
|
if (message) {
|
|
84
|
-
assignSequenceIdentifiers(message, { current: 0 });
|
|
84
|
+
assignSequenceIdentifiers(message, { current: 0 }, isEquivalentPlaceholder);
|
|
85
85
|
const replacement = generateSayJSXElement(message);
|
|
86
86
|
path.replaceWith(replacement);
|
|
87
87
|
path.skip();
|
package/dist/parser.mjs
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { AUTO_INCREMENT_IDENTIFIER, ArgumentMessage, ChoiceMessage, CompositeMessage, ElementMessage, LiteralMessage } from "@saykit/config/features/messages";
|
|
2
|
+
import { unwrapPlaceholder } from "@saykit/transform-js/parser";
|
|
2
3
|
import * as t from "@babel/types";
|
|
3
4
|
//#region src/parser.ts
|
|
5
|
+
/**
|
|
6
|
+
* Attribute used to name the ICU tag an embedded element extracts as, e.g.
|
|
7
|
+
* `<a say-tag="link">` becomes `<link></link>` rather than `<0></0>`. It never
|
|
8
|
+
* reaches the real element.
|
|
9
|
+
*/
|
|
10
|
+
const TAG_ATTRIBUTE = "say-tag";
|
|
11
|
+
const TAG_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
4
12
|
function parseJSXContainerElement(element) {
|
|
5
13
|
if (element.openingElement.selfClosing) return null;
|
|
6
14
|
const processed = processJSXOpeningElement(element.openingElement);
|
|
@@ -16,7 +24,10 @@ function parseJSXContainerElement(element) {
|
|
|
16
24
|
if (t.isJSXElement(c)) p.push(parseJSXElement(c, true));
|
|
17
25
|
else if (t.isJSXFragment(c)) p.push(new ElementMessage(AUTO_INCREMENT_IDENTIFIER, [], c));
|
|
18
26
|
else if (t.isJSXExpressionContainer(c)) {
|
|
19
|
-
if (t.isExpression(c.expression))
|
|
27
|
+
if (t.isExpression(c.expression)) {
|
|
28
|
+
const [identifier, value] = unwrapPlaceholder(c.expression);
|
|
29
|
+
p.push(new ArgumentMessage(identifier, value));
|
|
30
|
+
}
|
|
20
31
|
}
|
|
21
32
|
return p;
|
|
22
33
|
}, []);
|
|
@@ -56,16 +67,20 @@ function parseJSXOpeningElement(element) {
|
|
|
56
67
|
identifier,
|
|
57
68
|
value: new ElementMessage(AUTO_INCREMENT_IDENTIFIER, [], a.value.expression)
|
|
58
69
|
});
|
|
59
|
-
else if (t.isExpression(a.value.expression))
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
70
|
+
else if (t.isExpression(a.value.expression)) {
|
|
71
|
+
const [name, value] = unwrapPlaceholder(a.value.expression);
|
|
72
|
+
b.push({
|
|
73
|
+
identifier,
|
|
74
|
+
value: new ArgumentMessage(name, value)
|
|
75
|
+
});
|
|
76
|
+
}
|
|
63
77
|
}
|
|
64
78
|
return b;
|
|
65
79
|
}, []);
|
|
66
80
|
const initialiser = findAttributeValueIfExpressionOrStringLiteral(element.attributes, "_");
|
|
67
81
|
if (!initialiser) return null;
|
|
68
|
-
const
|
|
82
|
+
const [identifier, selector] = unwrapPlaceholder(initialiser);
|
|
83
|
+
const choice = new ChoiceMessage(kind, identifier, branches, selector);
|
|
69
84
|
return new CompositeMessage({
|
|
70
85
|
id: findAttributeValueIfStringLiteralAsString(element.attributes, "id"),
|
|
71
86
|
context: findAttributeValueIfStringLiteralAsString(element.attributes, "context")
|
|
@@ -82,8 +97,27 @@ function parseJSXElement(element, fallback) {
|
|
|
82
97
|
const message = element.openingElement.selfClosing ? parseJSXOpeningElement(element.openingElement) : parseJSXContainerElement(element);
|
|
83
98
|
if (message) return message;
|
|
84
99
|
if (!fallback) return null;
|
|
85
|
-
|
|
86
|
-
|
|
100
|
+
const identifier = takeElementTag(element) ?? AUTO_INCREMENT_IDENTIFIER;
|
|
101
|
+
if (element.openingElement.selfClosing) return new ElementMessage(identifier, [], element);
|
|
102
|
+
return new ElementMessage(identifier, [parseJSXElement(t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier("Say"), []), t.jsxClosingElement(t.jsxIdentifier("Say")), element.children), true)], element);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Read the tag name off an embedded element and remove the attribute so it
|
|
106
|
+
* never reaches the rendered element. Only static strings name a tag; anything
|
|
107
|
+
* else is left in place and the element falls back to an auto-incremented
|
|
108
|
+
* identifier.
|
|
109
|
+
*/
|
|
110
|
+
function takeElementTag(element) {
|
|
111
|
+
const attributes = element.openingElement.attributes;
|
|
112
|
+
const index = attributes.findIndex((a) => t.isJSXAttribute(a) && getAttributeNameAsString(a) === TAG_ATTRIBUTE);
|
|
113
|
+
if (index === -1) return void 0;
|
|
114
|
+
const attribute = attributes[index];
|
|
115
|
+
const value = t.isJSXExpressionContainer(attribute.value) ? attribute.value.expression : attribute.value;
|
|
116
|
+
if (!t.isStringLiteral(value)) return void 0;
|
|
117
|
+
const tag = value.value;
|
|
118
|
+
if (!TAG_PATTERN.test(tag)) throw new Error(`Invalid '${TAG_ATTRIBUTE}' value '${tag}', expected a letter or underscore followed by letters, digits, or underscores`);
|
|
119
|
+
attributes.splice(index, 1);
|
|
120
|
+
return tag;
|
|
87
121
|
}
|
|
88
122
|
function getReferences(node) {
|
|
89
123
|
if (!node.loc?.filename) return [];
|
|
@@ -117,11 +151,5 @@ function findAttributeValueAsBoolean(attributes, identifier) {
|
|
|
117
151
|
if (t.isJSXExpressionContainer(attribute.value) && t.isBooleanLiteral(attribute.value.expression)) return attribute.value.expression.value;
|
|
118
152
|
}
|
|
119
153
|
}
|
|
120
|
-
function getExpressionAsIdentifier(node) {
|
|
121
|
-
if (t.isIdentifier(node)) return node.name;
|
|
122
|
-
/* v8 ignore next */
|
|
123
|
-
if (t.isJSXIdentifier(node)) return node.name;
|
|
124
|
-
return AUTO_INCREMENT_IDENTIFIER;
|
|
125
|
-
}
|
|
126
154
|
//#endregion
|
|
127
155
|
export { parseJSXContainerElement, parseJSXElement, parseJSXOpeningElement };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saykit/transform-jsx",
|
|
3
|
-
"version": "0.0.0-beta-
|
|
3
|
+
"version": "0.0.0-beta-20260729113209",
|
|
4
4
|
"description": "JSX and TSX source transformer for SayKit",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -47,8 +47,8 @@
|
|
|
47
47
|
"@babel/parser": "^7.29.7",
|
|
48
48
|
"@babel/traverse": "^7.29.7",
|
|
49
49
|
"@babel/types": "^7.29.7",
|
|
50
|
-
"@saykit/config": "^0.0.0-beta-
|
|
51
|
-
"@saykit/transform-js": "^0.0.0-beta-
|
|
50
|
+
"@saykit/config": "^0.0.0-beta-20260729113209",
|
|
51
|
+
"@saykit/transform-js": "^0.0.0-beta-20260729113209"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@babel/core": "^7.29.7",
|