@saykit/transform-jsx 0.6.0 → 0.7.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 +64 -9
- package/package.json +3 -3
package/dist/parser.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AUTO_INCREMENT_IDENTIFIER, ArgumentMessage, ChoiceMessage, CompositeMessage, ElementMessage, LiteralMessage } from "@saykit/config/features/messages";
|
|
1
|
+
import { AUTO_INCREMENT_IDENTIFIER, ArgumentMessage, ChoiceMessage, CompositeMessage, ElementMessage, LiteralMessage, isArgumentType, validateArgumentStyle, validateBranchIdentifier } from "@saykit/config/features/messages";
|
|
2
2
|
import { unwrapPlaceholder } from "@saykit/transform-js/parser";
|
|
3
3
|
import * as t from "@babel/types";
|
|
4
4
|
//#region src/parser.ts
|
|
@@ -16,9 +16,7 @@ function parseJSXContainerElement(element) {
|
|
|
16
16
|
const [accessor] = processed;
|
|
17
17
|
const children = element.children.reduce((p, c, i, a) => {
|
|
18
18
|
if (t.isJSXText(c)) {
|
|
19
|
-
|
|
20
|
-
if (i === 0) text = text.trimStart();
|
|
21
|
-
if (i === a.length - 1) text = text.trimEnd();
|
|
19
|
+
const text = normalizeJSXText(c.value, i === 0, i === a.length - 1);
|
|
22
20
|
if (text) p.push(new LiteralMessage(text));
|
|
23
21
|
}
|
|
24
22
|
if (t.isJSXElement(c)) p.push(parseJSXElement(c, true));
|
|
@@ -44,15 +42,23 @@ function parseJSXOpeningElement(element) {
|
|
|
44
42
|
const processed = processJSXOpeningElement(element);
|
|
45
43
|
if (!processed) return null;
|
|
46
44
|
const [accessor, kind] = processed;
|
|
45
|
+
const descriptorId = findAttributeValueIfStringLiteralAsString(element.attributes, "id");
|
|
46
|
+
const descriptorContext = findAttributeValueIfStringLiteralAsString(element.attributes, "context");
|
|
47
|
+
const wrap = (children) => new CompositeMessage({
|
|
48
|
+
id: descriptorId,
|
|
49
|
+
context: descriptorContext
|
|
50
|
+
}, [], getReferences(element), children, accessor);
|
|
47
51
|
if (typeof kind === "string" && [
|
|
48
52
|
"select",
|
|
49
53
|
"ordinal",
|
|
50
54
|
"plural"
|
|
51
55
|
].includes(kind)) {
|
|
56
|
+
const offset = kind === "select" ? void 0 : findPluralOffset(element.attributes);
|
|
52
57
|
const branches = element.attributes.reduce((b, a) => {
|
|
53
58
|
if (!t.isJSXAttribute(a)) return b;
|
|
54
59
|
let identifier = getAttributeNameAsString(a);
|
|
55
60
|
if (identifier === "_" || identifier === "id" || identifier === "context") return b;
|
|
61
|
+
if (offset !== void 0 && identifier === "offset") return b;
|
|
56
62
|
if (identifier.startsWith("_") && identifier.length > 1 && !Number.isNaN(+identifier.slice(1))) identifier = identifier.slice(1);
|
|
57
63
|
if (t.isStringLiteral(a.value)) b.push({
|
|
58
64
|
identifier,
|
|
@@ -77,17 +83,66 @@ function parseJSXOpeningElement(element) {
|
|
|
77
83
|
}
|
|
78
84
|
return b;
|
|
79
85
|
}, []);
|
|
86
|
+
for (const branch of branches) validateBranchIdentifier(kind, branch.identifier);
|
|
80
87
|
const initialiser = findAttributeValueIfExpressionOrStringLiteral(element.attributes, "_");
|
|
81
88
|
if (!initialiser) return null;
|
|
82
89
|
const [identifier, selector] = unwrapPlaceholder(initialiser);
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
return wrap([new ChoiceMessage(kind, identifier, branches, selector, offset)]);
|
|
91
|
+
}
|
|
92
|
+
if (typeof kind === "string" && isArgumentType(kind)) {
|
|
93
|
+
const initialiser = findAttributeValueIfExpressionOrStringLiteral(element.attributes, "_");
|
|
94
|
+
if (!initialiser) return null;
|
|
95
|
+
const style = findAttributeValueIfStringLiteralAsString(element.attributes, "style");
|
|
96
|
+
if (style !== void 0) validateArgumentStyle(kind, style);
|
|
97
|
+
const [identifier, value] = unwrapPlaceholder(initialiser);
|
|
98
|
+
return wrap([new ArgumentMessage(identifier, value, {
|
|
99
|
+
type: kind,
|
|
100
|
+
style
|
|
101
|
+
})]);
|
|
88
102
|
}
|
|
89
103
|
return null;
|
|
90
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* The `offset` a plural subtracts from its selector before `#` is formatted.
|
|
107
|
+
* See the JS parser's counterpart — the constraints are the same, only the
|
|
108
|
+
* syntax carrying it differs.
|
|
109
|
+
*/
|
|
110
|
+
function findPluralOffset(attributes) {
|
|
111
|
+
for (const attribute of attributes) {
|
|
112
|
+
if (!t.isJSXAttribute(attribute)) continue;
|
|
113
|
+
if (getAttributeNameAsString(attribute) !== "offset") continue;
|
|
114
|
+
if (!t.isJSXExpressionContainer(attribute.value)) continue;
|
|
115
|
+
const { expression } = attribute.value;
|
|
116
|
+
if (!t.isNumericLiteral(expression)) continue;
|
|
117
|
+
if (!Number.isInteger(expression.value) || expression.value < 0) continue;
|
|
118
|
+
return expression.value;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Punctuation that never takes a space in front of it. A formatter wrapping
|
|
123
|
+
* long JSX regularly strands these at the start of a line, on their own or
|
|
124
|
+
* ahead of the rest of a clause.
|
|
125
|
+
*/
|
|
126
|
+
const CLOSING_PUNCTUATION = /^[.,;:!?)\]}»”’]/;
|
|
127
|
+
/**
|
|
128
|
+
* Collapse the whitespace in a JSX text child the way the source reads.
|
|
129
|
+
*
|
|
130
|
+
* A line break between a word and its neighbour is only how a formatter wrapped
|
|
131
|
+
* a long line, so it still means a space. Dropping it would run the words
|
|
132
|
+
* together — and because ids are content hashes, silently orphan every existing
|
|
133
|
+
* translation for the message.
|
|
134
|
+
*
|
|
135
|
+
* The exception is a line that begins with punctuation, which belongs tight
|
|
136
|
+
* against whatever precedes it. Only an implicit break is treated this way: a
|
|
137
|
+
* space the author wrote on the same line is deliberate and always survives.
|
|
138
|
+
*/
|
|
139
|
+
function normalizeJSXText(value, isFirst, isLast) {
|
|
140
|
+
let text = value.replace(/\s+/g, " ");
|
|
141
|
+
if (isFirst) text = text.trimStart();
|
|
142
|
+
else if (text.startsWith(" ") && /^[^\S\n]*\n/.test(value) && CLOSING_PUNCTUATION.test(text.slice(1))) text = text.slice(1);
|
|
143
|
+
if (isLast) text = text.trimEnd();
|
|
144
|
+
return text;
|
|
145
|
+
}
|
|
91
146
|
function processJSXOpeningElement(element) {
|
|
92
147
|
if (t.isJSXIdentifier(element.name) && element.name.name === "Say") return [element.name, null];
|
|
93
148
|
if (t.isJSXMemberExpression(element.name) && t.isJSXIdentifier(element.name.object) && element.name.object.name === "Say" && t.isJSXIdentifier(element.name.property)) return [element.name.object, element.name.property.name.toLowerCase()];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saykit/transform-jsx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
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.
|
|
51
|
-
"@saykit/transform-js": "^0.
|
|
50
|
+
"@saykit/config": "^0.7.0",
|
|
51
|
+
"@saykit/transform-js": "^0.7.0"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@babel/core": "^7.29.7",
|