@sigil-dev/compiler 0.9.16 → 0.9.18

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/package.json CHANGED
@@ -2,9 +2,16 @@
2
2
  "name": "@sigil-dev/compiler",
3
3
  "module": "index.ts",
4
4
  "type": "module",
5
- "version": "0.9.16",
5
+ "version": "0.9.18",
6
6
  "private": false,
7
- "description": "Compiler for the Sigil framework",
7
+ "description": "Compiles Sigil's JSX and $state/$derived macros into direct DOM updates",
8
+ "license": "MIT",
9
+ "homepage": "https://sigil.cane1712.dev/docs/internals/compiler",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://git.cane1712.dev/sigil/sigil.git",
13
+ "directory": "packages/compiler"
14
+ },
8
15
  "peerDependencies": {
9
16
  "typescript": "^5"
10
17
  },
@@ -30,7 +30,7 @@ export function collectChildren(
30
30
 
31
31
  for (const child of node.children) {
32
32
  if (t.isJSXText(child)) {
33
- const text = jsxText(child);
33
+ const text = jsxText(child, node.children);
34
34
  if (text === null) continue;
35
35
  childrenElements.push(t.stringLiteral(text));
36
36
  continue;
@@ -769,7 +769,7 @@ export function processElement(
769
769
  );
770
770
  }
771
771
  } else if (t.isJSXText(child)) {
772
- const text = jsxText(child);
772
+ const text = jsxText(child, node.children);
773
773
  if (text === null) continue;
774
774
  if (hydrate) {
775
775
  // SSR hydration: text already in DOM from SSR, do nothing.
@@ -77,7 +77,7 @@ export function processFragment(
77
77
  );
78
78
  }
79
79
  } else if (t.isJSXText(child)) {
80
- const text = jsxText(child);
80
+ const text = jsxText(child, node.children);
81
81
  if (text === null) continue;
82
82
  if (hydrate) {
83
83
  // SSR hydration: text already in DOM from SSR, do nothing.
@@ -77,7 +77,8 @@ function childrenSSR(
77
77
  const parts: Array<string | t.Expression> = [];
78
78
  for (const child of children) {
79
79
  if (t.isJSXText(child)) {
80
- const text = jsxText(child);
80
+ // raw text (<script>, <style>) has no inline neighbours
81
+ const text = jsxText(child, raw ? undefined : children);
81
82
  if (text !== null) parts.push(raw ? text : escapeText(text));
82
83
  continue;
83
84
  }
@@ -183,17 +183,94 @@ export function buildHydrationScope(
183
183
  );
184
184
  }
185
185
 
186
+ /** Text that takes no space before it: closing punctuation. */
187
+ const NO_SPACE_BEFORE =
188
+ /^[.,;:!?)\]}%\u2019\u201d\u00bb\u2026\u3001\u3002\u300d\u300f\uff09\uff0c\uff0e\uff1a\uff1b\uff01\uff1f]/;
189
+ /** Text that takes no space after it: opening brackets and quotes. */
190
+ const NO_SPACE_AFTER = /[(\[{\u2018\u201c\u00ab\u300c\u300e\uff08\/]$/;
191
+ /** Japanese and Chinese run words together, so a line break there is no space. */
192
+ const CJK =
193
+ /[\u3000-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff00-\uffef]/;
194
+
195
+ /** The sibling an inline neighbour is: an element or an expression, past comments. */
196
+ function neighbour(
197
+ siblings: readonly t.Node[],
198
+ from: number,
199
+ step: 1 | -1,
200
+ ): t.Node | null {
201
+ for (let i = from + step; i >= 0 && i < siblings.length; i += step) {
202
+ const n = siblings[i] as t.Node;
203
+ if (t.isJSXExpressionContainer(n) && t.isJSXEmptyExpression(n.expression))
204
+ continue;
205
+ return n;
206
+ }
207
+ return null;
208
+ }
209
+ /** Elements that sit in a line of text, where HTML renders a line break between two of them as a space. */
210
+ const INLINE = new Set([
211
+ "a",
212
+ "abbr",
213
+ "b",
214
+ "bdi",
215
+ "bdo",
216
+ "cite",
217
+ "code",
218
+ "data",
219
+ "dfn",
220
+ "em",
221
+ "i",
222
+ "kbd",
223
+ "mark",
224
+ "q",
225
+ "s",
226
+ "samp",
227
+ "small",
228
+ "span",
229
+ "strong",
230
+ "sub",
231
+ "sup",
232
+ "time",
233
+ "u",
234
+ "var",
235
+ ]);
236
+ const isInline = (n: t.Node | null) =>
237
+ n !== null &&
238
+ t.isJSXElement(n) &&
239
+ t.isJSXIdentifier(n.openingElement.name) &&
240
+ INLINE.has(n.openingElement.name.name);
241
+
242
+ const isNode = (n: t.Node | null) =>
243
+ n !== null &&
244
+ (t.isJSXElement(n) ||
245
+ t.isJSXFragment(n) ||
246
+ t.isJSXExpressionContainer(n) ||
247
+ t.isJSXSpreadChild(n));
248
+
186
249
  /**
187
- * The text a JSXText child renders, or null when nothing is left. This is
188
- * the standard JSX whitespace rule (React and Babel use it too): whitespace
189
- * touching a line break is dropped, lines that are only whitespace vanish,
190
- * and the remaining lines join with one space. Whitespace that doesn't touch
191
- * a line break is kept, so `Hello <b>big</b> world` keeps both spaces.
250
+ * The text a JSXText child renders, or null when nothing is left. Whitespace
251
+ * that doesn't touch a line break is kept, so `Hello <b>big</b> world` keeps
252
+ * both spaces. Lines that are only whitespace vanish, the remaining lines
253
+ * join with one space, and the whitespace at the start and end of an
254
+ * element's content goes, so indentation never becomes text.
255
+ *
256
+ * Unlike React's rule, a line break between text and an element or
257
+ * expression next to it reads as a space, the way it does in HTML:
258
+ *
259
+ * loaded from
260
+ * <a href="…">sekai.best</a>.
261
+ *
262
+ * is "loaded from sekai.best." with no {" "}. No space goes before closing
263
+ * punctuation (</a>⏎.), after an opening bracket or quote, or next to
264
+ * Japanese and Chinese text. Pass the parent's children as `siblings` to get
265
+ * this; without them (a <script>'s text) the line breaks just go.
192
266
  *
193
267
  * Every mode (ssr, hydrate, dom) must go through this: if they disagree,
194
268
  * server HTML and client-rendered pages show different text.
195
269
  */
196
- export function jsxText(child: t.JSXText): string | null {
270
+ export function jsxText(
271
+ child: t.JSXText,
272
+ siblings?: readonly t.Node[],
273
+ ): string | null {
197
274
  const lines = child.value.split(/\r\n|\n|\r/);
198
275
  let lastNonEmpty = 0;
199
276
  for (let i = 0; i < lines.length; i++) {
@@ -207,7 +284,38 @@ export function jsxText(child: t.JSXText): string | null {
207
284
  if (!line) continue;
208
285
  out += i === lastNonEmpty ? line : `${line} `;
209
286
  }
210
- return out || null;
287
+ if (!out) {
288
+ // a line break between two inline elements is a space, as in HTML:
289
+ // <a href="…">three.js</a>
290
+ // <span>by the three.js authors</span>
291
+ // Between anything else (blocks, components) it is indentation.
292
+ if (siblings && /[\r\n]/.test(child.value)) {
293
+ const at = siblings.indexOf(child);
294
+ if (
295
+ isInline(neighbour(siblings, at, -1)) &&
296
+ isInline(neighbour(siblings, at, 1))
297
+ )
298
+ return " ";
299
+ }
300
+ return null;
301
+ }
302
+ if (!siblings) return out;
303
+
304
+ const at = siblings.indexOf(child);
305
+ // a line break before the text, after an element or expression
306
+ if (
307
+ /^[ \t]*[\r\n]/.test(child.value) &&
308
+ isNode(neighbour(siblings, at, -1))
309
+ ) {
310
+ if (!NO_SPACE_BEFORE.test(out) && !CJK.test(out[0] as string))
311
+ out = ` ${out}`;
312
+ }
313
+ // a line break after the text, before an element or expression
314
+ if (/[\r\n][ \t]*$/.test(child.value) && isNode(neighbour(siblings, at, 1))) {
315
+ if (!NO_SPACE_AFTER.test(out) && !CJK.test(out.at(-1) as string))
316
+ out = `${out} `;
317
+ }
318
+ return out;
211
319
  }
212
320
 
213
321
  /** JSX that runs when the expression is evaluated (not inside a function). */
@@ -72,7 +72,7 @@ describe("whitespace", () => {
72
72
  );
73
73
  });
74
74
 
75
- test("whitespace touching a line break is dropped (standard JSX)", () => {
75
+ test("a line break between text and an element reads as a space", () => {
76
76
  expectAll(
77
77
  `export const App = () => (
78
78
  <p>
@@ -80,7 +80,151 @@ describe("whitespace", () => {
80
80
  <b>big</b> world
81
81
  </p>
82
82
  );`,
83
- "<p>Hello<b>big</b> world</p>",
83
+ "<p>Hello <b>big</b> world</p>",
84
+ );
85
+ });
86
+
87
+ test("a link wrapped onto its own line needs no {' '}", () => {
88
+ expectAll(
89
+ `export const App = () => (
90
+ <li>
91
+ A few assets are loaded from
92
+ <a href="https://sekai.best">sekai.best</a>
93
+ .
94
+ </li>
95
+ );`,
96
+ '<li>A few assets are loaded from <a href="https://sekai.best">sekai.best</a>.</li>',
97
+ );
98
+ });
99
+
100
+ test("an element then text on the next line gets a space", () => {
101
+ expectAll(
102
+ `export const App = () => (
103
+ <p>
104
+ <b>Miku</b>
105
+ sings
106
+ </p>
107
+ );`,
108
+ "<p><b>Miku</b> sings</p>",
109
+ );
110
+ });
111
+
112
+ test("a line break between two inline elements is a space", () => {
113
+ expectAll(
114
+ `const LIBS = [["three.js", "the three.js authors", "MIT"]];
115
+ export const App = () => (
116
+ <ul>
117
+ {LIBS.map(([what, who, licence]) => (
118
+ <li>
119
+ <a href="https://threejs.org">{what}</a>
120
+ <span>
121
+ by {who}, {licence}
122
+ </span>
123
+ </li>
124
+ ))}
125
+ </ul>
126
+ );`,
127
+ '<ul><li><a href="https://threejs.org">three.js</a> <span>by the three.js authors, MIT</span></li></ul>',
128
+ );
129
+ expectAll(
130
+ `export const App = () => (
131
+ <p>
132
+ <b>bold</b>
133
+ <i>italic</i>
134
+ <code>code</code>
135
+ </p>
136
+ );`,
137
+ "<p><b>bold</b> <i>italic</i> <code>code</code></p>",
138
+ );
139
+ });
140
+
141
+ test("indentation between blocks and components stays nothing", () => {
142
+ expectAll(
143
+ `const Item = () => <span>x</span>;
144
+ export const App = () => (
145
+ <div>
146
+ <p>one</p>
147
+ <p>two</p>
148
+ <Item />
149
+ <Item />
150
+ <span>inline</span>
151
+ <div>block</div>
152
+ </div>
153
+ );`,
154
+ "<div><p>one</p><p>two</p><span>x</span><span>x</span><span>inline</span><div>block</div></div>",
155
+ );
156
+ });
157
+
158
+ test("expressions count as neighbours too", () => {
159
+ expectAll(
160
+ `const n = 3;
161
+ export const App = () => (
162
+ <p>
163
+ showing page
164
+ {n}
165
+ of 9
166
+ </p>
167
+ );`,
168
+ "<p>showing page 3 of 9</p>",
169
+ );
170
+ });
171
+
172
+ test("no space before closing punctuation or after an opening bracket", () => {
173
+ expectAll(
174
+ `export const App = () => (
175
+ <p>
176
+ (
177
+ <b>a</b>
178
+ ), <i>b</i>
179
+ !
180
+ </p>
181
+ );`,
182
+ "<p>(<b>a</b>), <i>b</i>!</p>",
183
+ );
184
+ });
185
+
186
+ test("no space next to Japanese text", () => {
187
+ expectAll(
188
+ `export const App = () => (
189
+ <p>
190
+ これは
191
+ <b>テスト</b>
192
+ です
193
+ </p>
194
+ );`,
195
+ "<p>これは<b>テスト</b>です</p>",
196
+ );
197
+ });
198
+
199
+ test("lines of only whitespace between elements still vanish", () => {
200
+ expectAll(
201
+ `export const App = () => (
202
+ <ul>
203
+ <li>a</li>
204
+ <li>b</li>
205
+ </ul>
206
+ );`,
207
+ "<ul><li>a</li><li>b</li></ul>",
208
+ );
209
+ });
210
+
211
+ test("an explicit {' '} still works", () => {
212
+ expectAll(
213
+ `export const App = () => <p>from{" "}<a>x</a></p>;`,
214
+ "<p>from <a>x</a></p>",
215
+ );
216
+ });
217
+
218
+ test("comments are not neighbours", () => {
219
+ expectAll(
220
+ `export const App = () => (
221
+ <p>
222
+ {/* nothing */}
223
+ text
224
+ {/* nothing */}
225
+ </p>
226
+ );`,
227
+ "<p>text</p>",
84
228
  );
85
229
  });
86
230