@saykit/transform-jsx 0.6.0 → 0.6.1
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 +28 -4
- 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, 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));
|
|
@@ -77,6 +75,7 @@ function parseJSXOpeningElement(element) {
|
|
|
77
75
|
}
|
|
78
76
|
return b;
|
|
79
77
|
}, []);
|
|
78
|
+
for (const branch of branches) validateBranchIdentifier(kind, branch.identifier);
|
|
80
79
|
const initialiser = findAttributeValueIfExpressionOrStringLiteral(element.attributes, "_");
|
|
81
80
|
if (!initialiser) return null;
|
|
82
81
|
const [identifier, selector] = unwrapPlaceholder(initialiser);
|
|
@@ -88,6 +87,31 @@ function parseJSXOpeningElement(element) {
|
|
|
88
87
|
}
|
|
89
88
|
return null;
|
|
90
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Punctuation that never takes a space in front of it. A formatter wrapping
|
|
92
|
+
* long JSX regularly strands these at the start of a line, on their own or
|
|
93
|
+
* ahead of the rest of a clause.
|
|
94
|
+
*/
|
|
95
|
+
const CLOSING_PUNCTUATION = /^[.,;:!?)\]}»”’]/;
|
|
96
|
+
/**
|
|
97
|
+
* Collapse the whitespace in a JSX text child the way the source reads.
|
|
98
|
+
*
|
|
99
|
+
* A line break between a word and its neighbour is only how a formatter wrapped
|
|
100
|
+
* a long line, so it still means a space. Dropping it would run the words
|
|
101
|
+
* together — and because ids are content hashes, silently orphan every existing
|
|
102
|
+
* translation for the message.
|
|
103
|
+
*
|
|
104
|
+
* The exception is a line that begins with punctuation, which belongs tight
|
|
105
|
+
* against whatever precedes it. Only an implicit break is treated this way: a
|
|
106
|
+
* space the author wrote on the same line is deliberate and always survives.
|
|
107
|
+
*/
|
|
108
|
+
function normalizeJSXText(value, isFirst, isLast) {
|
|
109
|
+
let text = value.replace(/\s+/g, " ");
|
|
110
|
+
if (isFirst) text = text.trimStart();
|
|
111
|
+
else if (text.startsWith(" ") && /^[^\S\n]*\n/.test(value) && CLOSING_PUNCTUATION.test(text.slice(1))) text = text.slice(1);
|
|
112
|
+
if (isLast) text = text.trimEnd();
|
|
113
|
+
return text;
|
|
114
|
+
}
|
|
91
115
|
function processJSXOpeningElement(element) {
|
|
92
116
|
if (t.isJSXIdentifier(element.name) && element.name.name === "Say") return [element.name, null];
|
|
93
117
|
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.6.
|
|
3
|
+
"version": "0.6.1",
|
|
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.6.
|
|
51
|
-
"@saykit/transform-js": "^0.6.
|
|
50
|
+
"@saykit/config": "^0.6.1",
|
|
51
|
+
"@saykit/transform-js": "^0.6.1"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@babel/core": "^7.29.7",
|