@saykit/transform-js 0.9.1 → 0.11.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 -2
- package/dist/generator.d.mts +14 -1
- package/dist/generator.mjs +11 -3
- package/dist/index.mjs +2 -2
- package/dist/parser.d.mts +11 -15
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ pnpm add -D @saykit/transform-js
|
|
|
18
18
|
import js from '@saykit/transform-js';
|
|
19
19
|
|
|
20
20
|
// inside a bucket:
|
|
21
|
-
transformer: js()
|
|
21
|
+
transformer: js(),
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
For JSX/TSX, combine with [`@saykit/transform-jsx`](https://github.com/k0d13/saykit/tree/main/packages/transform-jsx):
|
|
@@ -28,7 +28,7 @@ import js from '@saykit/transform-js';
|
|
|
28
28
|
import jsx from '@saykit/transform-jsx';
|
|
29
29
|
|
|
30
30
|
// inside a bucket:
|
|
31
|
-
transformer: [js(), jsx()]
|
|
31
|
+
transformer: [js(), jsx()],
|
|
32
32
|
```
|
|
33
33
|
|
|
34
34
|
## Documentation
|
package/dist/generator.d.mts
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
import { CompositeMessage } from "@saykit/config/features/messages";
|
|
2
2
|
import * as t from "@babel/types";
|
|
3
3
|
//#region src/generator.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Whether a message is one formatted value and nothing else: `say.date(x)` on
|
|
6
|
+
* its own rather than inside a sentence. The style and the locale already
|
|
7
|
+
* decide what it renders as, so there is nothing in it for a translator to
|
|
8
|
+
* change: it is never extracted, and the call carries the message itself
|
|
9
|
+
* rather than an id to look one up by.
|
|
10
|
+
*/
|
|
11
|
+
declare function isLoneArgument(message: CompositeMessage): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* The property a call looks its message up by: an id into the catalogue, or
|
|
14
|
+
* the ICU message itself when nothing was extracted.
|
|
15
|
+
*/
|
|
16
|
+
declare function generateDescriptorProperty(message: CompositeMessage): t.ObjectProperty;
|
|
4
17
|
declare function generateSayCallExpression(message: CompositeMessage): t.CallExpression;
|
|
5
18
|
//#endregion
|
|
6
|
-
export { generateSayCallExpression };
|
|
19
|
+
export { generateDescriptorProperty, generateSayCallExpression, isLoneArgument };
|
package/dist/generator.mjs
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import { ArgumentMessage, ChoiceMessage, CompositeMessage, ElementMessage } from "@saykit/config/features/messages";
|
|
2
2
|
import * as t from "@babel/types";
|
|
3
3
|
//#region src/generator.ts
|
|
4
|
-
function
|
|
4
|
+
function isLoneArgument(message) {
|
|
5
|
+
const [child] = message.children;
|
|
6
|
+
return message.children.length === 1 && child instanceof ArgumentMessage && !!child.format;
|
|
7
|
+
}
|
|
8
|
+
function generateDescriptorProperty(message) {
|
|
9
|
+
if (isLoneArgument(message)) return t.objectProperty(t.identifier("message"), t.stringLiteral(message.toICUString()));
|
|
5
10
|
const id = message.descriptor.id ?? message.toHashString();
|
|
11
|
+
return t.objectProperty(t.identifier("id"), t.stringLiteral(id));
|
|
12
|
+
}
|
|
13
|
+
function generateSayCallExpression(message) {
|
|
6
14
|
const children = generateChildExpressions(message.children);
|
|
7
|
-
const properties = t.objectExpression([
|
|
15
|
+
const properties = t.objectExpression([generateDescriptorProperty(message), ...[...new Map(children).entries()].map(([ident, expr]) => t.objectProperty(t.identifier(`_${ident}`), expr))]);
|
|
8
16
|
return t.callExpression(t.memberExpression(message.accessor, t.identifier("call")), [properties]);
|
|
9
17
|
}
|
|
10
18
|
function generateChildExpressions(messages) {
|
|
@@ -23,4 +31,4 @@ function generateChildExpressions(messages) {
|
|
|
23
31
|
}, []);
|
|
24
32
|
}
|
|
25
33
|
//#endregion
|
|
26
|
-
export { generateSayCallExpression };
|
|
34
|
+
export { generateDescriptorProperty, generateSayCallExpression, isLoneArgument };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { generateSayCallExpression } from "./generator.mjs";
|
|
1
|
+
import { generateSayCallExpression, isLoneArgument } from "./generator.mjs";
|
|
2
2
|
import { collectLeadingComments, isEquivalentPlaceholder, parseExpression } from "./parser.mjs";
|
|
3
3
|
import { generate } from "@babel/generator";
|
|
4
4
|
import * as parser from "@babel/parser";
|
|
@@ -49,7 +49,7 @@ function createJsTransformer() {
|
|
|
49
49
|
path.skip();
|
|
50
50
|
}
|
|
51
51
|
} });
|
|
52
|
-
return messages.map((message) => ({
|
|
52
|
+
return messages.filter((message) => !isLoneArgument(message)).map((message) => ({
|
|
53
53
|
message: message.toICUString(),
|
|
54
54
|
translation: void 0,
|
|
55
55
|
id: message.descriptor.id,
|
package/dist/parser.d.mts
CHANGED
|
@@ -10,10 +10,9 @@ declare function processExpression(expression: t.Node): [t.Expression, t.ObjectE
|
|
|
10
10
|
* value alone, so the wrapper never reaches the output: `${{ total: sum() }}`
|
|
11
11
|
* is `{total}` rather than `{0}`.
|
|
12
12
|
*
|
|
13
|
-
* An inline one-key object is the only shape this claims, and it
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* returned untouched and named the way it always was.
|
|
13
|
+
* An inline one-key object is the only shape this claims, and it could not mean
|
|
14
|
+
* anything else: interpolating an object stringifies it to `[object Object]`.
|
|
15
|
+
* Anything else is returned untouched.
|
|
17
16
|
*/
|
|
18
17
|
declare function unwrapPlaceholder(expression: t.Expression): [string | typeof AUTO_INCREMENT_IDENTIFIER, t.Expression];
|
|
19
18
|
/**
|
|
@@ -22,27 +21,24 @@ declare function unwrapPlaceholder(expression: t.Expression): [string | typeof A
|
|
|
22
21
|
*
|
|
23
22
|
* A value is compared whole: the same variable interpolated twice is one value,
|
|
24
23
|
* while `${name}` beside `${{ name: author.name }}` is two things claiming one
|
|
25
|
-
* name. An element is compared on its opening element alone
|
|
26
|
-
*
|
|
27
|
-
* translation rather than the source.
|
|
24
|
+
* name. An element is compared on its opening element alone, since its children
|
|
25
|
+
* come from the translation rather than the source.
|
|
28
26
|
*/
|
|
29
27
|
declare function isEquivalentPlaceholder(a: any, b: any): boolean;
|
|
30
28
|
declare function parseExpression(expression: t.Expression, fallback?: false): CompositeMessage | null;
|
|
31
29
|
declare function parseExpression(expression: t.Expression, fallback: true): CompositeMessage | ArgumentMessage;
|
|
32
30
|
/**
|
|
33
|
-
* The notes a translator is meant to read, taken from the comments
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* and stays there.
|
|
31
|
+
* The notes a translator is meant to read, taken from the comments above a
|
|
32
|
+
* message: `// TRANSLATORS: keep this under 20 characters`. A comment without
|
|
33
|
+
* the marker is a note to whoever is reading the code, and stays there.
|
|
37
34
|
*/
|
|
38
35
|
declare function getTranslatorComments(comments: readonly t.Comment[] | null | undefined): string[];
|
|
39
36
|
/**
|
|
40
37
|
* The comments written in front of a message, in the order they were written.
|
|
41
38
|
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* to the statement gathers them from wherever they landed.
|
|
39
|
+
* Babel attaches a comment on its own line to whatever node begins at it, so a
|
|
40
|
+
* message is rarely the node holding its own notes. Walking out to the
|
|
41
|
+
* statement gathers them from wherever they landed.
|
|
46
42
|
*/
|
|
47
43
|
declare function collectLeadingComments(path: NodePath<t.Node>): t.Comment[];
|
|
48
44
|
//#endregion
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saykit/transform-js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.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.11.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@babel/core": "^7.29.7",
|