@saykit/transform-jsx 0.0.0-beta-20260728002240 → 0.0.0-beta-20260729112211
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.d.mts +8 -1
- package/dist/parser.mjs +39 -3
- 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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { generateSayJSXElement } from "./generator.mjs";
|
|
2
|
-
import { parseJSXElement } from "./parser.mjs";
|
|
2
|
+
import { isEquivalentElement, parseJSXElement } from "./parser.mjs";
|
|
3
3
|
import { generate } from "@babel/generator";
|
|
4
4
|
import * as parser from "@babel/parser";
|
|
5
5
|
import traverse_ from "@babel/traverse";
|
|
@@ -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 }, isEquivalentElement);
|
|
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 }, isEquivalentElement);
|
|
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 }, isEquivalentElement);
|
|
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 }, isEquivalentElement);
|
|
85
85
|
const replacement = generateSayJSXElement(message);
|
|
86
86
|
path.replaceWith(replacement);
|
|
87
87
|
path.skip();
|
package/dist/parser.d.mts
CHANGED
|
@@ -5,5 +5,12 @@ declare function parseJSXContainerElement(element: t.JSXElement): CompositeMessa
|
|
|
5
5
|
declare function parseJSXOpeningElement(element: t.JSXOpeningElement): CompositeMessage | null;
|
|
6
6
|
declare function parseJSXElement(element: t.JSXElement, fallback?: false): CompositeMessage | null;
|
|
7
7
|
declare function parseJSXElement(element: t.JSXElement, fallback: true): CompositeMessage | ElementMessage;
|
|
8
|
+
/**
|
|
9
|
+
* Whether two elements sharing a tag are the same element, and so compile to
|
|
10
|
+
* one prop a translator can use interchangeably. `say-tag` has been stripped by
|
|
11
|
+
* the time this runs, so it compares the element name and the props left over,
|
|
12
|
+
* not the children: those come from the translation, not the source.
|
|
13
|
+
*/
|
|
14
|
+
declare function isEquivalentElement(a: any, b: any): boolean;
|
|
8
15
|
//#endregion
|
|
9
|
-
export { parseJSXContainerElement, parseJSXElement, parseJSXOpeningElement };
|
|
16
|
+
export { isEquivalentElement, parseJSXContainerElement, parseJSXElement, parseJSXOpeningElement };
|
package/dist/parser.mjs
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { AUTO_INCREMENT_IDENTIFIER, ArgumentMessage, ChoiceMessage, CompositeMessage, ElementMessage, LiteralMessage } from "@saykit/config/features/messages";
|
|
2
2
|
import * as t from "@babel/types";
|
|
3
3
|
//#region src/parser.ts
|
|
4
|
+
/**
|
|
5
|
+
* Attribute used to name the ICU tag an embedded element extracts as, e.g.
|
|
6
|
+
* `<a say-tag="link">` becomes `<link></link>` rather than `<0></0>`. It never
|
|
7
|
+
* reaches the real element.
|
|
8
|
+
*/
|
|
9
|
+
const TAG_ATTRIBUTE = "say-tag";
|
|
10
|
+
const TAG_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
4
11
|
function parseJSXContainerElement(element) {
|
|
5
12
|
if (element.openingElement.selfClosing) return null;
|
|
6
13
|
const processed = processJSXOpeningElement(element.openingElement);
|
|
@@ -82,8 +89,37 @@ function parseJSXElement(element, fallback) {
|
|
|
82
89
|
const message = element.openingElement.selfClosing ? parseJSXOpeningElement(element.openingElement) : parseJSXContainerElement(element);
|
|
83
90
|
if (message) return message;
|
|
84
91
|
if (!fallback) return null;
|
|
85
|
-
|
|
86
|
-
|
|
92
|
+
const identifier = takeElementTag(element) ?? AUTO_INCREMENT_IDENTIFIER;
|
|
93
|
+
if (element.openingElement.selfClosing) return new ElementMessage(identifier, [], element);
|
|
94
|
+
return new ElementMessage(identifier, [parseJSXElement(t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier("Say"), []), t.jsxClosingElement(t.jsxIdentifier("Say")), element.children), true)], element);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Read the tag name off an embedded element and remove the attribute so it
|
|
98
|
+
* never reaches the rendered element. Only static strings name a tag; anything
|
|
99
|
+
* else is left in place and the element falls back to an auto-incremented
|
|
100
|
+
* identifier.
|
|
101
|
+
*/
|
|
102
|
+
function takeElementTag(element) {
|
|
103
|
+
const attributes = element.openingElement.attributes;
|
|
104
|
+
const index = attributes.findIndex((a) => t.isJSXAttribute(a) && getAttributeNameAsString(a) === TAG_ATTRIBUTE);
|
|
105
|
+
if (index === -1) return void 0;
|
|
106
|
+
const attribute = attributes[index];
|
|
107
|
+
const value = t.isJSXExpressionContainer(attribute.value) ? attribute.value.expression : attribute.value;
|
|
108
|
+
if (!t.isStringLiteral(value)) return void 0;
|
|
109
|
+
const tag = value.value;
|
|
110
|
+
if (!TAG_PATTERN.test(tag)) throw new Error(`Invalid '${TAG_ATTRIBUTE}' value '${tag}', expected a letter or underscore followed by letters, digits, or underscores`);
|
|
111
|
+
attributes.splice(index, 1);
|
|
112
|
+
return tag;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Whether two elements sharing a tag are the same element, and so compile to
|
|
116
|
+
* one prop a translator can use interchangeably. `say-tag` has been stripped by
|
|
117
|
+
* the time this runs, so it compares the element name and the props left over,
|
|
118
|
+
* not the children: those come from the translation, not the source.
|
|
119
|
+
*/
|
|
120
|
+
function isEquivalentElement(a, b) {
|
|
121
|
+
if (!t.isJSXElement(a) || !t.isJSXElement(b)) return false;
|
|
122
|
+
return t.isNodesEquivalent(a.openingElement, b.openingElement);
|
|
87
123
|
}
|
|
88
124
|
function getReferences(node) {
|
|
89
125
|
if (!node.loc?.filename) return [];
|
|
@@ -124,4 +160,4 @@ function getExpressionAsIdentifier(node) {
|
|
|
124
160
|
return AUTO_INCREMENT_IDENTIFIER;
|
|
125
161
|
}
|
|
126
162
|
//#endregion
|
|
127
|
-
export { parseJSXContainerElement, parseJSXElement, parseJSXOpeningElement };
|
|
163
|
+
export { isEquivalentElement, 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-20260729112211",
|
|
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-20260729112211",
|
|
51
|
+
"@saykit/transform-js": "^0.0.0-beta-20260729112211"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@babel/core": "^7.29.7",
|