@saykit/transform-jsx 0.0.0-beta-20260729131250 → 0.0.0-beta-20260803222843

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