@saykit/transform-js 0.7.0 → 0.9.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/dist/parser.mjs +9 -34
- package/package.json +2 -2
package/dist/parser.mjs
CHANGED
|
@@ -6,16 +6,19 @@ function parseTaggedTemplateExpression(tagged) {
|
|
|
6
6
|
const processed = processExpression(tagged.tag);
|
|
7
7
|
if (!processed) return null;
|
|
8
8
|
const [accessor, descriptor] = processed;
|
|
9
|
-
const children = tagged.quasi
|
|
10
|
-
c.push(new LiteralMessage(q.value.cooked ?? q.value.raw));
|
|
11
|
-
if (t.isExpression(tagged.quasi.expressions[i])) c.push(parseExpression(tagged.quasi.expressions[i], true));
|
|
12
|
-
return c;
|
|
13
|
-
}, []);
|
|
9
|
+
const children = buildTemplateChildren(tagged.quasi);
|
|
14
10
|
return new CompositeMessage({
|
|
15
11
|
id: descriptor ? findPropertyValueIfStringLiteralAsString(descriptor, "id") : void 0,
|
|
16
12
|
context: descriptor ? findPropertyValueIfStringLiteralAsString(descriptor, "context") : void 0
|
|
17
13
|
}, getTranslatorComments(tagged.leadingComments ?? []), tagged.loc ? [`${tagged.loc.filename}:${tagged.loc.start.line}`] : [], children, accessor);
|
|
18
14
|
}
|
|
15
|
+
function buildTemplateChildren(template) {
|
|
16
|
+
return template.quasis.reduce((c, q, i) => {
|
|
17
|
+
c.push(new LiteralMessage(q.value.cooked ?? q.value.raw));
|
|
18
|
+
if (t.isExpression(template.expressions[i])) c.push(parseExpression(template.expressions[i], true));
|
|
19
|
+
return c;
|
|
20
|
+
}, []);
|
|
21
|
+
}
|
|
19
22
|
function parseCallExpression(call) {
|
|
20
23
|
const processed = processExpression(call.callee);
|
|
21
24
|
if (!processed) return null;
|
|
@@ -41,6 +44,7 @@ function parseCallExpression(call) {
|
|
|
41
44
|
if (offset !== void 0 && getPropertyNameAsString(p.key) === "offset") return c;
|
|
42
45
|
let message = null;
|
|
43
46
|
if (!message && t.isStringLiteral(p.value)) message = new LiteralMessage(p.value.value);
|
|
47
|
+
if (!message && t.isTemplateLiteral(p.value)) message = new CompositeMessage({}, [], [], buildTemplateChildren(p.value), p.value);
|
|
44
48
|
if (!message) message = parseExpression(p.value, true);
|
|
45
49
|
c.push({
|
|
46
50
|
identifier: getPropertyNameAsString(p.key),
|
|
@@ -69,15 +73,6 @@ function parseCallExpression(call) {
|
|
|
69
73
|
}
|
|
70
74
|
return null;
|
|
71
75
|
}
|
|
72
|
-
/**
|
|
73
|
-
* The `offset` a plural subtracts from its selector before `#` is formatted, so
|
|
74
|
-
* "You and 2 others" can select on a total of three.
|
|
75
|
-
*
|
|
76
|
-
* Read only from an integer literal: the offset is baked into the extracted
|
|
77
|
-
* message, so it has to be known at build time, and a fractional or negative
|
|
78
|
-
* one is not something ICU accepts. Anything else stays an ordinary branch and
|
|
79
|
-
* is validated as the key it looks like.
|
|
80
|
-
*/
|
|
81
76
|
function findPluralOffset(object) {
|
|
82
77
|
for (const property of object.properties) {
|
|
83
78
|
if (!t.isObjectProperty(property) || property.computed) continue;
|
|
@@ -128,16 +123,6 @@ function getExpressionAsKey(node) {
|
|
|
128
123
|
if (t.isJSXIdentifier(node)) return node.name;
|
|
129
124
|
return AUTO_INCREMENT_IDENTIFIER;
|
|
130
125
|
}
|
|
131
|
-
/**
|
|
132
|
-
* Read the name off a value written as a single-key object and hand back the
|
|
133
|
-
* value alone, so the wrapper never reaches the output: `${{ total: sum() }}`
|
|
134
|
-
* is `{total}` rather than `{0}`.
|
|
135
|
-
*
|
|
136
|
-
* An inline one-key object is the only shape this claims, and it is a shape
|
|
137
|
-
* that could not mean anything else — interpolating an object stringifies it
|
|
138
|
-
* to `[object Object]`, and rendering one in JSX throws. Anything else is
|
|
139
|
-
* returned untouched and named the way it always was.
|
|
140
|
-
*/
|
|
141
126
|
function unwrapPlaceholder(expression) {
|
|
142
127
|
if (!t.isObjectExpression(expression) || expression.properties.length !== 1) return [getExpressionAsKey(expression), expression];
|
|
143
128
|
const [property] = expression.properties;
|
|
@@ -148,16 +133,6 @@ function unwrapPlaceholder(expression) {
|
|
|
148
133
|
if (!PLACEHOLDER_PATTERN.test(name)) throw new Error(`Invalid placeholder name '${name}', expected a letter or underscore followed by letters, digits, or underscores`);
|
|
149
134
|
return [name, property.value];
|
|
150
135
|
}
|
|
151
|
-
/**
|
|
152
|
-
* Whether two placeholders sharing a name are the same placeholder, and so
|
|
153
|
-
* compile to one prop a translator can move around the sentence freely.
|
|
154
|
-
*
|
|
155
|
-
* A value is compared whole: the same variable interpolated twice is one value,
|
|
156
|
-
* while `${name}` beside `${{ name: author.name }}` is two things claiming one
|
|
157
|
-
* name. An element is compared on its opening element alone — `say-tag` has
|
|
158
|
-
* been stripped by the time this runs, and its children come from the
|
|
159
|
-
* translation rather than the source.
|
|
160
|
-
*/
|
|
161
136
|
function isEquivalentPlaceholder(a, b) {
|
|
162
137
|
if (t.isJSXElement(a) || t.isJSXElement(b)) {
|
|
163
138
|
if (!t.isJSXElement(a) || !t.isJSXElement(b)) return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saykit/transform-js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "JavaScript and TypeScript transformer for SayKit",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -47,7 +47,7 @@
|
|
|
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.
|
|
50
|
+
"@saykit/config": "^0.9.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@babel/core": "^7.29.7",
|