@sigil-dev/compiler 0.9.21 → 0.9.23
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 +162 -162
- package/package.json +14 -13
- package/src/analysis/hydration.ts +899 -0
- package/src/babel/handlers/dom/state.ts +106 -106
- package/src/babel/index.ts +685 -667
- package/src/babel/jsx/anchor-mount.ts +426 -426
- package/src/babel/jsx/children.ts +116 -116
- package/src/babel/jsx/element.ts +1027 -960
- package/src/babel/jsx/fragment.ts +271 -271
- package/src/babel/jsx/keyed-list.ts +317 -317
- package/src/babel/jsx/ssr.ts +307 -297
- package/src/babel/jsx/utils.ts +355 -333
- package/src/babel/util/bind.ts +143 -143
- package/src/babel/util/compile-state.ts +5 -0
- package/src/babel/util/css.ts +208 -208
- package/src/babel/util/props.ts +256 -256
- package/src/babel/util/reactive-fns.ts +110 -0
- package/src/babel/util/static-assertion.ts +51 -0
- package/src/bun-plugin.ts +103 -103
- package/src/vite/index.ts +61 -61
- package/test/bun-plugin.test.ts +45 -45
- package/test/hydration-analysis.test.ts +133 -0
- package/test/hydration.test.ts +277 -277
- package/test/jsx.test.ts +254 -254
- package/test/reactive-helpers.test.ts +135 -0
- package/test/render-parity.test.ts +827 -827
- package/test/scoped-css.test.ts +166 -166
- package/test/ssr-boolean-attrs.test.ts +56 -56
- package/test/ssr-raw-text.test.ts +68 -68
- package/test/ssr.test.ts +130 -130
- package/test/static-assertion.test.ts +123 -0
- package/test/static-skip.test.ts +292 -0
- package/test/style-object.test.ts +36 -36
package/src/babel/jsx/utils.ts
CHANGED
|
@@ -1,333 +1,355 @@
|
|
|
1
|
-
import { types as t } from "@babel/core";
|
|
2
|
-
import { isImportRead, isStoreRead } from "../util/props.ts";
|
|
3
|
-
|
|
4
|
-
export const ATTR_MAP: Record<string, string> = {
|
|
5
|
-
class: "className",
|
|
6
|
-
for: "htmlFor",
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (t.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (t.
|
|
48
|
-
return
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (t.
|
|
57
|
-
return
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
*
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
hydrate: boolean,
|
|
140
|
-
nodesVar = "__nodes",
|
|
141
|
-
parentVar?: string,
|
|
142
|
-
): t.Expression {
|
|
143
|
-
if (hydrate) {
|
|
144
|
-
const args: t.Expression[] = [t.identifier(nodesVar)];
|
|
145
|
-
if (parentVar) {
|
|
146
|
-
args.push(t.identifier(parentVar));
|
|
147
|
-
}
|
|
148
|
-
return t.callExpression(t.identifier("
|
|
149
|
-
}
|
|
150
|
-
return t.callExpression(
|
|
151
|
-
t.memberExpression(
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
)
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
)
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
"
|
|
234
|
-
"
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
) {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
if (
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
if (
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}
|
|
1
|
+
import { types as t } from "@babel/core";
|
|
2
|
+
import { isImportRead, isStoreRead } from "../util/props.ts";
|
|
3
|
+
|
|
4
|
+
export const ATTR_MAP: Record<string, string> = {
|
|
5
|
+
class: "className",
|
|
6
|
+
for: "htmlFor",
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The names the compiler treats as reactive in one file.
|
|
11
|
+
*
|
|
12
|
+
* A `Set` of signal names, plus `reactiveFns`: same-file functions that read
|
|
13
|
+
* one (see `collectReactiveFns`). Kept on the same object so the many
|
|
14
|
+
* `containsSignal(expr, signals)` call sites need no new argument.
|
|
15
|
+
*/
|
|
16
|
+
export class SignalScope extends Set<string> {
|
|
17
|
+
readonly reactiveFns = new Set<string>();
|
|
18
|
+
override clear(): void {
|
|
19
|
+
super.clear();
|
|
20
|
+
this.reactiveFns.clear();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function containsSignal(
|
|
25
|
+
expr: t.Expression | t.Node,
|
|
26
|
+
signals: Set<string>,
|
|
27
|
+
asCallee = false,
|
|
28
|
+
): boolean {
|
|
29
|
+
if (!expr) return false;
|
|
30
|
+
if (t.isIdentifier(expr)) return signals.has(expr.name);
|
|
31
|
+
if (t.isCallExpression(expr)) {
|
|
32
|
+
if (isStoreRead(expr)) return true;
|
|
33
|
+
// `label()` where label reads a signal inside its body
|
|
34
|
+
if (
|
|
35
|
+
t.isIdentifier(expr.callee) &&
|
|
36
|
+
(signals as Partial<SignalScope>).reactiveFns?.has(expr.callee.name)
|
|
37
|
+
) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
return (
|
|
41
|
+
containsSignal(expr.callee as t.Expression, signals, true) ||
|
|
42
|
+
expr.arguments.some((a) => containsSignal(a as t.Expression, signals))
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
// a read on an imported store; `utils.format` as a callee is not one
|
|
46
|
+
if (!asCallee && isImportRead(expr)) return true;
|
|
47
|
+
if (t.isBinaryExpression(expr)) {
|
|
48
|
+
return (
|
|
49
|
+
containsSignal(expr.left as t.Expression, signals) ||
|
|
50
|
+
containsSignal(expr.right, signals)
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
if (t.isMemberExpression(expr)) {
|
|
54
|
+
return containsSignal(expr.object as t.Expression, signals);
|
|
55
|
+
}
|
|
56
|
+
if (t.isLogicalExpression(expr)) {
|
|
57
|
+
return (
|
|
58
|
+
containsSignal(expr.left as t.Expression, signals) ||
|
|
59
|
+
containsSignal(expr.right as t.Expression, signals)
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
if (t.isConditionalExpression(expr)) {
|
|
63
|
+
return (
|
|
64
|
+
containsSignal(expr.test as t.Expression, signals) ||
|
|
65
|
+
containsSignal(expr.consequent as t.Expression, signals) ||
|
|
66
|
+
containsSignal(expr.alternate as t.Expression, signals)
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
if (t.isUnaryExpression(expr)) {
|
|
70
|
+
return containsSignal(expr.argument as t.Expression, signals);
|
|
71
|
+
}
|
|
72
|
+
if (t.isTemplateLiteral(expr)) {
|
|
73
|
+
return expr.expressions.some((e) =>
|
|
74
|
+
containsSignal(e as t.Expression, signals),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
// ── additions ──
|
|
78
|
+
if (t.isArrayExpression(expr)) {
|
|
79
|
+
return expr.elements.some(
|
|
80
|
+
(e) => e !== null && containsSignal(e as t.Expression, signals),
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if (t.isObjectExpression(expr)) {
|
|
84
|
+
return expr.properties.some((p) => {
|
|
85
|
+
if (t.isObjectProperty(p)) {
|
|
86
|
+
const computedKey = p.computed
|
|
87
|
+
? containsSignal(p.key as t.Expression, signals)
|
|
88
|
+
: false;
|
|
89
|
+
return computedKey || containsSignal(p.value as t.Expression, signals);
|
|
90
|
+
}
|
|
91
|
+
if (t.isSpreadElement(p)) return containsSignal(p.argument, signals);
|
|
92
|
+
return false;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (t.isSequenceExpression(expr)) {
|
|
96
|
+
return expr.expressions.some((e) => containsSignal(e, signals));
|
|
97
|
+
}
|
|
98
|
+
if (t.isAssignmentExpression(expr)) {
|
|
99
|
+
return containsSignal(expr.right, signals);
|
|
100
|
+
}
|
|
101
|
+
if (t.isSpreadElement(expr)) {
|
|
102
|
+
return containsSignal(expr.argument, signals);
|
|
103
|
+
}
|
|
104
|
+
// ArrowFunction/Function — don't traverse, signals inside are captured
|
|
105
|
+
// not reactive at the call site (onclick={() => count()} is fine as-is)
|
|
106
|
+
if (t.isArrowFunctionExpression(expr) || t.isFunctionExpression(expr)) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
// Babel represents ?. as distinct node types not in t.is* guards
|
|
110
|
+
if (t.isOptionalMemberExpression(expr)) {
|
|
111
|
+
return containsSignal((expr as any).object, signals);
|
|
112
|
+
}
|
|
113
|
+
if (t.isOptionalCallExpression(expr)) {
|
|
114
|
+
const n = expr as any;
|
|
115
|
+
return (
|
|
116
|
+
containsSignal(n.callee, signals) ||
|
|
117
|
+
n.arguments.some((a: t.Node) => containsSignal(a, signals))
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function isPrimitive(expr: t.Expression): boolean {
|
|
124
|
+
if (t.isCallExpression(expr) && t.isIdentifier(expr.callee)) return true;
|
|
125
|
+
if (t.isBinaryExpression(expr)) return true;
|
|
126
|
+
if (t.isTemplateLiteral(expr)) return true;
|
|
127
|
+
if (t.isIdentifier(expr)) return true;
|
|
128
|
+
// user().name is a MemberExpression after state handler rewrites user → user()
|
|
129
|
+
if (t.isMemberExpression(expr)) return true;
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Generate createElement call. In hydrate mode, claims from node pool.
|
|
135
|
+
* When parentVar is provided, passes it to claim() for SPA fallback (append on create).
|
|
136
|
+
*/
|
|
137
|
+
export function getCreateElement(
|
|
138
|
+
tag: string,
|
|
139
|
+
hydrate: boolean,
|
|
140
|
+
nodesVar = "__nodes",
|
|
141
|
+
parentVar?: string,
|
|
142
|
+
): t.Expression {
|
|
143
|
+
if (hydrate) {
|
|
144
|
+
const args: t.Expression[] = [t.identifier(nodesVar), t.stringLiteral(tag)];
|
|
145
|
+
if (parentVar) {
|
|
146
|
+
args.push(t.identifier(parentVar));
|
|
147
|
+
}
|
|
148
|
+
return t.callExpression(t.identifier("claim"), args);
|
|
149
|
+
}
|
|
150
|
+
return t.callExpression(
|
|
151
|
+
t.memberExpression(t.identifier("document"), t.identifier("createElement")),
|
|
152
|
+
[t.stringLiteral(tag)],
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Generate createTextNode call. In hydrate mode, claims from node pool.
|
|
158
|
+
* When parentVar is provided, passes it to claimText() for SPA fallback.
|
|
159
|
+
*/
|
|
160
|
+
export function getCreateText(
|
|
161
|
+
hydrate: boolean,
|
|
162
|
+
nodesVar = "__nodes",
|
|
163
|
+
parentVar?: string,
|
|
164
|
+
): t.Expression {
|
|
165
|
+
if (hydrate) {
|
|
166
|
+
const args: t.Expression[] = [t.identifier(nodesVar)];
|
|
167
|
+
if (parentVar) {
|
|
168
|
+
args.push(t.identifier(parentVar));
|
|
169
|
+
}
|
|
170
|
+
return t.callExpression(t.identifier("claimText"), args);
|
|
171
|
+
}
|
|
172
|
+
return t.callExpression(
|
|
173
|
+
t.memberExpression(
|
|
174
|
+
t.identifier("document"),
|
|
175
|
+
t.identifier("createTextNode"),
|
|
176
|
+
),
|
|
177
|
+
[t.stringLiteral("")],
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Generate `const <nodesVar> = Array.from(el.childNodes)` for hydration scope.
|
|
183
|
+
* Returns the generated variable name for use by children.
|
|
184
|
+
*/
|
|
185
|
+
export function buildHydrationScope(
|
|
186
|
+
parentVar: string,
|
|
187
|
+
statements: t.Statement[],
|
|
188
|
+
nodesVar: string,
|
|
189
|
+
): void {
|
|
190
|
+
statements.push(
|
|
191
|
+
t.variableDeclaration("const", [
|
|
192
|
+
t.variableDeclarator(
|
|
193
|
+
t.identifier(nodesVar),
|
|
194
|
+
t.callExpression(
|
|
195
|
+
t.memberExpression(t.identifier("Array"), t.identifier("from")),
|
|
196
|
+
[
|
|
197
|
+
t.memberExpression(
|
|
198
|
+
t.identifier(parentVar),
|
|
199
|
+
t.identifier("childNodes"),
|
|
200
|
+
),
|
|
201
|
+
],
|
|
202
|
+
),
|
|
203
|
+
),
|
|
204
|
+
]),
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Text that takes no space before it: closing punctuation. */
|
|
209
|
+
const NO_SPACE_BEFORE =
|
|
210
|
+
/^[.,;:!?)\]}%\u2019\u201d\u00bb\u2026\u3001\u3002\u300d\u300f\uff09\uff0c\uff0e\uff1a\uff1b\uff01\uff1f]/;
|
|
211
|
+
/** Text that takes no space after it: opening brackets and quotes. */
|
|
212
|
+
const NO_SPACE_AFTER = /[(\[{\u2018\u201c\u00ab\u300c\u300e\uff08\/]$/;
|
|
213
|
+
/** Japanese and Chinese run words together, so a line break there is no space. */
|
|
214
|
+
const CJK =
|
|
215
|
+
/[\u3000-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff00-\uffef]/;
|
|
216
|
+
|
|
217
|
+
/** The sibling an inline neighbour is: an element or an expression, past comments. */
|
|
218
|
+
function neighbour(
|
|
219
|
+
siblings: readonly t.Node[],
|
|
220
|
+
from: number,
|
|
221
|
+
step: 1 | -1,
|
|
222
|
+
): t.Node | null {
|
|
223
|
+
for (let i = from + step; i >= 0 && i < siblings.length; i += step) {
|
|
224
|
+
const n = siblings[i] as t.Node;
|
|
225
|
+
if (t.isJSXExpressionContainer(n) && t.isJSXEmptyExpression(n.expression))
|
|
226
|
+
continue;
|
|
227
|
+
return n;
|
|
228
|
+
}
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
/** Elements that sit in a line of text, where HTML renders a line break between two of them as a space. */
|
|
232
|
+
const INLINE = new Set([
|
|
233
|
+
"a",
|
|
234
|
+
"abbr",
|
|
235
|
+
"b",
|
|
236
|
+
"bdi",
|
|
237
|
+
"bdo",
|
|
238
|
+
"cite",
|
|
239
|
+
"code",
|
|
240
|
+
"data",
|
|
241
|
+
"dfn",
|
|
242
|
+
"em",
|
|
243
|
+
"i",
|
|
244
|
+
"kbd",
|
|
245
|
+
"mark",
|
|
246
|
+
"q",
|
|
247
|
+
"s",
|
|
248
|
+
"samp",
|
|
249
|
+
"small",
|
|
250
|
+
"span",
|
|
251
|
+
"strong",
|
|
252
|
+
"sub",
|
|
253
|
+
"sup",
|
|
254
|
+
"time",
|
|
255
|
+
"u",
|
|
256
|
+
"var",
|
|
257
|
+
]);
|
|
258
|
+
const isInline = (n: t.Node | null) =>
|
|
259
|
+
n !== null &&
|
|
260
|
+
t.isJSXElement(n) &&
|
|
261
|
+
t.isJSXIdentifier(n.openingElement.name) &&
|
|
262
|
+
INLINE.has(n.openingElement.name.name);
|
|
263
|
+
|
|
264
|
+
const isNode = (n: t.Node | null) =>
|
|
265
|
+
n !== null &&
|
|
266
|
+
(t.isJSXElement(n) ||
|
|
267
|
+
t.isJSXFragment(n) ||
|
|
268
|
+
t.isJSXExpressionContainer(n) ||
|
|
269
|
+
t.isJSXSpreadChild(n));
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* The text a JSXText child renders, or null when nothing is left. Whitespace
|
|
273
|
+
* that doesn't touch a line break is kept, so `Hello <b>big</b> world` keeps
|
|
274
|
+
* both spaces. Lines that are only whitespace vanish, the remaining lines
|
|
275
|
+
* join with one space, and the whitespace at the start and end of an
|
|
276
|
+
* element's content goes, so indentation never becomes text.
|
|
277
|
+
*
|
|
278
|
+
* Unlike React's rule, a line break between text and an element or
|
|
279
|
+
* expression next to it reads as a space, the way it does in HTML:
|
|
280
|
+
*
|
|
281
|
+
* loaded from
|
|
282
|
+
* <a href="…">sekai.best</a>.
|
|
283
|
+
*
|
|
284
|
+
* is "loaded from sekai.best." with no {" "}. No space goes before closing
|
|
285
|
+
* punctuation (</a>⏎.), after an opening bracket or quote, or next to
|
|
286
|
+
* Japanese and Chinese text. Pass the parent's children as `siblings` to get
|
|
287
|
+
* this; without them (a <script>'s text) the line breaks just go.
|
|
288
|
+
*
|
|
289
|
+
* Every mode (ssr, hydrate, dom) must go through this: if they disagree,
|
|
290
|
+
* server HTML and client-rendered pages show different text.
|
|
291
|
+
*/
|
|
292
|
+
export function jsxText(
|
|
293
|
+
child: t.JSXText,
|
|
294
|
+
siblings?: readonly t.Node[],
|
|
295
|
+
): string | null {
|
|
296
|
+
const lines = child.value.split(/\r\n|\n|\r/);
|
|
297
|
+
let lastNonEmpty = 0;
|
|
298
|
+
for (let i = 0; i < lines.length; i++) {
|
|
299
|
+
if (/[^ \t]/.test(lines[i] as string)) lastNonEmpty = i;
|
|
300
|
+
}
|
|
301
|
+
let out = "";
|
|
302
|
+
for (let i = 0; i < lines.length; i++) {
|
|
303
|
+
let line = (lines[i] as string).replace(/\t/g, " ");
|
|
304
|
+
if (i !== 0) line = line.replace(/^ +/, "");
|
|
305
|
+
if (i !== lines.length - 1) line = line.replace(/ +$/, "");
|
|
306
|
+
if (!line) continue;
|
|
307
|
+
out += i === lastNonEmpty ? line : `${line} `;
|
|
308
|
+
}
|
|
309
|
+
if (!out) {
|
|
310
|
+
// a line break between two inline elements is a space, as in HTML:
|
|
311
|
+
// <a href="…">three.js</a>
|
|
312
|
+
// <span>by the three.js authors</span>
|
|
313
|
+
// Between anything else (blocks, components) it is indentation.
|
|
314
|
+
if (siblings && /[\r\n]/.test(child.value)) {
|
|
315
|
+
const at = siblings.indexOf(child);
|
|
316
|
+
if (
|
|
317
|
+
isInline(neighbour(siblings, at, -1)) &&
|
|
318
|
+
isInline(neighbour(siblings, at, 1))
|
|
319
|
+
)
|
|
320
|
+
return " ";
|
|
321
|
+
}
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
if (!siblings) return out;
|
|
325
|
+
|
|
326
|
+
const at = siblings.indexOf(child);
|
|
327
|
+
// a line break before the text, after an element or expression
|
|
328
|
+
if (
|
|
329
|
+
/^[ \t]*[\r\n]/.test(child.value) &&
|
|
330
|
+
isNode(neighbour(siblings, at, -1))
|
|
331
|
+
) {
|
|
332
|
+
if (!NO_SPACE_BEFORE.test(out) && !CJK.test(out[0] as string))
|
|
333
|
+
out = ` ${out}`;
|
|
334
|
+
}
|
|
335
|
+
// a line break after the text, before an element or expression
|
|
336
|
+
if (/[\r\n][ \t]*$/.test(child.value) && isNode(neighbour(siblings, at, 1))) {
|
|
337
|
+
if (!NO_SPACE_AFTER.test(out) && !CJK.test(out.at(-1) as string))
|
|
338
|
+
out = `${out} `;
|
|
339
|
+
}
|
|
340
|
+
return out;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** JSX that runs when the expression is evaluated (not inside a function). */
|
|
344
|
+
export function hasJSX(expr: t.Node): boolean {
|
|
345
|
+
if (t.isJSXElement(expr) || t.isJSXFragment(expr)) return true;
|
|
346
|
+
if (t.isConditionalExpression(expr))
|
|
347
|
+
return hasJSX(expr.consequent) || hasJSX(expr.alternate);
|
|
348
|
+
if (t.isLogicalExpression(expr))
|
|
349
|
+
return hasJSX(expr.left) || hasJSX(expr.right);
|
|
350
|
+
if (t.isParenthesizedExpression(expr) || t.isTSAsExpression(expr))
|
|
351
|
+
return hasJSX(expr.expression);
|
|
352
|
+
if (t.isArrayExpression(expr))
|
|
353
|
+
return expr.elements.some((e) => e !== null && hasJSX(e));
|
|
354
|
+
return false;
|
|
355
|
+
}
|