@torpor/view 0.4.18 → 1.0.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/README.md +41 -16
- package/dist/Cleanup-D0wvGW5d.d.cts +9 -0
- package/dist/Cleanup-D0wvGW5d.d.cts.map +1 -0
- package/dist/Cleanup-D0wvGW5d.d.mts +9 -0
- package/dist/Cleanup-D0wvGW5d.d.mts.map +1 -0
- package/dist/Component-0MH0zd-m.d.cts +11 -0
- package/dist/Component-0MH0zd-m.d.cts.map +1 -0
- package/dist/Component-0MH0zd-m.d.mts +11 -0
- package/dist/Component-0MH0zd-m.d.mts.map +1 -0
- package/dist/Effect-BTKurOpr.d.mts +381 -0
- package/dist/Effect-BTKurOpr.d.mts.map +1 -0
- package/dist/Effect-COl3aRJV.d.cts +381 -0
- package/dist/Effect-COl3aRJV.d.cts.map +1 -0
- package/dist/compile.cjs +1727 -739
- package/dist/compile.d.cts +85 -50
- package/dist/compile.d.cts.map +1 -1
- package/dist/compile.d.mts +85 -50
- package/dist/compile.d.mts.map +1 -1
- package/dist/compile.mjs +1730 -741
- package/dist/compile.mjs.map +1 -1
- package/dist/dev.cjs +62 -63
- package/dist/dev.d.cts +4 -3
- package/dist/dev.d.cts.map +1 -1
- package/dist/dev.d.mts +4 -3
- package/dist/dev.d.mts.map +1 -1
- package/dist/dev.mjs +61 -62
- package/dist/dev.mjs.map +1 -1
- package/dist/{devContext-B3yskfhA.cjs → devContext-BulTn3U5.cjs} +6 -9
- package/dist/{devContext-CQ8RhBX-.mjs → devContext-ZNsPyOgB.mjs} +3 -4
- package/dist/devContext-ZNsPyOgB.mjs.map +1 -0
- package/dist/fromWebSocket-1QOjcrHE.cjs +182 -0
- package/dist/fromWebSocket-B52kN3-W.d.mts +91 -0
- package/dist/fromWebSocket-B52kN3-W.d.mts.map +1 -0
- package/dist/fromWebSocket-DeB35Q4w.d.cts +91 -0
- package/dist/fromWebSocket-DeB35Q4w.d.cts.map +1 -0
- package/dist/fromWebSocket-Dxc_uwm1.mjs +137 -0
- package/dist/fromWebSocket-Dxc_uwm1.mjs.map +1 -0
- package/dist/index.cjs +2073 -494
- package/dist/index.d.cts +600 -112
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +600 -112
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2054 -491
- package/dist/index.mjs.map +1 -1
- package/dist/ssr.cjs +78 -17
- package/dist/ssr.d.cts +64 -11
- package/dist/ssr.d.cts.map +1 -1
- package/dist/ssr.d.mts +64 -11
- package/dist/ssr.d.mts.map +1 -1
- package/dist/ssr.mjs +67 -14
- package/dist/ssr.mjs.map +1 -1
- package/package.json +21 -19
- package/dist/Cleanup-CYsytTEc.d.cts +0 -9
- package/dist/Cleanup-CYsytTEc.d.cts.map +0 -1
- package/dist/Cleanup-CgjyN0lW.d.mts +0 -9
- package/dist/Cleanup-CgjyN0lW.d.mts.map +0 -1
- package/dist/Component-DmWGMgak.d.mts +0 -11
- package/dist/Component-DmWGMgak.d.mts.map +0 -1
- package/dist/Component-kGLRFYg2.d.cts +0 -11
- package/dist/Component-kGLRFYg2.d.cts.map +0 -1
- package/dist/Region-Bb6HAb-F.d.cts +0 -201
- package/dist/Region-Bb6HAb-F.d.cts.map +0 -1
- package/dist/Region-CTchywkr.d.mts +0 -201
- package/dist/Region-CTchywkr.d.mts.map +0 -1
- package/dist/devContext-CQ8RhBX-.mjs.map +0 -1
- package/dist/formatText-BNP3P38F.d.cts +0 -17
- package/dist/formatText-BNP3P38F.d.cts.map +0 -1
- package/dist/formatText-D6Ov8Ub0.d.mts +0 -17
- package/dist/formatText-D6Ov8Ub0.d.mts.map +0 -1
- package/dist/formatText-DTIRmc3X.mjs +0 -61
- package/dist/formatText-DTIRmc3X.mjs.map +0 -1
- package/dist/formatText-Dhl2O0Ee.cjs +0 -96
package/dist/compile.cjs
CHANGED
|
@@ -1,13 +1,210 @@
|
|
|
1
|
-
|
|
2
|
-
//#region src/compile/utils/
|
|
3
|
-
function
|
|
4
|
-
let
|
|
5
|
-
for (let j = start + 1; j < content.length; j++) if (content[j] === "`" && content[j - 1] !== "\\" && level === 0) return j;
|
|
6
|
-
else if (content[j] === "{" && (level > 0 || content[j - 1] === "$")) level += 1;
|
|
7
|
-
else if (content[j] === "}" && level > 0) level -= 1;
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/compile/utils/endOfString.ts
|
|
3
|
+
function endOfString(char, content, start) {
|
|
4
|
+
for (let j = start + 1; j < content.length; j++) if (content[j] === char && content[j - 1] !== "\\") return j;
|
|
8
5
|
return content.length;
|
|
9
6
|
}
|
|
10
|
-
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/compile/utils/codeScanner.ts
|
|
9
|
+
/**
|
|
10
|
+
* Shared scanner for skipping over non-code parts of JavaScript expression
|
|
11
|
+
* text: string literals, template literals (including their interpolations),
|
|
12
|
+
* comments, and regex literals. Used everywhere the compiler scans
|
|
13
|
+
* expression text and needs to tell code from literal text.
|
|
14
|
+
*
|
|
15
|
+
* Note: this is for *code* contexts. For torpor template text (where quotes
|
|
16
|
+
* are literal characters and `{...}` are interpolation delimiters), use this
|
|
17
|
+
* only for the *interiors* of `{...}` expressions -- the template level
|
|
18
|
+
* itself has different rules.
|
|
19
|
+
*/
|
|
20
|
+
const REGEX_KEYWORDS = /* @__PURE__ */ new Set([
|
|
21
|
+
"return",
|
|
22
|
+
"typeof",
|
|
23
|
+
"instanceof",
|
|
24
|
+
"in",
|
|
25
|
+
"of",
|
|
26
|
+
"new",
|
|
27
|
+
"delete",
|
|
28
|
+
"void",
|
|
29
|
+
"throw",
|
|
30
|
+
"case",
|
|
31
|
+
"do",
|
|
32
|
+
"else",
|
|
33
|
+
"yield",
|
|
34
|
+
"await"
|
|
35
|
+
]);
|
|
36
|
+
/**
|
|
37
|
+
* If the character at `start` begins a string literal, a template literal, a
|
|
38
|
+
* comment (line or block), or a regex literal, returns the index just after
|
|
39
|
+
* it. Returns -1 if it doesn't (a `/` that is division, or any other
|
|
40
|
+
* character).
|
|
41
|
+
*/
|
|
42
|
+
function skipStringOrComment(value, start) {
|
|
43
|
+
return skipInRange(value, start);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns the [start, end) ranges of `value` that are code -- i.e. not the
|
|
47
|
+
* contents of string literals, regex literals, or comments. Template string
|
|
48
|
+
* contents are skipped, except for the contents of `${...}` interpolations,
|
|
49
|
+
* which are code again.
|
|
50
|
+
*/
|
|
51
|
+
function codeRanges(value) {
|
|
52
|
+
const ranges = [];
|
|
53
|
+
let codeStart = 0;
|
|
54
|
+
let i = 0;
|
|
55
|
+
while (i < value.length) {
|
|
56
|
+
const next = skipInRange(value, i, ranges, codeStart);
|
|
57
|
+
if (next !== -1) {
|
|
58
|
+
i = next;
|
|
59
|
+
codeStart = i;
|
|
60
|
+
} else i += 1;
|
|
61
|
+
}
|
|
62
|
+
pushRange(ranges, codeStart, value.length);
|
|
63
|
+
return ranges;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* `skipStringOrComment` with optional collection of the interrupted code
|
|
67
|
+
* range (for `codeRanges`), so there is a single implementation of the scan
|
|
68
|
+
* order.
|
|
69
|
+
*/
|
|
70
|
+
function skipInRange(value, start, ranges, codeStart) {
|
|
71
|
+
const char = value[start];
|
|
72
|
+
if (char === "'" || char === "\"") {
|
|
73
|
+
if (ranges) pushRange(ranges, codeStart, start);
|
|
74
|
+
return endOfString(char, value, start) + 1;
|
|
75
|
+
}
|
|
76
|
+
if (char === "`") {
|
|
77
|
+
if (ranges) pushRange(ranges, codeStart, start);
|
|
78
|
+
return skipTemplateString(value, start, ranges);
|
|
79
|
+
}
|
|
80
|
+
if (char === "/" && value[start + 1] === "/") {
|
|
81
|
+
if (ranges) pushRange(ranges, codeStart, start);
|
|
82
|
+
const end = value.indexOf("\n", start + 2);
|
|
83
|
+
return end === -1 ? value.length : end;
|
|
84
|
+
}
|
|
85
|
+
if (char === "/" && value[start + 1] === "*") {
|
|
86
|
+
if (ranges) pushRange(ranges, codeStart, start);
|
|
87
|
+
const end = value.indexOf("*/", start + 2);
|
|
88
|
+
return end === -1 ? value.length : end + 2;
|
|
89
|
+
}
|
|
90
|
+
if (char === "/" && isRegexPosition(value, start)) {
|
|
91
|
+
if (ranges) pushRange(ranges, codeStart, start);
|
|
92
|
+
return skipRegexLiteral(value, start);
|
|
93
|
+
}
|
|
94
|
+
return -1;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Skips over a template string, given the index of its opening backtick, and
|
|
98
|
+
* returns the index just after the closing backtick. Handles escaped
|
|
99
|
+
* characters, and scans `${...}` interpolations correctly (nested strings,
|
|
100
|
+
* templates, comments and regexes within them can't desync the scan).
|
|
101
|
+
*/
|
|
102
|
+
function skipTemplateString(value, start, ranges) {
|
|
103
|
+
let i = start + 1;
|
|
104
|
+
while (i < value.length) {
|
|
105
|
+
const char = value[i];
|
|
106
|
+
if (char === "\\") i += 2;
|
|
107
|
+
else if (char === "`") return i + 1;
|
|
108
|
+
else if (char === "$" && value[i + 1] === "{") i = endOfInterpolation(value, i + 2, ranges) + 1;
|
|
109
|
+
else i += 1;
|
|
110
|
+
}
|
|
111
|
+
return value.length;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Skips over the code inside a `${...}` interpolation, given the index just
|
|
115
|
+
* after the `${`, collecting its code ranges when provided. Returns the
|
|
116
|
+
* index of the matching `}`.
|
|
117
|
+
*/
|
|
118
|
+
function endOfInterpolation(value, start, ranges) {
|
|
119
|
+
let level = 0;
|
|
120
|
+
let codeStart = start;
|
|
121
|
+
let i = start;
|
|
122
|
+
while (i < value.length) {
|
|
123
|
+
const char = value[i];
|
|
124
|
+
if (char === "{") {
|
|
125
|
+
level += 1;
|
|
126
|
+
i += 1;
|
|
127
|
+
} else if (char === "}") {
|
|
128
|
+
if (level === 0) {
|
|
129
|
+
pushRange(ranges, codeStart, i);
|
|
130
|
+
return i;
|
|
131
|
+
}
|
|
132
|
+
level -= 1;
|
|
133
|
+
i += 1;
|
|
134
|
+
} else {
|
|
135
|
+
const next = skipInRange(value, i, ranges, codeStart);
|
|
136
|
+
if (next !== -1) {
|
|
137
|
+
i = next;
|
|
138
|
+
codeStart = i;
|
|
139
|
+
} else i += 1;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
pushRange(ranges, codeStart, value.length);
|
|
143
|
+
return value.length;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Decides whether the `/` at `slashIndex` starts a regex literal (rather
|
|
147
|
+
* than being division), by looking at the previous significant character.
|
|
148
|
+
* The standard lexical heuristic -- not a full parse, but correct for
|
|
149
|
+
* real-world code.
|
|
150
|
+
*/
|
|
151
|
+
function isRegexPosition(value, slashIndex) {
|
|
152
|
+
let i = slashIndex - 1;
|
|
153
|
+
while (i >= 0) {
|
|
154
|
+
const char = value[i];
|
|
155
|
+
if (char === " " || char === " " || char === "\n" || char === "\r") i -= 1;
|
|
156
|
+
else if (char === "/" && value[i - 1] === "*") {
|
|
157
|
+
const open = value.lastIndexOf("/*", i - 2);
|
|
158
|
+
if (open === -1) return true;
|
|
159
|
+
i = open - 1;
|
|
160
|
+
} else break;
|
|
161
|
+
}
|
|
162
|
+
if (i < 0) return true;
|
|
163
|
+
const char = value[i];
|
|
164
|
+
if (char === ")" || char === "]" || char === "}" || char === "\"" || char === "'" || char === "`") return false;
|
|
165
|
+
if (/[a-z0-9_$]/i.test(char)) {
|
|
166
|
+
let wordStart = i;
|
|
167
|
+
while (wordStart >= 0 && /[a-z0-9_$]/i.test(value[wordStart])) wordStart -= 1;
|
|
168
|
+
return REGEX_KEYWORDS.has(value.substring(wordStart + 1, i + 1));
|
|
169
|
+
}
|
|
170
|
+
if (char === "+" || char === "-") {
|
|
171
|
+
if (value[i - 1] === char) {
|
|
172
|
+
const before = value[i - 2];
|
|
173
|
+
return !(before && /[a-z0-9_$)\]]/i.test(before));
|
|
174
|
+
}
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Skips over a regex literal, given the index of its opening `/`, and
|
|
181
|
+
* returns the index just after its flags. Returns -1 if the literal is
|
|
182
|
+
* unterminated (in which case the `/` is treated as division).
|
|
183
|
+
*/
|
|
184
|
+
function skipRegexLiteral(value, start) {
|
|
185
|
+
let i = start + 1;
|
|
186
|
+
let inClass = false;
|
|
187
|
+
while (i < value.length) {
|
|
188
|
+
const char = value[i];
|
|
189
|
+
if (char === "\\") i += 2;
|
|
190
|
+
else if (char === "\n") return -1;
|
|
191
|
+
else if (char === "[") {
|
|
192
|
+
inClass = true;
|
|
193
|
+
i += 1;
|
|
194
|
+
} else if (char === "]") {
|
|
195
|
+
inClass = false;
|
|
196
|
+
i += 1;
|
|
197
|
+
} else if (char === "/" && !inClass) {
|
|
198
|
+
i += 1;
|
|
199
|
+
while (i < value.length && /[a-z]/i.test(value[i])) i += 1;
|
|
200
|
+
return i;
|
|
201
|
+
} else i += 1;
|
|
202
|
+
}
|
|
203
|
+
return -1;
|
|
204
|
+
}
|
|
205
|
+
function pushRange(ranges, start, end) {
|
|
206
|
+
if (ranges && end > start) ranges.push([start, end]);
|
|
207
|
+
}
|
|
11
208
|
//#endregion
|
|
12
209
|
//#region src/compile/utils/Builder.ts
|
|
13
210
|
var Builder = class {
|
|
@@ -24,7 +221,7 @@ var Builder = class {
|
|
|
24
221
|
append(text) {
|
|
25
222
|
if (this.#mapped) {
|
|
26
223
|
this.lineMap.push(this.#text.length);
|
|
27
|
-
for (let i
|
|
224
|
+
for (let i = 0; i < text.length; i++) if (text[i] === "\n") this.lineMap.push(this.#text.length + i);
|
|
28
225
|
this.#text += "\n" + text;
|
|
29
226
|
return;
|
|
30
227
|
}
|
|
@@ -40,12 +237,12 @@ var Builder = class {
|
|
|
40
237
|
} else {
|
|
41
238
|
if (text[i] === "}" || text[i] === ")" || text[i] === "]") this.#space -= 1;
|
|
42
239
|
this.#text += " ".repeat(Math.max(0, this.#space));
|
|
43
|
-
const start
|
|
240
|
+
const start = i;
|
|
44
241
|
while (i < text.length && text[i] !== "\n") {
|
|
45
|
-
if (text[i] === "`") i =
|
|
242
|
+
if (text[i] === "`") i = skipStringOrComment(text, i) - 1;
|
|
46
243
|
i++;
|
|
47
244
|
}
|
|
48
|
-
const line = text.substring(start
|
|
245
|
+
const line = text.substring(start, i + 1);
|
|
49
246
|
if (line.startsWith("*")) this.#text += " ";
|
|
50
247
|
this.#text += line;
|
|
51
248
|
if (text[i - 1] === "{" || text[i - 1] === "(" || text[i - 1] === "[") this.#space += 1;
|
|
@@ -58,7 +255,99 @@ var Builder = class {
|
|
|
58
255
|
return this.#text;
|
|
59
256
|
}
|
|
60
257
|
};
|
|
61
|
-
|
|
258
|
+
//#endregion
|
|
259
|
+
//#region src/compile/utils/isParentNode.ts
|
|
260
|
+
function isParentNode(node) {
|
|
261
|
+
return "children" in node;
|
|
262
|
+
}
|
|
263
|
+
//#endregion
|
|
264
|
+
//#region src/compile/utils/collectMarkupExpressions.ts
|
|
265
|
+
/**
|
|
266
|
+
* Walks a markup tree and returns every expression string it contains —
|
|
267
|
+
* `@control (...)` statements, reactive attribute values, and `{...}` text
|
|
268
|
+
* interpolations — concatenated. Used for import detection: `$`-primitives
|
|
269
|
+
* used inside markup (e.g. `@if ($pending(() => $state.data))`,
|
|
270
|
+
* `disabled={$pending(...)}`) must be imported, and the script-only scan in
|
|
271
|
+
* `buildCode`/`buildServerCode` doesn't see them.
|
|
272
|
+
*
|
|
273
|
+
* Static attribute values and literal text outside `{...}` are excluded, so a
|
|
274
|
+
* plain string containing e.g. `$pending` does not produce a spurious import.
|
|
275
|
+
*/
|
|
276
|
+
function collectMarkupExpressions(root) {
|
|
277
|
+
let out = "";
|
|
278
|
+
const walk = (node) => {
|
|
279
|
+
if (node.type === "control") out += node.statement + "\n";
|
|
280
|
+
if (node.type === "element" || node.type === "component" || node.type === "special") {
|
|
281
|
+
for (const attribute of node.attributes) if (attribute.reactive && attribute.value !== void 0) out += attribute.value + "\n";
|
|
282
|
+
}
|
|
283
|
+
if (node.type === "text") out += extractInterpolations(node.content) + "\n";
|
|
284
|
+
if (isParentNode(node)) for (const child of node.children) walk(child);
|
|
285
|
+
};
|
|
286
|
+
walk(root);
|
|
287
|
+
return out;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Extracts the `{expr}` interpolation segments from text content (level 0
|
|
291
|
+
* text is prose and is dropped), skipping string literals, template strings,
|
|
292
|
+
* comments and regex literals inside the braces (they're copied into the
|
|
293
|
+
* output verbatim).
|
|
294
|
+
*/
|
|
295
|
+
function extractInterpolations(content) {
|
|
296
|
+
let out = "";
|
|
297
|
+
let level = 0;
|
|
298
|
+
for (let i = 0; i < content.length; i++) {
|
|
299
|
+
const char = content[i];
|
|
300
|
+
if (char === "{") {
|
|
301
|
+
if (level === 0) out += "\n";
|
|
302
|
+
level++;
|
|
303
|
+
out += char;
|
|
304
|
+
} else if (char === "}") {
|
|
305
|
+
level--;
|
|
306
|
+
out += char;
|
|
307
|
+
} else if (level > 0) {
|
|
308
|
+
const skipped = skipStringOrComment(content, i);
|
|
309
|
+
if (skipped !== -1) {
|
|
310
|
+
out += content.substring(i, skipped);
|
|
311
|
+
i = skipped - 1;
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
out += char;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return out;
|
|
318
|
+
}
|
|
319
|
+
//#endregion
|
|
320
|
+
//#region src/compile/utils/markupRendersComponent.ts
|
|
321
|
+
/**
|
|
322
|
+
* Returns true if `markup` renders anything that forwards the component's
|
|
323
|
+
* `$context` parameter: a child component invocation (`<Child />`), a
|
|
324
|
+
* self-recursive `<@component self>`, or a `<slot>` definition (whose
|
|
325
|
+
* generated render call passes `$context` through to the filling parent).
|
|
326
|
+
*
|
|
327
|
+
* `contextProps` only captures *direct* `$context.x` reads in the script, so
|
|
328
|
+
* this is needed to decide whether the `$context` parameter is actually used:
|
|
329
|
+
* every child-component / slot render call passes `$context` positionally
|
|
330
|
+
* (see buildComponentNode / buildSlotNode and their server counterparts),
|
|
331
|
+
* even when the component never names `$context` explicitly.
|
|
332
|
+
*
|
|
333
|
+
* Bias is towards `true`: when uncertain we keep `$context` named, which is
|
|
334
|
+
* always safe at the cost of a missed unused-parameter cleanup.
|
|
335
|
+
*/
|
|
336
|
+
function markupRendersComponent(markup) {
|
|
337
|
+
return nodeRendersComponent(markup);
|
|
338
|
+
}
|
|
339
|
+
function nodeRendersComponent(node) {
|
|
340
|
+
if (node.type === "component") return true;
|
|
341
|
+
if (node.type === "special") {
|
|
342
|
+
const tagName = node.tagName;
|
|
343
|
+
if (tagName === "@component" || tagName === "slot") return true;
|
|
344
|
+
}
|
|
345
|
+
const children = node.children;
|
|
346
|
+
if (children) {
|
|
347
|
+
for (const child of children) if (nodeRendersComponent(child)) return true;
|
|
348
|
+
}
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
62
351
|
//#endregion
|
|
63
352
|
//#region src/compile/build/client/addMappedText.ts
|
|
64
353
|
function addMappedText(preText, text, postText, span, status, b) {
|
|
@@ -76,7 +365,6 @@ function addMappedText(preText, text, postText, span, status, b) {
|
|
|
76
365
|
});
|
|
77
366
|
} else b.append(preText + text + postText);
|
|
78
367
|
}
|
|
79
|
-
|
|
80
368
|
//#endregion
|
|
81
369
|
//#region src/compile/build/client/addPopDevBoundary.ts
|
|
82
370
|
function addPopDevBoundary(status, b) {
|
|
@@ -86,7 +374,6 @@ function addPopDevBoundary(status, b) {
|
|
|
86
374
|
b.append(`t_pop_dev_bound();`);
|
|
87
375
|
}
|
|
88
376
|
}
|
|
89
|
-
|
|
90
377
|
//#endregion
|
|
91
378
|
//#region src/compile/build/client/addPushDevBoundary.ts
|
|
92
379
|
function addPushDevBoundary(type, name, status, b) {
|
|
@@ -100,14 +387,73 @@ function addPushDevBoundary(type, name, status, b) {
|
|
|
100
387
|
b.append("");
|
|
101
388
|
}
|
|
102
389
|
}
|
|
103
|
-
|
|
104
390
|
//#endregion
|
|
105
391
|
//#region src/compile/types/comments.ts
|
|
106
|
-
const ANCHOR_COMMENT = "<!>";
|
|
107
392
|
const HYDRATION_START_COMMENT = "<![>";
|
|
108
393
|
const HYDRATION_END_COMMENT = "<!]>";
|
|
109
394
|
const HYDRATION_BREAK_COMMENT = "<!^>";
|
|
110
|
-
|
|
395
|
+
//#endregion
|
|
396
|
+
//#region src/compile/utils/isControlNode.ts
|
|
397
|
+
function isControlNode(node) {
|
|
398
|
+
return node.type === "control";
|
|
399
|
+
}
|
|
400
|
+
//#endregion
|
|
401
|
+
//#region src/compile/utils/isElementNode.ts
|
|
402
|
+
function isElementNode(node) {
|
|
403
|
+
return node.type === "element";
|
|
404
|
+
}
|
|
405
|
+
//#endregion
|
|
406
|
+
//#region src/compile/utils/nonRenderingOperations.ts
|
|
407
|
+
/**
|
|
408
|
+
* Control operations that produce no DOM output within a fragment.
|
|
409
|
+
*
|
|
410
|
+
* Used by both the fragment builder (to find the first child that actually
|
|
411
|
+
* renders) and the whitespace trimmer (so that whitespace adjacent to these
|
|
412
|
+
* nodes is treated as leading/trailing whitespace of the container, rather
|
|
413
|
+
* than as significant inter-sibling whitespace — without this, every row of
|
|
414
|
+
* a `@for { @key = ...; <tr>...</tr> }` would emit a leading whitespace
|
|
415
|
+
* text node, since the whitespace sits between the @key and the <tr>).
|
|
416
|
+
*/
|
|
417
|
+
const NON_RENDERING_OPERATIONS = /* @__PURE__ */ new Set([
|
|
418
|
+
"@key",
|
|
419
|
+
"@const",
|
|
420
|
+
"@console",
|
|
421
|
+
"@debugger",
|
|
422
|
+
"@function",
|
|
423
|
+
"@async function"
|
|
424
|
+
]);
|
|
425
|
+
//#endregion
|
|
426
|
+
//#region src/compile/utils/getSingleElementRoot.ts
|
|
427
|
+
/**
|
|
428
|
+
* Returns the single rendering `Element` root child of `children` if the
|
|
429
|
+
* fragment is "single-root element" — i.e. it has exactly one child that
|
|
430
|
+
* renders to the DOM, that child is an `Element`, and every other child is a
|
|
431
|
+
* non-rendering control node (`@key`, `@const`, …) or a comment. Returns
|
|
432
|
+
* `undefined` otherwise.
|
|
433
|
+
*
|
|
434
|
+
* When this returns an `Element`, the compiler emits the
|
|
435
|
+
* `t_fragment_el` / `t_root_el` / `t_add_element` codegen path, which clones
|
|
436
|
+
* the cached template's `firstElementChild` directly into the parent and
|
|
437
|
+
* skips the per-instance `DocumentFragment` allocation that `getFragment`
|
|
438
|
+
* produces.
|
|
439
|
+
*
|
|
440
|
+
* Text-root fragments (`{row.label}` standalone inside a `@for` body) are
|
|
441
|
+
* intentionally excluded: their root is a `Text`, not an `Element`, so
|
|
442
|
+
* `template.content.firstElementChild` would be `null`. Multi-root fragments
|
|
443
|
+
* (`<tr>...</tr><tr>...</tr>`) are excluded because no single element
|
|
444
|
+
* represents the whole fragment.
|
|
445
|
+
*/
|
|
446
|
+
function getSingleElementRoot(children) {
|
|
447
|
+
let renderingRoot;
|
|
448
|
+
for (const child of children) {
|
|
449
|
+
if (child.type === "comment") continue;
|
|
450
|
+
if (isControlNode(child) && NON_RENDERING_OPERATIONS.has(child.operation)) continue;
|
|
451
|
+
if (renderingRoot) return void 0;
|
|
452
|
+
if (!isElementNode(child)) return void 0;
|
|
453
|
+
renderingRoot = child;
|
|
454
|
+
}
|
|
455
|
+
return renderingRoot;
|
|
456
|
+
}
|
|
111
457
|
//#endregion
|
|
112
458
|
//#region src/compile/utils/isReactive.ts
|
|
113
459
|
/**
|
|
@@ -120,7 +466,6 @@ const HYDRATION_BREAK_COMMENT = "<!^>";
|
|
|
120
466
|
function isReactive(text) {
|
|
121
467
|
return text.includes("{") && text.includes("}");
|
|
122
468
|
}
|
|
123
|
-
|
|
124
469
|
//#endregion
|
|
125
470
|
//#region src/compile/utils/trimQuotes.ts
|
|
126
471
|
/**
|
|
@@ -139,7 +484,24 @@ function trimQuotes(text) {
|
|
|
139
484
|
}
|
|
140
485
|
return text.substring(start, end);
|
|
141
486
|
}
|
|
142
|
-
|
|
487
|
+
//#endregion
|
|
488
|
+
//#region src/compile/utils/voidTags.ts
|
|
489
|
+
const voidTags = [
|
|
490
|
+
"area",
|
|
491
|
+
"base",
|
|
492
|
+
"br",
|
|
493
|
+
"col",
|
|
494
|
+
"embed",
|
|
495
|
+
"hr",
|
|
496
|
+
"img",
|
|
497
|
+
"input",
|
|
498
|
+
"link",
|
|
499
|
+
"meta",
|
|
500
|
+
"param",
|
|
501
|
+
"source",
|
|
502
|
+
"track",
|
|
503
|
+
"wbr"
|
|
504
|
+
];
|
|
143
505
|
//#endregion
|
|
144
506
|
//#region src/compile/build/client/buildFragmentText.ts
|
|
145
507
|
/**
|
|
@@ -148,7 +510,10 @@ function trimQuotes(text) {
|
|
|
148
510
|
function buildFragmentText(node, status, b) {
|
|
149
511
|
const fragments = [];
|
|
150
512
|
buildNodeFragmentText(node, status, fragments);
|
|
151
|
-
if (fragments.length)
|
|
513
|
+
if (fragments.length) {
|
|
514
|
+
if (fragments.some((f) => !f.singleRootElement && f.usesFragmentCache !== false)) b.append(`const t_fragments: DocumentFragment[] = [];`);
|
|
515
|
+
if (fragments.some((f) => f.singleRootElement)) b.append(`const t_fragment_els: Element[] = [];`);
|
|
516
|
+
}
|
|
152
517
|
}
|
|
153
518
|
function buildNodeFragmentText(node, status, fragments, currentFragment) {
|
|
154
519
|
switch (node.type) {
|
|
@@ -185,7 +550,8 @@ function buildRootFragmentText(node, status, fragments) {
|
|
|
185
550
|
ns: status.ns,
|
|
186
551
|
effects: [],
|
|
187
552
|
events: [],
|
|
188
|
-
animations: []
|
|
553
|
+
animations: [],
|
|
554
|
+
singleRootElement: getSingleElementRoot(node.children) !== void 0
|
|
189
555
|
};
|
|
190
556
|
fragments.push(node.fragment);
|
|
191
557
|
for (let child of node.children) buildNodeFragmentText(child, status, fragments, node.fragment);
|
|
@@ -196,19 +562,26 @@ function buildControlFragmentText(node, status, fragments, currentFragment) {
|
|
|
196
562
|
case "@switch group":
|
|
197
563
|
case "@for group":
|
|
198
564
|
case "@await group":
|
|
565
|
+
case "@try group":
|
|
199
566
|
case "@replace group":
|
|
200
567
|
case "@html group":
|
|
201
|
-
currentFragment.text +=
|
|
568
|
+
currentFragment.text += "<!>";
|
|
202
569
|
for (let child of node.children) buildNodeFragmentText(child, status, fragments, currentFragment);
|
|
203
570
|
break;
|
|
204
571
|
default:
|
|
572
|
+
if (NON_RENDERING_OPERATIONS.has(node.operation)) {
|
|
573
|
+
for (let child of node.children) buildNodeFragmentText(child, status, fragments, currentFragment);
|
|
574
|
+
break;
|
|
575
|
+
}
|
|
205
576
|
node.fragment = {
|
|
206
577
|
number: fragments.length,
|
|
207
578
|
text: "",
|
|
208
579
|
ns: status.ns,
|
|
209
580
|
effects: [],
|
|
210
581
|
events: [],
|
|
211
|
-
animations: []
|
|
582
|
+
animations: [],
|
|
583
|
+
singleRootElement: getSingleElementRoot(node.children) !== void 0,
|
|
584
|
+
usesFragmentCache: node.operation !== "@html"
|
|
212
585
|
};
|
|
213
586
|
fragments.push(node.fragment);
|
|
214
587
|
for (let child of node.children) buildNodeFragmentText(child, status, fragments, node.fragment);
|
|
@@ -216,7 +589,7 @@ function buildControlFragmentText(node, status, fragments, currentFragment) {
|
|
|
216
589
|
}
|
|
217
590
|
}
|
|
218
591
|
function buildComponentFragmentText(node, status, fragments, currentFragment) {
|
|
219
|
-
currentFragment.text +=
|
|
592
|
+
currentFragment.text += "<!>";
|
|
220
593
|
if (node.children.length) {
|
|
221
594
|
node.fragment = {
|
|
222
595
|
number: fragments.length,
|
|
@@ -224,16 +597,18 @@ function buildComponentFragmentText(node, status, fragments, currentFragment) {
|
|
|
224
597
|
ns: status.ns,
|
|
225
598
|
effects: [],
|
|
226
599
|
events: [],
|
|
227
|
-
animations: []
|
|
600
|
+
animations: [],
|
|
601
|
+
singleRootElement: getSingleElementRoot(node.children) !== void 0
|
|
228
602
|
};
|
|
229
603
|
fragments.push(node.fragment);
|
|
230
604
|
for (let child of node.children) buildNodeFragmentText(child, status, fragments, node.fragment);
|
|
231
605
|
}
|
|
232
606
|
}
|
|
233
607
|
function buildElementFragmentText(node, status, fragments, currentFragment) {
|
|
608
|
+
const tagName = node.tagName === "@element" ? "el" : node.tagName;
|
|
234
609
|
let needsClass = node.scopeStyles;
|
|
235
|
-
currentFragment.text += `<${
|
|
236
|
-
let attributesText = node.attributes.filter((a) => !a.name.startsWith("on")).map((a) => {
|
|
610
|
+
currentFragment.text += `<${tagName}`;
|
|
611
|
+
let attributesText = node.attributes.filter((a) => !a.name.startsWith("on")).filter((a) => !(a.name === "self" && node.tagName === "@element")).map((a) => {
|
|
237
612
|
if (a.value && a.reactive) {
|
|
238
613
|
if (a.name.startsWith("data-")) return `${a.name}=""`;
|
|
239
614
|
} else if (a.name === "class" && node.scopeStyles) {
|
|
@@ -245,29 +620,33 @@ function buildElementFragmentText(node, status, fragments, currentFragment) {
|
|
|
245
620
|
if (attributesText) currentFragment.text += " " + attributesText;
|
|
246
621
|
if (needsClass) currentFragment.text += ` class="torp-${status.styleHash}"`;
|
|
247
622
|
currentFragment.text += ">";
|
|
248
|
-
|
|
249
|
-
|
|
623
|
+
if (!voidTags.includes(node.tagName)) {
|
|
624
|
+
for (let child of node.children) buildNodeFragmentText(child, status, fragments, currentFragment);
|
|
625
|
+
currentFragment.text += `</${tagName}>`;
|
|
626
|
+
}
|
|
250
627
|
}
|
|
251
628
|
function buildSpecialFragmentText(node, status, fragments, currentFragment) {
|
|
252
629
|
switch (node.tagName) {
|
|
253
630
|
case "slot":
|
|
254
|
-
currentFragment.text +=
|
|
631
|
+
currentFragment.text += "<!>";
|
|
255
632
|
for (let child of node.children) buildNodeFragmentText(child, status, fragments, node.fragment);
|
|
256
633
|
break;
|
|
257
634
|
case "fill":
|
|
635
|
+
case "filldef":
|
|
258
636
|
node.fragment = {
|
|
259
637
|
number: fragments.length,
|
|
260
638
|
text: "",
|
|
261
639
|
ns: status.ns,
|
|
262
640
|
effects: [],
|
|
263
641
|
events: [],
|
|
264
|
-
animations: []
|
|
642
|
+
animations: [],
|
|
643
|
+
singleRootElement: getSingleElementRoot(node.children) !== void 0
|
|
265
644
|
};
|
|
266
645
|
fragments.push(node.fragment);
|
|
267
646
|
for (let child of node.children) buildNodeFragmentText(child, status, fragments, node.fragment);
|
|
268
647
|
break;
|
|
269
648
|
case "@element":
|
|
270
|
-
|
|
649
|
+
buildElementFragmentText(node, status, fragments, currentFragment);
|
|
271
650
|
break;
|
|
272
651
|
case "@component":
|
|
273
652
|
buildComponentFragmentText(node, status, fragments, currentFragment);
|
|
@@ -275,13 +654,67 @@ function buildSpecialFragmentText(node, status, fragments, currentFragment) {
|
|
|
275
654
|
default: throw new Error(`Invalid special node: ${node.tagName}`);
|
|
276
655
|
}
|
|
277
656
|
}
|
|
278
|
-
|
|
657
|
+
//#endregion
|
|
658
|
+
//#region src/compile/utils/bindingTarget.ts
|
|
659
|
+
/**
|
|
660
|
+
* Builds the statements that write a component's prop value back to the
|
|
661
|
+
* expression it is bound to (e.g. for `&name={$state.user?.name}`).
|
|
662
|
+
*
|
|
663
|
+
* The bound expression may use optional chaining, which can't be assigned to
|
|
664
|
+
* directly. In that case the write is compiled to a guarded assignment: the
|
|
665
|
+
* prop value is read first (so the write re-runs when the child changes it),
|
|
666
|
+
* the target object is read with `$peek` (so that replacing it doesn't re-run
|
|
667
|
+
* the write with a stale prop value), and the value is written through it
|
|
668
|
+
* when it exists:
|
|
669
|
+
*
|
|
670
|
+
* ```
|
|
671
|
+
* bindingTarget("$state.user?.name", `props["name"]`, "name", imports)
|
|
672
|
+
* // => const t_bind_name = props["name"];
|
|
673
|
+
* // const t_target_name = $peek(() => $state.user);
|
|
674
|
+
* // if (t_target_name != null) t_target_name.name = t_bind_name;
|
|
675
|
+
* ```
|
|
676
|
+
*
|
|
677
|
+
* @param value The bound expression
|
|
678
|
+
* @param right The value to assign to it
|
|
679
|
+
* @param name The prop name, used to make the temp variable names unique
|
|
680
|
+
* @param imports The set of runtime imports to add `$peek` to when the
|
|
681
|
+
* expression needs a guarded write
|
|
682
|
+
*
|
|
683
|
+
* @returns The assignment statements
|
|
684
|
+
*/
|
|
685
|
+
function bindingTarget(value, right, name, imports) {
|
|
686
|
+
let lastChainStart = -1;
|
|
687
|
+
for (let i = 0; i < value.length; i++) {
|
|
688
|
+
let char = value[i];
|
|
689
|
+
if (char === "\"" || char === "'" || char === "`" || char === "/") {
|
|
690
|
+
const skipped = skipStringOrComment(value, i);
|
|
691
|
+
if (skipped !== -1) {
|
|
692
|
+
i = skipped - 1;
|
|
693
|
+
continue;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
if (char === "?" && value[i + 1] === "." && !/[0-9]/.test(value[i + 2] ?? "")) {
|
|
697
|
+
let next = value[i + 2];
|
|
698
|
+
if (next === "(") throw new Error(`Cannot bind to "${value}": optional calls are not supported in binding expressions`);
|
|
699
|
+
lastChainStart = i;
|
|
700
|
+
if (next === "[") i += 1;
|
|
701
|
+
continue;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
if (lastChainStart === -1) return `${value} = ${right};`;
|
|
705
|
+
let guard = value.substring(0, lastChainStart);
|
|
706
|
+
let next = value[lastChainStart + 2];
|
|
707
|
+
if (next !== "[" && !/[A-Za-z0-9_$]/.test(next ?? "")) throw new Error(`Cannot bind to "${value}": unsupported optional chain in binding expression`);
|
|
708
|
+
let suffix = value.substring(lastChainStart + (next === "[" ? 2 : 1));
|
|
709
|
+
let varSuffix = name.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
710
|
+
imports?.add("$peek");
|
|
711
|
+
return `const t_bind_${varSuffix} = ${right};\nconst t_target_${varSuffix} = $peek(() => ${guard});\nif (t_target_${varSuffix} != null) t_target_${varSuffix}${suffix} = t_bind_${varSuffix};`;
|
|
712
|
+
}
|
|
279
713
|
//#endregion
|
|
280
714
|
//#region src/compile/utils/isSpecialNode.ts
|
|
281
715
|
function isSpecialNode(node) {
|
|
282
716
|
return node.type === "special";
|
|
283
717
|
}
|
|
284
|
-
|
|
285
718
|
//#endregion
|
|
286
719
|
//#region src/compile/build/utils/nextVarName.ts
|
|
287
720
|
/**
|
|
@@ -299,7 +732,6 @@ function nextVarName(name, status) {
|
|
|
299
732
|
status.varNames[name] += 1;
|
|
300
733
|
return varName;
|
|
301
734
|
}
|
|
302
|
-
|
|
303
735
|
//#endregion
|
|
304
736
|
//#region src/compile/build/client/addMappedTextWithOffsets.ts
|
|
305
737
|
function addMappedTextWithOffsets(preText, text, postText, spans, offsets, lengths, status, b) {
|
|
@@ -321,35 +753,50 @@ function addMappedTextWithOffsets(preText, text, postText, spans, offsets, lengt
|
|
|
321
753
|
}
|
|
322
754
|
} else b.append(preText + text + postText);
|
|
323
755
|
}
|
|
324
|
-
|
|
325
756
|
//#endregion
|
|
326
757
|
//#region src/compile/build/client/buildAddFragment.ts
|
|
327
758
|
function buildAddFragment(node, status, b, parentName, anchorName) {
|
|
328
759
|
if (node.fragment) {
|
|
329
760
|
const fragment = node.fragment;
|
|
330
761
|
const fragmentName = `t_fragment_${fragment.number}`;
|
|
331
|
-
status.imports.add("t_add_fragment");
|
|
332
762
|
if (fragment.effects.length > 0) {
|
|
333
763
|
status.imports.add("$run");
|
|
764
|
+
let forVarMask;
|
|
765
|
+
for (let effect of fragment.effects) if (effect.forVarMask !== void 0) {
|
|
766
|
+
if (forVarMask === void 0) forVarMask = 0;
|
|
767
|
+
forVarMask |= effect.forVarMask;
|
|
768
|
+
}
|
|
769
|
+
let trailing = "";
|
|
770
|
+
if (forVarMask !== void 0) trailing = `, undefined, { forVarMask: ${forVarMask} }`;
|
|
334
771
|
b.append("$run(() => {");
|
|
335
772
|
if (status.options.mapped === true) for (let effect of fragment.effects) addMappedTextWithOffsets("", effect.functionBody, "", effect.spans, effect.offsets, effect.lengths, status, b);
|
|
336
773
|
else b.append(fragment.effects.map((e) => e.functionBody).join("\n"));
|
|
337
|
-
b.append(`}${status.options.dev === true ? `, "setAttributes"` :
|
|
774
|
+
b.append(`}${status.options.dev === true ? `, "setAttributes"${trailing}` : trailing});`);
|
|
338
775
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
parentName,
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
status.imports.add("
|
|
348
|
-
|
|
776
|
+
if (fragment.singleRootElement) {
|
|
777
|
+
status.imports.add("t_add_element");
|
|
778
|
+
b.append(`t_add_element(${fragment.endVarName ?? fragmentName}, ${parentName}, ${anchorName});`);
|
|
779
|
+
if (fragment.endVarName) {
|
|
780
|
+
status.imports.add("t_next");
|
|
781
|
+
b.append(`t_next(${fragment.endVarName});`);
|
|
782
|
+
}
|
|
783
|
+
} else {
|
|
784
|
+
status.imports.add("t_add_fragment");
|
|
785
|
+
let params = [
|
|
786
|
+
fragmentName,
|
|
787
|
+
parentName,
|
|
788
|
+
anchorName
|
|
789
|
+
];
|
|
790
|
+
if (fragment.endVarName) params.push(fragment.endVarName);
|
|
791
|
+
if (fragment.endVarName && fragment.rootVarName) params.push(fragment.rootVarName);
|
|
792
|
+
b.append(`t_add_fragment(${params.join(", ")});`);
|
|
793
|
+
if (fragment.endVarName) {
|
|
794
|
+
status.imports.add("t_next");
|
|
795
|
+
b.append(`t_next(${fragment.endVarName});`);
|
|
796
|
+
}
|
|
349
797
|
}
|
|
350
798
|
}
|
|
351
799
|
}
|
|
352
|
-
|
|
353
800
|
//#endregion
|
|
354
801
|
//#region src/compile/parse/utils/isSpaceChar.ts
|
|
355
802
|
/**
|
|
@@ -364,7 +811,6 @@ function isSpaceChar(text, index) {
|
|
|
364
811
|
let code = text.charCodeAt(index);
|
|
365
812
|
return code === 32 || code >= 9 && code <= 13;
|
|
366
813
|
}
|
|
367
|
-
|
|
368
814
|
//#endregion
|
|
369
815
|
//#region src/compile/parse/utils/isSpace.ts
|
|
370
816
|
/**
|
|
@@ -378,25 +824,11 @@ function isSpace(text) {
|
|
|
378
824
|
for (let i = 0; i < text.length; i++) if (!isSpaceChar(text, i)) return false;
|
|
379
825
|
return true;
|
|
380
826
|
}
|
|
381
|
-
|
|
382
|
-
//#endregion
|
|
383
|
-
//#region src/compile/utils/isControlNode.ts
|
|
384
|
-
function isControlNode(node) {
|
|
385
|
-
return node.type === "control";
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
//#endregion
|
|
389
|
-
//#region src/compile/utils/isElementNode.ts
|
|
390
|
-
function isElementNode(node) {
|
|
391
|
-
return node.type === "element";
|
|
392
|
-
}
|
|
393
|
-
|
|
394
827
|
//#endregion
|
|
395
828
|
//#region src/compile/utils/isTextNode.ts
|
|
396
829
|
function isTextNode(node) {
|
|
397
830
|
return node.type === "text";
|
|
398
831
|
}
|
|
399
|
-
|
|
400
832
|
//#endregion
|
|
401
833
|
//#region src/compile/build/client/buildFragment.ts
|
|
402
834
|
/**
|
|
@@ -421,9 +853,14 @@ function buildFragment(node, status, b, parentName, anchorName) {
|
|
|
421
853
|
declareFragmentVars(node.fragment, node, fragmentPath, status, b, parentName, anchorName, varPaths, false, false);
|
|
422
854
|
b.append(`]);`);
|
|
423
855
|
} else {
|
|
424
|
-
status.imports.add("t_fragment");
|
|
425
856
|
const fragmentText = fragment.text.replaceAll("`", "\\`").replaceAll(/\s+/g, " ");
|
|
426
|
-
|
|
857
|
+
if (fragment.singleRootElement) {
|
|
858
|
+
status.imports.add("t_fragment_el");
|
|
859
|
+
b.append(`const ${fragmentName} = t_fragment_el($parent.ownerDocument!, t_fragment_els, ${fragment.number}, \`${fragmentText}\`${fragment.ns ? ", true" : ""});`);
|
|
860
|
+
} else {
|
|
861
|
+
status.imports.add("t_fragment");
|
|
862
|
+
b.append(`const ${fragmentName} = t_fragment($parent.ownerDocument!, t_fragments, ${fragment.number}, \`${fragmentText}\`${fragment.ns ? ", true" : ""});`);
|
|
863
|
+
}
|
|
427
864
|
let fragmentPath = {
|
|
428
865
|
parent: null,
|
|
429
866
|
type: "fragment",
|
|
@@ -437,17 +874,27 @@ function buildFragment(node, status, b, parentName, anchorName) {
|
|
|
437
874
|
}
|
|
438
875
|
function maybeAddRootNodeDeclaration(node, fragment, fragmentName, status, b, varPaths) {
|
|
439
876
|
if (!node.children.length) return;
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
877
|
+
const rootName = `t_root_${fragment.number}`;
|
|
878
|
+
const firstRendering = firstRenderingChild(node.children);
|
|
879
|
+
const isTextRoot = !!(firstRendering && isTextNode(firstRendering));
|
|
880
|
+
const rootFn = fragment.singleRootElement ? "t_root_el" : "t_root";
|
|
881
|
+
status.imports.add(rootFn);
|
|
882
|
+
const params = [fragmentName];
|
|
883
|
+
if (!fragment.singleRootElement && isTextRoot) params.push("true");
|
|
884
|
+
const rootPath = `${rootFn}(${params.join(", ")})`;
|
|
885
|
+
b.append(`const ${rootName} = ${rootPath};`);
|
|
886
|
+
varPaths.set(`${rootFn}(${fragmentName})`, rootName);
|
|
887
|
+
if (!fragment.singleRootElement) fragment.rootVarName = rootName;
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* Returns the first child that actually renders to the DOM, skipping comments
|
|
891
|
+
* and no-output control nodes like @key/@const.
|
|
892
|
+
*/
|
|
893
|
+
function firstRenderingChild(children) {
|
|
894
|
+
for (const child of children) {
|
|
895
|
+
if (child.type === "comment") continue;
|
|
896
|
+
if (isControlNode(child) && NON_RENDERING_OPERATIONS.has(child.operation)) continue;
|
|
897
|
+
return child;
|
|
451
898
|
}
|
|
452
899
|
}
|
|
453
900
|
function declareFragmentVars(fragment, node, path, status, b, parentName, anchorName, varPaths, lastChild, declare) {
|
|
@@ -470,6 +917,7 @@ function declareFragmentVars(fragment, node, path, status, b, parentName, anchor
|
|
|
470
917
|
case "special":
|
|
471
918
|
declareSpecialFragmentVars(fragment, node, path, status, b, parentName, anchorName, varPaths, lastChild, declare);
|
|
472
919
|
break;
|
|
920
|
+
case "comment": break;
|
|
473
921
|
default: throw new Error(`Invalid node type: ${node.type}`);
|
|
474
922
|
}
|
|
475
923
|
}
|
|
@@ -482,6 +930,7 @@ function declareControlFragmentVars(fragment, node, path, status, b, parentName,
|
|
|
482
930
|
case "@switch group":
|
|
483
931
|
case "@for group":
|
|
484
932
|
case "@await group":
|
|
933
|
+
case "@try group":
|
|
485
934
|
case "@replace group":
|
|
486
935
|
case "@html group":
|
|
487
936
|
declareParentAndAnchorFragmentVars(fragment, node, path, status, b, parentName, anchorName, varPaths, node.operation.substring(1).replace(" group", ""), declare);
|
|
@@ -530,10 +979,17 @@ function declareElementFragmentVars(fragment, node, path, status, b, parentName,
|
|
|
530
979
|
if (topLevel) fragment.endVarName = node.varName;
|
|
531
980
|
const varPath = getFragmentVarPath(fragment, status, node.varName, elementPath, varPaths);
|
|
532
981
|
if (status.options.useCreateElement === true) b.append(`let ${node.varName};`);
|
|
533
|
-
else if (node.tagName === "@element") b.append(`let ${node.varName} = ${varPath} as ${elementTypeName(node)};`);
|
|
534
982
|
else {
|
|
535
|
-
b.append(`
|
|
536
|
-
|
|
983
|
+
if (node.tagName === "@element") b.append(`let ${node.varName} = ${varPath} as ${elementTypeName(node)};`);
|
|
984
|
+
else {
|
|
985
|
+
b.append(`const ${node.varName} = ${varPath} as ${elementTypeName(node)};`);
|
|
986
|
+
node.varName;
|
|
987
|
+
}
|
|
988
|
+
const refAttribute = node.attributes.find((a) => a.name === "&ref");
|
|
989
|
+
if (refAttribute) {
|
|
990
|
+
let { value, fullyReactive } = refAttribute;
|
|
991
|
+
if (value != null && fullyReactive) b.append(`${value} = ${node.varName};`);
|
|
992
|
+
}
|
|
537
993
|
}
|
|
538
994
|
}
|
|
539
995
|
}
|
|
@@ -577,7 +1033,7 @@ function declareTextFragmentVars(fragment, node, path, status, b, varPaths, last
|
|
|
577
1033
|
if (status.options.useCreateElement === true) b.append(`let ${node.varName};`);
|
|
578
1034
|
else {
|
|
579
1035
|
b.append(`const ${node.varName} = ${varPath};`);
|
|
580
|
-
|
|
1036
|
+
node.varName;
|
|
581
1037
|
}
|
|
582
1038
|
}
|
|
583
1039
|
} else {
|
|
@@ -594,8 +1050,9 @@ function declareSpecialFragmentVars(fragment, node, path, status, b, parentName,
|
|
|
594
1050
|
switch (node.tagName) {
|
|
595
1051
|
case "slot":
|
|
596
1052
|
case "fill":
|
|
1053
|
+
case "filldef":
|
|
597
1054
|
for (let [i, child] of node.children.entries()) {
|
|
598
|
-
if (node.tagName === "slot" && isSpecialNode(child) && child.tagName === "fill") continue;
|
|
1055
|
+
if (node.tagName === "slot" && isSpecialNode(child) && (child.tagName === "fill" || child.tagName === "filldef")) continue;
|
|
599
1056
|
declareFragmentVars(fragment, child, path, status, b, parentName, anchorName, varPaths, i === node.children.length - 1, declare);
|
|
600
1057
|
}
|
|
601
1058
|
break;
|
|
@@ -609,7 +1066,8 @@ function declareSpecialFragmentVars(fragment, node, path, status, b, parentName,
|
|
|
609
1066
|
}
|
|
610
1067
|
}
|
|
611
1068
|
function declareParentAndAnchorFragmentVars(fragment, node, path, status, b, parentName, anchorName, varPaths, name, declare) {
|
|
612
|
-
|
|
1069
|
+
const topLevel = !path.parent;
|
|
1070
|
+
if (topLevel) node.parentName = `t_fragment_${fragment.number}`;
|
|
613
1071
|
else {
|
|
614
1072
|
const parentPath = path;
|
|
615
1073
|
const oldChildren = parentPath.children;
|
|
@@ -622,7 +1080,7 @@ function declareParentAndAnchorFragmentVars(fragment, node, path, status, b, par
|
|
|
622
1080
|
if (status.options.useCreateElement === true) b.append(`let ${node.parentName} as HTMLElement;`);
|
|
623
1081
|
else {
|
|
624
1082
|
b.append(`const ${node.parentName} = ${parentVarPath} as HTMLElement;`);
|
|
625
|
-
|
|
1083
|
+
node.parentName;
|
|
626
1084
|
}
|
|
627
1085
|
varPaths.set(parentVarPath, node.parentName);
|
|
628
1086
|
}
|
|
@@ -635,12 +1093,13 @@ function declareParentAndAnchorFragmentVars(fragment, node, path, status, b, par
|
|
|
635
1093
|
};
|
|
636
1094
|
path.children.push(anchorPath);
|
|
637
1095
|
node.varName = nextVarName(`${name}_anchor`, status);
|
|
1096
|
+
if (topLevel) fragment.endVarName = node.varName;
|
|
638
1097
|
const anchorVarPath = getFragmentVarPath(fragment, status, node.varName, anchorPath, varPaths);
|
|
639
1098
|
if (status.options.useCreateElement === true) b.append(`let ${node.varName};`);
|
|
640
1099
|
else {
|
|
641
1100
|
status.imports.add("t_anchor");
|
|
642
1101
|
b.append(`let ${node.varName} = t_anchor(${anchorVarPath}) as HTMLElement;`);
|
|
643
|
-
|
|
1102
|
+
node.varName;
|
|
644
1103
|
}
|
|
645
1104
|
} else {
|
|
646
1105
|
status.imports.add("t_cmt");
|
|
@@ -653,7 +1112,8 @@ function getFragmentVarPath(fragment, status, name, path, varPaths) {
|
|
|
653
1112
|
let node = path;
|
|
654
1113
|
while (node.parent) node = node.parent;
|
|
655
1114
|
let varName = `t_fragment_${fragment.number}`;
|
|
656
|
-
|
|
1115
|
+
const rootFn = fragment.singleRootElement ? "t_root_el" : "t_root";
|
|
1116
|
+
let varPath = getFragmentVarPathPart(node, varName, status, true, rootFn);
|
|
657
1117
|
for (let [existingPath, existingName] of varPaths) if (varPath.includes(existingPath)) varPath = varPath.replace(existingPath, existingName);
|
|
658
1118
|
if (name !== "?") {
|
|
659
1119
|
varPaths.set(varPath, name);
|
|
@@ -678,10 +1138,10 @@ function getFragmentVarPath(fragment, status, name, path, varPaths) {
|
|
|
678
1138
|
varPath = varPath.replaceAll("t_next_text", "t_next");
|
|
679
1139
|
return varPath;
|
|
680
1140
|
}
|
|
681
|
-
function getFragmentVarPathPart(path, varPath, status, root = false) {
|
|
1141
|
+
function getFragmentVarPathPart(path, varPath, status, root = false, rootFn = "t_root") {
|
|
682
1142
|
if (root) {
|
|
683
|
-
status.imports.add(
|
|
684
|
-
varPath =
|
|
1143
|
+
status.imports.add(rootFn);
|
|
1144
|
+
varPath = `${rootFn}(${varPath})`;
|
|
685
1145
|
} else {
|
|
686
1146
|
status.imports.add("t_child");
|
|
687
1147
|
varPath = `t_child(${varPath})`;
|
|
@@ -696,37 +1156,72 @@ function getFragmentVarPathPart(path, varPath, status, root = false) {
|
|
|
696
1156
|
}
|
|
697
1157
|
return varPath;
|
|
698
1158
|
}
|
|
699
|
-
|
|
700
|
-
|
|
1159
|
+
//#endregion
|
|
1160
|
+
//#region src/compile/utils/forVarsReadIn.ts
|
|
1161
|
+
/**
|
|
1162
|
+
* Returns a bitmask of the for-loop variables whose substituted paths appear
|
|
1163
|
+
* in `expr`. Bit N is set when the Nth for-var (by position in `forVarNames`)
|
|
1164
|
+
* is read.
|
|
1165
|
+
*
|
|
1166
|
+
* After `replaceForVarNames` has run, every reference to a for-var in the
|
|
1167
|
+
* body has been rewritten to its data-bag path (e.g. `row` →
|
|
1168
|
+
* `t_item_42.data.row`, or `t_item_42.data` for single-var loops). This
|
|
1169
|
+
* helper scans the POST-substitution `expr` for those paths and returns a
|
|
1170
|
+
* bitmask — used to annotate each effect with `forVarMask` so the keyed-list
|
|
1171
|
+
* reconciler's no-proxy force-rerun path (`rerunRegionEffects`) can skip
|
|
1172
|
+
* effects that don't depend on the changed for-vars via a single bitwise AND.
|
|
1173
|
+
*
|
|
1174
|
+
* Conservative by design: a false positive (claiming a dependency that
|
|
1175
|
+
* doesn't really exist) just causes an unnecessary re-run, while a false
|
|
1176
|
+
* negative would drop a needed update. String literals that happen to
|
|
1177
|
+
* contain the substituted path are extremely unlikely and harmless.
|
|
1178
|
+
*/
|
|
1179
|
+
function forVarsReadIn(expr, forVarNames) {
|
|
1180
|
+
let mask = 0;
|
|
1181
|
+
for (let i = 0; i < forVarNames.length; i++) {
|
|
1182
|
+
const subPath = forVarNames[i][1];
|
|
1183
|
+
if (subPath && expr.includes(subPath)) mask |= 1 << i;
|
|
1184
|
+
}
|
|
1185
|
+
return mask;
|
|
1186
|
+
}
|
|
701
1187
|
//#endregion
|
|
702
1188
|
//#region src/compile/build/client/replaceForVarNames.ts
|
|
703
1189
|
/**
|
|
704
1190
|
* Variables used within a for loop need to be retrieved from the loop data.
|
|
705
1191
|
*/
|
|
706
1192
|
function replaceForVarNames(value, status) {
|
|
707
|
-
|
|
708
|
-
|
|
1193
|
+
if (status.forVarNames.length === 0) return value;
|
|
1194
|
+
let result = "";
|
|
1195
|
+
let index = 0;
|
|
1196
|
+
for (let [start, end] of codeRanges(value)) {
|
|
1197
|
+
result += value.substring(index, start);
|
|
1198
|
+
result += replaceVarNames(value.substring(start, end), status);
|
|
1199
|
+
index = end;
|
|
1200
|
+
}
|
|
1201
|
+
result += value.substring(index);
|
|
1202
|
+
return result;
|
|
1203
|
+
}
|
|
1204
|
+
function replaceVarNames(code, status) {
|
|
1205
|
+
for (let varName of status.forVarNames) code = code.replaceAll(new RegExp(`(^|\\s|\\(|\\[|\\{|!|\\?|\\.\\.\\.)${varName[0]}($|\\s|\\.|,|\\(|\\)|\\[|\\]|\\}|;|\\?)`, "g"), `$1${varName[1]}$2`);
|
|
1206
|
+
return code;
|
|
709
1207
|
}
|
|
710
|
-
|
|
711
1208
|
//#endregion
|
|
712
1209
|
//#region src/compile/build/client/buildRun.ts
|
|
713
1210
|
function buildRun(functionName, functionBody, status, b) {
|
|
714
1211
|
functionBody = replaceForVarNames(functionBody, status);
|
|
715
1212
|
addPushDevBoundary("run", functionName, status, b);
|
|
1213
|
+
let forVarMask = status.forVarNames.length > 0 ? forVarsReadIn(functionBody, status.forVarNames) : void 0;
|
|
1214
|
+
let trailing = "";
|
|
1215
|
+
if (status.options.dev === true) {
|
|
1216
|
+
trailing = `, "${functionName}"`;
|
|
1217
|
+
if (forVarMask !== void 0) trailing += `, { forVarMask: ${forVarMask} }`;
|
|
1218
|
+
} else if (forVarMask !== void 0) trailing = `, undefined, { forVarMask: ${forVarMask} }`;
|
|
716
1219
|
status.imports.add("$run");
|
|
717
1220
|
b.append("$run(() => {");
|
|
718
1221
|
b.append(functionBody);
|
|
719
|
-
b.append(`}${
|
|
1222
|
+
b.append(`}${trailing});`);
|
|
720
1223
|
addPopDevBoundary(status, b);
|
|
721
1224
|
}
|
|
722
|
-
|
|
723
|
-
//#endregion
|
|
724
|
-
//#region src/compile/utils/endOfString.ts
|
|
725
|
-
function endOfString(char, content, start) {
|
|
726
|
-
for (let j = start + 1; j < content.length; j++) if (content[j] === char && content[j - 1] !== "\\") return j;
|
|
727
|
-
return content.length;
|
|
728
|
-
}
|
|
729
|
-
|
|
730
1225
|
//#endregion
|
|
731
1226
|
//#region src/compile/build/client/getAttributeOffsets.ts
|
|
732
1227
|
function getAttributeOffsets(value, span) {
|
|
@@ -739,7 +1234,6 @@ function getAttributeOffsets(value, span) {
|
|
|
739
1234
|
let level = 0;
|
|
740
1235
|
for (let i = 0; i < value.length; i++) {
|
|
741
1236
|
const char = value[i];
|
|
742
|
-
const nextChar = value[i + 1];
|
|
743
1237
|
if (char === "{") {
|
|
744
1238
|
level++;
|
|
745
1239
|
if (level === 1) {
|
|
@@ -762,25 +1256,10 @@ function getAttributeOffsets(value, span) {
|
|
|
762
1256
|
}
|
|
763
1257
|
} else if (char === "`" && level === 0) textContent += "\\";
|
|
764
1258
|
else if (level > 0) {
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
continue;
|
|
770
|
-
} else if (char === "/" && nextChar === "*") {
|
|
771
|
-
let start = i;
|
|
772
|
-
i = value.indexOf("*/", i) + 1;
|
|
773
|
-
textContent += value.substring(start, i + 1);
|
|
774
|
-
continue;
|
|
775
|
-
} else if (char === "\"" || char === "'") {
|
|
776
|
-
let start = i;
|
|
777
|
-
i = endOfString(char, value, i);
|
|
778
|
-
textContent += value.substring(start, i + 1);
|
|
779
|
-
continue;
|
|
780
|
-
} else if (char === "`") {
|
|
781
|
-
let start = i;
|
|
782
|
-
i = endOfTemplateString(value, i);
|
|
783
|
-
textContent += value.substring(start, i + 1);
|
|
1259
|
+
const skipped = skipStringOrComment(value, i);
|
|
1260
|
+
if (skipped !== -1) {
|
|
1261
|
+
textContent += value.substring(i, skipped);
|
|
1262
|
+
i = skipped - 1;
|
|
784
1263
|
continue;
|
|
785
1264
|
}
|
|
786
1265
|
}
|
|
@@ -794,7 +1273,6 @@ function getAttributeOffsets(value, span) {
|
|
|
794
1273
|
lengths
|
|
795
1274
|
};
|
|
796
1275
|
}
|
|
797
|
-
|
|
798
1276
|
//#endregion
|
|
799
1277
|
//#region src/compile/build/client/buildComponentNode.ts
|
|
800
1278
|
function buildComponentNode(node, status, b, root = false) {
|
|
@@ -811,51 +1289,31 @@ function buildComponentNode(node, status, b, root = false) {
|
|
|
811
1289
|
name = name.substring(1);
|
|
812
1290
|
props.push({
|
|
813
1291
|
name,
|
|
814
|
-
preText: "",
|
|
815
1292
|
value,
|
|
816
|
-
|
|
817
|
-
spans: [span],
|
|
818
|
-
offsets: [],
|
|
819
|
-
lengths: []
|
|
1293
|
+
spans: [span]
|
|
820
1294
|
});
|
|
821
1295
|
runs.push(`${propsName}["${name}"] = ${value};`);
|
|
822
|
-
bindingRuns.push(
|
|
1296
|
+
bindingRuns.push(bindingTarget(value, `${propsName}["${name}"]`, name, status.imports));
|
|
823
1297
|
} else if (name === "class") {
|
|
824
|
-
status.
|
|
825
|
-
const params = [value];
|
|
826
|
-
if (node.scopeStyles) params.push(`"torp-${status.styleHash}"`);
|
|
827
|
-
value = params.join(", ");
|
|
1298
|
+
if (node.scopeStyles) value = `[${value}, "torp-${status.styleHash}"]`;
|
|
828
1299
|
props.push({
|
|
829
1300
|
name,
|
|
830
|
-
preText: "t_class(",
|
|
831
1301
|
value,
|
|
832
|
-
|
|
833
|
-
spans: [span],
|
|
834
|
-
offsets: [],
|
|
835
|
-
lengths: []
|
|
1302
|
+
spans: [span]
|
|
836
1303
|
});
|
|
837
|
-
runs.push(`${propsName}["${name}"] =
|
|
1304
|
+
runs.push(`${propsName}["${name}"] = ${value};`);
|
|
838
1305
|
} else if (name === "style") {
|
|
839
|
-
status.imports.add("t_style");
|
|
840
1306
|
props.push({
|
|
841
1307
|
name,
|
|
842
|
-
preText: "t_style(",
|
|
843
1308
|
value,
|
|
844
|
-
|
|
845
|
-
spans: [span],
|
|
846
|
-
offsets: [],
|
|
847
|
-
lengths: []
|
|
1309
|
+
spans: [span]
|
|
848
1310
|
});
|
|
849
|
-
runs.push(`${propsName}["${name}"] =
|
|
1311
|
+
runs.push(`${propsName}["${name}"] = ${value};`);
|
|
850
1312
|
} else {
|
|
851
1313
|
props.push({
|
|
852
1314
|
name,
|
|
853
|
-
preText: "",
|
|
854
1315
|
value,
|
|
855
|
-
|
|
856
|
-
spans: [span],
|
|
857
|
-
offsets: [],
|
|
858
|
-
lengths: []
|
|
1316
|
+
spans: [span]
|
|
859
1317
|
});
|
|
860
1318
|
runs.push(`${propsName}["${name}"] = ${value};`);
|
|
861
1319
|
}
|
|
@@ -863,38 +1321,28 @@ function buildComponentNode(node, status, b, root = false) {
|
|
|
863
1321
|
const { newValue, spans, offsets, lengths } = getAttributeOffsets(value, span);
|
|
864
1322
|
value = newValue;
|
|
865
1323
|
if (name === "class") {
|
|
866
|
-
status.
|
|
867
|
-
const params = [value];
|
|
868
|
-
if (node.scopeStyles) params.push(`"torp-${status.styleHash}"`);
|
|
869
|
-
value = params.join(", ");
|
|
1324
|
+
if (node.scopeStyles) value = `[${value}, "torp-${status.styleHash}"]`;
|
|
870
1325
|
props.push({
|
|
871
1326
|
name,
|
|
872
|
-
preText: "t_class(",
|
|
873
1327
|
value,
|
|
874
|
-
postText: ")",
|
|
875
1328
|
spans,
|
|
876
1329
|
offsets,
|
|
877
1330
|
lengths
|
|
878
1331
|
});
|
|
879
|
-
runs.push(`${propsName}["${name}"] =
|
|
1332
|
+
runs.push(`${propsName}["${name}"] = ${value};`);
|
|
880
1333
|
} else if (name === "style") {
|
|
881
|
-
status.imports.add("t_style");
|
|
882
1334
|
props.push({
|
|
883
1335
|
name,
|
|
884
|
-
preText: "t_style(",
|
|
885
1336
|
value,
|
|
886
|
-
postText: ")",
|
|
887
1337
|
spans,
|
|
888
1338
|
offsets,
|
|
889
1339
|
lengths
|
|
890
1340
|
});
|
|
891
|
-
runs.push(`${propsName}["${name}"] =
|
|
1341
|
+
runs.push(`${propsName}["${name}"] = ${value};`);
|
|
892
1342
|
} else {
|
|
893
1343
|
props.push({
|
|
894
1344
|
name,
|
|
895
|
-
preText: "",
|
|
896
1345
|
value,
|
|
897
|
-
postText: "",
|
|
898
1346
|
spans,
|
|
899
1347
|
offsets,
|
|
900
1348
|
lengths
|
|
@@ -905,27 +1353,22 @@ function buildComponentNode(node, status, b, root = false) {
|
|
|
905
1353
|
if (name === "class" && node.scopeStyles) value = `"${trimQuotes(value)} torp-${status.styleHash}"`;
|
|
906
1354
|
props.push({
|
|
907
1355
|
name,
|
|
908
|
-
preText: "",
|
|
909
1356
|
value,
|
|
910
|
-
postText: "",
|
|
911
|
-
spans: [span]
|
|
912
|
-
offsets: [],
|
|
913
|
-
lengths: []
|
|
1357
|
+
postText: " as const",
|
|
1358
|
+
spans: [span]
|
|
914
1359
|
});
|
|
915
1360
|
} else props.push({
|
|
916
1361
|
name,
|
|
917
|
-
preText: "",
|
|
918
1362
|
value: "true",
|
|
919
|
-
|
|
920
|
-
spans: [span],
|
|
921
|
-
offsets: [],
|
|
922
|
-
lengths: []
|
|
1363
|
+
spans: [span]
|
|
923
1364
|
});
|
|
924
|
-
b.append(`
|
|
1365
|
+
b.append(`let ${propsName} = $watch({`);
|
|
925
1366
|
for (let p of props) {
|
|
926
1367
|
let value = replaceForVarNames(p.value, status);
|
|
927
|
-
|
|
928
|
-
|
|
1368
|
+
p.preText ??= "";
|
|
1369
|
+
p.postText ??= "";
|
|
1370
|
+
if (p.spans.length === 1) addMappedText(p.name ? `${p.name}: ` + p.preText : p.preText, value, p.postText + ",", p.spans[0], status, b);
|
|
1371
|
+
else addMappedTextWithOffsets(p.name ? `${p.name}: ` + p.preText : p.preText, value, p.postText + ",", p.spans, p.offsets, p.lengths, status, b);
|
|
929
1372
|
}
|
|
930
1373
|
b.append("});");
|
|
931
1374
|
if (runs.length) buildRun("setProps", replaceForVarNames(runs.join("\n"), status), status, b);
|
|
@@ -947,14 +1390,14 @@ function buildComponentNode(node, status, b, root = false) {
|
|
|
947
1390
|
const slotsName = componentHasSlots ? nextVarName("slots", status) : "undefined";
|
|
948
1391
|
if (componentHasSlots) {
|
|
949
1392
|
b.append(`const ${slotsName}: Record<string, SlotRender> = {};`);
|
|
950
|
-
for (let slot of node.children) if (isSpecialNode(slot) && slot.tagName === "fill") {
|
|
1393
|
+
for (let slot of node.children) if (isSpecialNode(slot) && (slot.tagName === "fill" || slot.tagName === "filldef")) {
|
|
951
1394
|
const nameAttribute = slot.attributes.find((a) => a.name === "name");
|
|
952
1395
|
const slotName = nameAttribute?.value ? trimQuotes(nameAttribute.value) : "_";
|
|
953
1396
|
const slotParams = [
|
|
954
1397
|
"$sparent: ParentNode",
|
|
955
1398
|
"$sanchor: Node | null",
|
|
956
|
-
slot.hasSlotProps ? "$
|
|
957
|
-
"// @ts-ignore\n$context?: Record<PropertyKey, any>"
|
|
1399
|
+
slot.hasSlotProps ? "$slot: Record<PropertyKey, any>" : "_$slot?: Record<PropertyKey, any>",
|
|
1400
|
+
"// @ts-ignore\n// eslint-disable-next-line no-unused-vars\n$context?: Record<PropertyKey, any>"
|
|
958
1401
|
];
|
|
959
1402
|
b.append(`${slot.hasSlotProps ? "// @ts-ignore\n" : ""}${slotsName}["${slotName}"] = (\n${slotParams.join(",\n")}\n) => {`);
|
|
960
1403
|
buildFragment(slot, status, b, "$sparent", "$sanchor");
|
|
@@ -978,7 +1421,172 @@ function buildComponentNode(node, status, b, root = false) {
|
|
|
978
1421
|
addMappedText("", `${componentName}(${renderParams})`, ";", node.span, status, b);
|
|
979
1422
|
b.append("");
|
|
980
1423
|
}
|
|
981
|
-
|
|
1424
|
+
//#endregion
|
|
1425
|
+
//#region src/compile/build/client/buildAwaitNode.ts
|
|
1426
|
+
function buildAwaitNode(node, status, b) {
|
|
1427
|
+
const anchorName = node.varName ?? "null";
|
|
1428
|
+
const parentName = node.parentName || anchorName + ".parentNode";
|
|
1429
|
+
const regionName = nextVarName("await_region", status);
|
|
1430
|
+
const branches = node.children.filter((n) => isControlNode(n));
|
|
1431
|
+
const awaitBranch = branches.find((n) => n.operation === "@await");
|
|
1432
|
+
const withBranch = branches.find((n) => n.operation === "@with");
|
|
1433
|
+
status.imports.add("t_region");
|
|
1434
|
+
status.imports.add("t_run_await");
|
|
1435
|
+
b.append("");
|
|
1436
|
+
b.append("/* @await */");
|
|
1437
|
+
addPushDevBoundary("control", "@await", status, b);
|
|
1438
|
+
b.append(`
|
|
1439
|
+
const ${regionName} = t_region(${status.options.dev === true ? `"await"` : ""});
|
|
1440
|
+
t_run_await(${regionName}, ${anchorName}, (${status.inHead ? "" : "t_before"}) => {`);
|
|
1441
|
+
if (awaitBranch !== void 0 && awaitBranch.children.length > 0) {
|
|
1442
|
+
buildFragment(awaitBranch, status, b, parentName, "t_before");
|
|
1443
|
+
status.fragmentStack.push({
|
|
1444
|
+
fragment: awaitBranch.fragment,
|
|
1445
|
+
path: ""
|
|
1446
|
+
});
|
|
1447
|
+
for (let child of awaitBranch.children) buildNode(child, status, b, parentName, "t_before");
|
|
1448
|
+
status.fragmentStack.pop();
|
|
1449
|
+
buildAddFragment(awaitBranch, status, b, parentName, "t_before");
|
|
1450
|
+
}
|
|
1451
|
+
if (withBranch !== void 0) {
|
|
1452
|
+
b.append(`}, (${status.inHead ? "" : "t_before"}) => {`);
|
|
1453
|
+
if (withBranch.children.length > 0) {
|
|
1454
|
+
buildFragment(withBranch, status, b, parentName, "t_before");
|
|
1455
|
+
status.fragmentStack.push({
|
|
1456
|
+
fragment: withBranch.fragment,
|
|
1457
|
+
path: ""
|
|
1458
|
+
});
|
|
1459
|
+
for (let child of withBranch.children) buildNode(child, status, b, parentName, "t_before");
|
|
1460
|
+
status.fragmentStack.pop();
|
|
1461
|
+
buildAddFragment(withBranch, status, b, parentName, "t_before");
|
|
1462
|
+
}
|
|
1463
|
+
b.append(`}${status.options.dev === true ? `, "runAwait"` : ""});`);
|
|
1464
|
+
} else b.append(`}, null${status.options.dev === true ? `, "runAwait"` : ""});`);
|
|
1465
|
+
addPopDevBoundary(status, b);
|
|
1466
|
+
b.append("");
|
|
1467
|
+
}
|
|
1468
|
+
//#endregion
|
|
1469
|
+
//#region src/compile/utils/hasNestedControl.ts
|
|
1470
|
+
/**
|
|
1471
|
+
* Returns true if any descendant of a `@for` body is a control node that
|
|
1472
|
+
* creates its own region and effects (`@if`, `@for`, `@await`, `@switch`,
|
|
1473
|
+
* `@replace`, `@html`).
|
|
1474
|
+
*
|
|
1475
|
+
* Operations that produce no DOM and no region (`@key`, `@const`, `@console`,
|
|
1476
|
+
* `@debugger`, `@function`) are fine — same category as `@key`. They are
|
|
1477
|
+
* descended through, so a real control statement nested under a `@key` (a
|
|
1478
|
+
* common shape — `@for` bodies almost always lead with `@key = ...`) is still
|
|
1479
|
+
* detected.
|
|
1480
|
+
*
|
|
1481
|
+
* Extracted from `isForBodyNoProxySafe` so the no-proxy specialization, the
|
|
1482
|
+
* leaf-row region-chain specialization (`isForBodyLeafSafe`), and any future
|
|
1483
|
+
* pass that needs the same structural predicate can share one source of
|
|
1484
|
+
* truth.
|
|
1485
|
+
*/
|
|
1486
|
+
function hasNestedControl(children) {
|
|
1487
|
+
for (const child of children) {
|
|
1488
|
+
if (isControlNode(child)) {
|
|
1489
|
+
if (!NON_RENDERING_OPERATIONS.has(child.operation)) return true;
|
|
1490
|
+
if (hasNestedControl(child.children)) return true;
|
|
1491
|
+
continue;
|
|
1492
|
+
}
|
|
1493
|
+
if (isElementLike$1(child)) {
|
|
1494
|
+
if (hasNestedControl(child.children)) return true;
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
return false;
|
|
1498
|
+
}
|
|
1499
|
+
function isElementLike$1(node) {
|
|
1500
|
+
return node.type === "element" || node.type === "component" || node.type === "special";
|
|
1501
|
+
}
|
|
1502
|
+
//#endregion
|
|
1503
|
+
//#region src/compile/utils/isForBodyLeafSafe.ts
|
|
1504
|
+
/**
|
|
1505
|
+
* Returns true if the body of a `@for` loop contains no nested control
|
|
1506
|
+
* statements (`@if`, `@for`, `@await`, `@switch`, `@replace`, `@html`).
|
|
1507
|
+
*
|
|
1508
|
+
* When true, every effect created by the row template lives directly on the
|
|
1509
|
+
* item's own region — never on a descendant region chained off it. The
|
|
1510
|
+
* compiler can then emit a "leaf-row" `createListItem` that skips the
|
|
1511
|
+
* per-item `pushRegion(item)` / `popRegion(oldRegion)` calls: `runListItems`
|
|
1512
|
+
* has already pushed the item onto the active region (via
|
|
1513
|
+
* `pushRegion(item, true)`) before calling `create()`, and a leaf body never
|
|
1514
|
+
* creates descendant regions, so `context.activeRegion` stays put at the item
|
|
1515
|
+
* for the whole callback. Saves two function calls per row plus the
|
|
1516
|
+
* `devContext.onRegionPushed` / `onRegionPopped` invocations, which on the
|
|
1517
|
+
* 10k-row js-framework-bench `runlots` op is 20000 fewer calls per pass.
|
|
1518
|
+
*
|
|
1519
|
+
* Note: every no-proxy-safe body is also leaf-safe (`isForBodyNoProxySafe`
|
|
1520
|
+
* includes `hasNestedControl`), but the converse is not true — a body that
|
|
1521
|
+
* writes to a for-var (disqualifying it from no-proxy) can still be leaf-safe
|
|
1522
|
+
* and earn this specialization.
|
|
1523
|
+
*/
|
|
1524
|
+
function isForBodyLeafSafe(forBodyChildren) {
|
|
1525
|
+
return !hasNestedControl(forBodyChildren);
|
|
1526
|
+
}
|
|
1527
|
+
//#endregion
|
|
1528
|
+
//#region src/compile/utils/isForBodyNoProxySafe.ts
|
|
1529
|
+
/**
|
|
1530
|
+
* Returns true if the body of a `@for` loop never writes to any of its loop
|
|
1531
|
+
* variables (`forVars`) — neither directly (`row = ...`, `row.x = ...`,
|
|
1532
|
+
* `row[k] = ...`) nor via compound assignment (`+=`, `++`, etc.) — AND the
|
|
1533
|
+
* body contains no nested control statements (`@if`, `@for`, `@await`, …).
|
|
1534
|
+
*
|
|
1535
|
+
* When both hold, the compiler can emit a "no-proxy" `@for` specialization
|
|
1536
|
+
* that skips the per-item shallow `$watch` Proxy around the loop variable
|
|
1537
|
+
* bag: reads of `data.<forVar>` are plain property accesses (no proxyGet
|
|
1538
|
+
* trap), and the compiler-emitted `updateListItem` callback re-runs item
|
|
1539
|
+
* effects manually (via `t_rerun_region_effects`) when a forVar's reference
|
|
1540
|
+
* actually changes.
|
|
1541
|
+
*
|
|
1542
|
+
* The "no nested controls" constraint keeps the manual effect re-run sound:
|
|
1543
|
+
* without it, a nested `@if`'s effects would live on a descendant region
|
|
1544
|
+
* chained off the original item, but after keyed reconciliation that chain
|
|
1545
|
+
* is orphaned (the live item is a different object), so the re-run walk
|
|
1546
|
+
* couldn't find them. With no nested controls every row effect is on the
|
|
1547
|
+
* item itself, which is exactly what `t_rerun_region_effects` walks.
|
|
1548
|
+
*
|
|
1549
|
+
* Detection is intentionally conservative: a regex scan over every
|
|
1550
|
+
* expression-like string in the body (text interpolation, attribute values,
|
|
1551
|
+
* event handlers, control statements). Anything ambiguous falls back to the
|
|
1552
|
+
* proxy path. The for-statement itself (`for (let row of ...)`) is NOT
|
|
1553
|
+
* scanned — that's the loop header, not the body — so `for (...; i++)` style
|
|
1554
|
+
* loops with `i++` in the header are still eligible.
|
|
1555
|
+
*/
|
|
1556
|
+
function isForBodyNoProxySafe(forBodyChildren, forVars) {
|
|
1557
|
+
if (forVars.length === 0) return false;
|
|
1558
|
+
if (hasNestedControl(forBodyChildren)) return false;
|
|
1559
|
+
const collected = [];
|
|
1560
|
+
for (const child of forBodyChildren) collectExpressionStrings(child, collected);
|
|
1561
|
+
for (const varName of forVars) {
|
|
1562
|
+
const escaped = varName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1563
|
+
const writePattern = new RegExp(`\\[\\s*${escaped}\\s*\\]\\s*=(?![=>])|\\{\\s*${escaped}\\s*(:[^}]+)?\\}\\s*=(?![=>])|\\b${escaped}(?:\\.[a-zA-Z_$][\\w$]*|\\[[^\\]]+\\])?\\s*(?:=(?![=>])|[+\\-*/%&|^]=|\\+\\+|--)|(?:\\+\\+|--)\\s*${escaped}\\b`);
|
|
1564
|
+
for (const text of collected) if (writePattern.test(text)) return false;
|
|
1565
|
+
}
|
|
1566
|
+
return true;
|
|
1567
|
+
}
|
|
1568
|
+
function isElementLike(node) {
|
|
1569
|
+
return node.type === "element" || node.type === "component" || node.type === "special";
|
|
1570
|
+
}
|
|
1571
|
+
function collectExpressionStrings(node, out) {
|
|
1572
|
+
if (isTextNode(node)) {
|
|
1573
|
+
out.push(node.content);
|
|
1574
|
+
return;
|
|
1575
|
+
}
|
|
1576
|
+
if (isElementLike(node)) {
|
|
1577
|
+
for (const attr of node.attributes) {
|
|
1578
|
+
if (attr.value !== void 0) if (attr.name.startsWith("&")) out.push(`${attr.value} =`);
|
|
1579
|
+
else out.push(attr.value);
|
|
1580
|
+
if (attr.fullyReactive && attr.name !== attr.value) out.push(attr.name);
|
|
1581
|
+
}
|
|
1582
|
+
for (const child of node.children) collectExpressionStrings(child, out);
|
|
1583
|
+
return;
|
|
1584
|
+
}
|
|
1585
|
+
if (isControlNode(node)) {
|
|
1586
|
+
out.push(node.statement);
|
|
1587
|
+
for (const child of node.children) collectExpressionStrings(child, out);
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
982
1590
|
//#endregion
|
|
983
1591
|
//#region src/compile/utils/trimMatched.ts
|
|
984
1592
|
/**
|
|
@@ -997,102 +1605,6 @@ function trimMatched(text, startChar, endChar) {
|
|
|
997
1605
|
}
|
|
998
1606
|
return text.substring(start, end);
|
|
999
1607
|
}
|
|
1000
|
-
|
|
1001
|
-
//#endregion
|
|
1002
|
-
//#region src/compile/build/client/buildAwaitNode.ts
|
|
1003
|
-
function buildAwaitNode(node, status, b) {
|
|
1004
|
-
const anchorName = node.varName ?? "null";
|
|
1005
|
-
const parentName = node.parentName;
|
|
1006
|
-
const regionName = nextVarName("await_region", status);
|
|
1007
|
-
const indexName = nextVarName("await_index", status);
|
|
1008
|
-
const tokenName = nextVarName("await_token", status);
|
|
1009
|
-
const branches = node.children.filter((n) => isControlNode(n));
|
|
1010
|
-
let awaitBranch = branches.find((n) => n.operation === "@await");
|
|
1011
|
-
if (!awaitBranch) {}
|
|
1012
|
-
let thenBranch = branches.find((n) => n.operation === "@then");
|
|
1013
|
-
if (!thenBranch) thenBranch = {
|
|
1014
|
-
type: "control",
|
|
1015
|
-
operation: "@then",
|
|
1016
|
-
statement: "then",
|
|
1017
|
-
children: [],
|
|
1018
|
-
span: {
|
|
1019
|
-
start: 0,
|
|
1020
|
-
end: 0
|
|
1021
|
-
}
|
|
1022
|
-
};
|
|
1023
|
-
let catchBranch = branches.find((n) => n.operation === "@catch");
|
|
1024
|
-
if (!catchBranch) catchBranch = {
|
|
1025
|
-
type: "control",
|
|
1026
|
-
operation: "@catch",
|
|
1027
|
-
statement: "catch",
|
|
1028
|
-
children: [],
|
|
1029
|
-
span: {
|
|
1030
|
-
start: 0,
|
|
1031
|
-
end: 0
|
|
1032
|
-
}
|
|
1033
|
-
};
|
|
1034
|
-
const awaiterName = trimMatched(awaitBranch.statement.substring(5).trim(), "(", ")");
|
|
1035
|
-
const thenVar = trimMatched(thenBranch.statement.substring(4).trim(), "(", ")");
|
|
1036
|
-
const catchVar = trimMatched(catchBranch.statement.substring(5).trim(), "(", ")");
|
|
1037
|
-
status.imports.add("t_region");
|
|
1038
|
-
status.imports.add("t_run_control");
|
|
1039
|
-
status.imports.add("t_run_branch");
|
|
1040
|
-
status.imports.add("t_push_region");
|
|
1041
|
-
status.imports.add("t_pop_region");
|
|
1042
|
-
b.append("");
|
|
1043
|
-
b.append("/* @await */");
|
|
1044
|
-
addPushDevBoundary("control", `@${branches[0].statement}`, status, b);
|
|
1045
|
-
b.append(`
|
|
1046
|
-
const ${regionName} = t_region(${status.options.dev === true ? `"await"` : ""});
|
|
1047
|
-
let ${tokenName} = 0;
|
|
1048
|
-
let ${indexName} = -1;
|
|
1049
|
-
t_run_control(${regionName}, ${anchorName}, (t_before) => {`);
|
|
1050
|
-
let index = 0;
|
|
1051
|
-
buildAwaitBranch(awaitBranch, status, b, parentName, regionName, indexName, index++);
|
|
1052
|
-
b.append(`
|
|
1053
|
-
${tokenName}++;
|
|
1054
|
-
((t_token) => {`);
|
|
1055
|
-
awaitBranch.span.start += 7;
|
|
1056
|
-
addMappedText("", replaceForVarNames(awaiterName, status), "", awaitBranch.span, status, b);
|
|
1057
|
-
thenBranch.span.start -= 1;
|
|
1058
|
-
addMappedText(".then((", replaceForVarNames(thenVar, status), ") => {", thenBranch.span, status, b);
|
|
1059
|
-
b.append(`if (t_token === ${tokenName}) {`);
|
|
1060
|
-
buildAwaitBranch(thenBranch, status, b, parentName, regionName, indexName, index++);
|
|
1061
|
-
b.append(`}
|
|
1062
|
-
})`);
|
|
1063
|
-
catchBranch.span.start -= 1;
|
|
1064
|
-
addMappedText(".catch((", replaceForVarNames(catchVar, status), ") => {", catchBranch.span, status, b);
|
|
1065
|
-
b.append(`if (t_token === ${tokenName}) {`);
|
|
1066
|
-
buildAwaitBranch(catchBranch, status, b, parentName, regionName, indexName, index++);
|
|
1067
|
-
b.append(`}
|
|
1068
|
-
});
|
|
1069
|
-
})(${tokenName});
|
|
1070
|
-
}${status.options.dev === true ? `, "runAwait"` : ""});`);
|
|
1071
|
-
addPopDevBoundary(status, b);
|
|
1072
|
-
b.append("");
|
|
1073
|
-
}
|
|
1074
|
-
function buildAwaitBranch(node, status, b, parentName, regionName, indexName, index) {
|
|
1075
|
-
b.append(`if (!t_run_branch(${regionName}, ${indexName}, ${index})) return;`);
|
|
1076
|
-
if (node.children.length > 0) {
|
|
1077
|
-
b.append(`
|
|
1078
|
-
const t_new_region = t_region(${status.options.dev === true ? `"await_branch"` : ""});
|
|
1079
|
-
const t_old_control_region = t_push_region(${regionName});
|
|
1080
|
-
const t_old_region = t_push_region(t_new_region, true);
|
|
1081
|
-
`);
|
|
1082
|
-
buildFragment(node, status, b, parentName, "t_before");
|
|
1083
|
-
status.fragmentStack.push({
|
|
1084
|
-
fragment: node.fragment,
|
|
1085
|
-
path: ""
|
|
1086
|
-
});
|
|
1087
|
-
for (let child of node.children) buildNode(child, status, b, parentName, "t_before");
|
|
1088
|
-
status.fragmentStack.pop();
|
|
1089
|
-
buildAddFragment(node, status, b, parentName, "t_before");
|
|
1090
|
-
b.append("t_pop_region(t_old_region);");
|
|
1091
|
-
b.append("t_pop_region(t_old_control_region);");
|
|
1092
|
-
}
|
|
1093
|
-
b.append(`${indexName} = ${index};`);
|
|
1094
|
-
}
|
|
1095
|
-
|
|
1096
1608
|
//#endregion
|
|
1097
1609
|
//#region src/compile/build/client/buildForNode.ts
|
|
1098
1610
|
const forLoopRegex = /for\s*\((.+?);.*?;.*?\)/;
|
|
@@ -1117,9 +1629,6 @@ function buildForNode(node, status, b) {
|
|
|
1117
1629
|
}
|
|
1118
1630
|
const regionName = nextVarName("for_region", status);
|
|
1119
1631
|
const listItemsName = nextVarName("new_items", status);
|
|
1120
|
-
const previousItemName = nextVarName("previous_item", status);
|
|
1121
|
-
const nextItemName = nextVarName("next_item", status);
|
|
1122
|
-
const newItemName = nextVarName("new_item", status);
|
|
1123
1632
|
const itemName = nextVarName("item", status);
|
|
1124
1633
|
const beforeName = nextVarName("before", status);
|
|
1125
1634
|
let keyStatement = "";
|
|
@@ -1131,10 +1640,13 @@ function buildForNode(node, status, b) {
|
|
|
1131
1640
|
keyStatement = matches[2];
|
|
1132
1641
|
}
|
|
1133
1642
|
}
|
|
1643
|
+
const noWatch = isForBodyNoProxySafe(node.children, forVarNames);
|
|
1644
|
+
if (noWatch) status.imports.add("t_rerun_region_effects");
|
|
1645
|
+
const singleVar = noWatch && forVarNames.length === 1;
|
|
1646
|
+
const leafRow = isForBodyLeafSafe(node.children);
|
|
1134
1647
|
status.imports.add("t_region");
|
|
1135
1648
|
status.imports.add("t_run_list");
|
|
1136
|
-
status.imports.add("
|
|
1137
|
-
status.imports.add("ListItem");
|
|
1649
|
+
status.imports.add("ListItemSpec");
|
|
1138
1650
|
b.append("");
|
|
1139
1651
|
b.append("/* @for */");
|
|
1140
1652
|
addPushDevBoundary("control", `@${node.statement}`, status, b);
|
|
@@ -1145,41 +1657,51 @@ function buildForNode(node, status, b) {
|
|
|
1145
1657
|
${parentName},
|
|
1146
1658
|
${anchorName},
|
|
1147
1659
|
${status.options.dev === true ? "function createNewItems() {" : "() => {"}
|
|
1148
|
-
let ${listItemsName}:
|
|
1149
|
-
let ${previousItemName} = ${regionName};
|
|
1150
|
-
let ${nextItemName} = ${regionName}.nextRegion;`);
|
|
1660
|
+
let ${listItemsName}: ListItemSpec[] = [];`);
|
|
1151
1661
|
addMappedText("", `${replaceForVarNames(node.statement, status)}`, " {", node.span, status, b);
|
|
1152
|
-
b.append(
|
|
1153
|
-
b.append(
|
|
1154
|
-
if (key !== void 0) addMappedText("", `${keyStatement || "undefined"}`, "
|
|
1155
|
-
|
|
1156
|
-
b.append(
|
|
1157
|
-
b.append(`
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
${previousItemName} = ${newItemName};
|
|
1161
|
-
${listItemsName}.push(${newItemName});
|
|
1162
|
-
}
|
|
1163
|
-
${regionName}.nextRegion = ${nextItemName};
|
|
1164
|
-
return ${listItemsName};
|
|
1165
|
-
},
|
|
1166
|
-
${status.options.dev === true ? `function createListItem(${itemName}, ${beforeName}) {` : `(${itemName}, ${beforeName}) => {`}`);
|
|
1662
|
+
if (singleVar) b.append(`${listItemsName}.push({ data: ${forVarNames[0]}, key: `);
|
|
1663
|
+
else b.append(`${listItemsName}.push({ data: { ${forVarNames.join(", ")} }, key: `);
|
|
1664
|
+
if (key !== void 0) addMappedText("", `${keyStatement || "undefined"}`, " });", key.span, status, b);
|
|
1665
|
+
else b.append(`undefined });`);
|
|
1666
|
+
b.append(`}`);
|
|
1667
|
+
b.append(`return ${listItemsName};`);
|
|
1668
|
+
b.append(`},
|
|
1669
|
+
${status.options.dev === true ? `function createListItem(${itemName}, ${beforeName}) {` : `(${itemName}, ${beforeName}) => {`}`);
|
|
1167
1670
|
let oldForVarNames = status.forVarNames;
|
|
1168
|
-
|
|
1169
|
-
|
|
1671
|
+
const forVarBitBase = oldForVarNames.length;
|
|
1672
|
+
status.forVarNames = [...status.forVarNames, ...singleVar ? forVarNames.map((v) => [v, `${itemName}.data`]) : forVarNames.map((v) => [v, `${itemName}.data.${v}`])];
|
|
1673
|
+
buildForItem(node, status, b, parentName, beforeName, itemName, leafRow);
|
|
1170
1674
|
status.forVarNames = oldForVarNames;
|
|
1171
1675
|
b.append(`},
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
)
|
|
1676
|
+
${status.options.dev === true ? "function updateListItem(t_old_item, t_new_item) {" : "(t_old_item, t_new_item) => {"}`);
|
|
1677
|
+
if (noWatch) {
|
|
1678
|
+
b.append(`let t_changed_mask = 0;`);
|
|
1679
|
+
if (singleVar) {
|
|
1680
|
+
const bit = 1 << forVarBitBase;
|
|
1681
|
+
b.append(`if (t_old_item.data !== t_new_item.data) {
|
|
1682
|
+
t_old_item.data = t_new_item.data;
|
|
1683
|
+
t_changed_mask = ${bit};
|
|
1684
|
+
}`);
|
|
1685
|
+
} else for (let i = 0; i < forVarNames.length; i++) {
|
|
1686
|
+
const varName = forVarNames[i];
|
|
1687
|
+
const bit = 1 << forVarBitBase + i;
|
|
1688
|
+
b.append(`if (t_old_item.data.${varName} !== t_new_item.data.${varName}) {
|
|
1689
|
+
t_old_item.data.${varName} = t_new_item.data.${varName};
|
|
1690
|
+
t_changed_mask |= ${bit};
|
|
1691
|
+
}`);
|
|
1692
|
+
}
|
|
1693
|
+
b.append(`if (t_changed_mask) t_rerun_region_effects(t_old_item, t_changed_mask);`);
|
|
1694
|
+
} else for (let varName of forVarNames) b.append(`t_old_item.data.${varName} = t_new_item.data.${varName};`);
|
|
1695
|
+
b.append(`}${noWatch ? ",\ntrue" : ""}\n);`);
|
|
1176
1696
|
addPopDevBoundary(status, b);
|
|
1177
1697
|
b.append("");
|
|
1178
1698
|
}
|
|
1179
|
-
function buildForItem(node, status, b, parentName, beforeName, itemName) {
|
|
1699
|
+
function buildForItem(node, status, b, parentName, beforeName, itemName, leafRow) {
|
|
1180
1700
|
const oldRegionName = nextVarName("old_region", status);
|
|
1181
|
-
|
|
1182
|
-
|
|
1701
|
+
if (!leafRow) {
|
|
1702
|
+
status.imports.add("t_push_region");
|
|
1703
|
+
b.append(`let ${oldRegionName} = t_push_region(${itemName});`);
|
|
1704
|
+
}
|
|
1183
1705
|
buildFragment(node, status, b, parentName, beforeName);
|
|
1184
1706
|
status.fragmentStack.push({
|
|
1185
1707
|
fragment: node.fragment,
|
|
@@ -1191,39 +1713,37 @@ function buildForItem(node, status, b, parentName, beforeName, itemName) {
|
|
|
1191
1713
|
}
|
|
1192
1714
|
status.fragmentStack.pop();
|
|
1193
1715
|
buildAddFragment(node, status, b, parentName, beforeName);
|
|
1194
|
-
|
|
1195
|
-
|
|
1716
|
+
if (!leafRow) {
|
|
1717
|
+
status.imports.add("t_pop_region");
|
|
1718
|
+
b.append(`t_pop_region(${oldRegionName});`);
|
|
1719
|
+
}
|
|
1196
1720
|
}
|
|
1197
|
-
|
|
1198
1721
|
//#endregion
|
|
1199
1722
|
//#region src/compile/build/client/buildHtmlNode.ts
|
|
1200
1723
|
function buildHtmlNode(node, status, b) {
|
|
1201
1724
|
const htmlAnchorName = node.varName;
|
|
1202
1725
|
const htmlParentName = node.parentName || htmlAnchorName + ".parentNode";
|
|
1203
|
-
const htmlRegionName = nextVarName("html_region", status);
|
|
1204
1726
|
node = node.children[0];
|
|
1205
1727
|
status.imports.add("t_region");
|
|
1206
1728
|
status.imports.add("t_run_control");
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
status.imports.add("t_pop_region");
|
|
1729
|
+
const firstNodeVar = nextVarName("html_first", status);
|
|
1730
|
+
const lastNodeVar = nextVarName("html_last", status);
|
|
1210
1731
|
b.append("");
|
|
1211
|
-
b.append(
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
t_run_control(
|
|
1215
|
-
buildHtmlBranch(node, status, b, htmlParentName, htmlRegionName);
|
|
1216
|
-
b.append(`}${status.options.dev === true ? ", \"html\"" : ""});`);
|
|
1217
|
-
b.append("");
|
|
1218
|
-
}
|
|
1219
|
-
function buildHtmlBranch(node, status, b, parentName, regionName) {
|
|
1220
|
-
status.imports.add("t_run_branch");
|
|
1732
|
+
b.append(`/* @html */`);
|
|
1733
|
+
b.append(`let ${firstNodeVar}: ChildNode | null = null;`);
|
|
1734
|
+
b.append(`let ${lastNodeVar}: ChildNode | null = null;`);
|
|
1735
|
+
b.append(`t_run_control(t_region(), ${htmlAnchorName}, (t_before) => {`);
|
|
1221
1736
|
b.append(`${replaceForVarNames(node.statement, status)};`);
|
|
1222
|
-
b.append(`if (
|
|
1223
|
-
b.append(`
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
`);
|
|
1737
|
+
b.append(`if (${firstNodeVar} !== null && ${lastNodeVar} !== null) {`);
|
|
1738
|
+
b.append(`let t_node: ChildNode | null = ${lastNodeVar};`);
|
|
1739
|
+
b.append(`while (t_node !== null && t_node !== ${firstNodeVar}) {`);
|
|
1740
|
+
b.append(`const t_prev: ChildNode | null = t_node.previousSibling;`);
|
|
1741
|
+
b.append(`t_node.remove();`);
|
|
1742
|
+
b.append(`t_node = t_prev;`);
|
|
1743
|
+
b.append(`}`);
|
|
1744
|
+
b.append(`if (${firstNodeVar}) ${firstNodeVar}.remove();`);
|
|
1745
|
+
b.append(`${firstNodeVar} = ${lastNodeVar} = null;`);
|
|
1746
|
+
b.append(`}`);
|
|
1227
1747
|
const templateName = nextVarName("template", status);
|
|
1228
1748
|
const fragmentName = `t_fragment_${node.fragment.number}`;
|
|
1229
1749
|
b.append(`let ${templateName} = document.createElement("template");`);
|
|
@@ -1231,10 +1751,23 @@ function buildHtmlBranch(node, status, b, parentName, regionName) {
|
|
|
1231
1751
|
node.span.end -= 2;
|
|
1232
1752
|
addMappedText("", `${templateName}.innerHTML = ${replaceForVarNames(node.statement, status)};`, "", node.span, status, b);
|
|
1233
1753
|
b.append(`let ${fragmentName} = ${templateName}.content.cloneNode(true) as DocumentFragment;`);
|
|
1234
|
-
|
|
1235
|
-
b.append(
|
|
1754
|
+
b.append(`${firstNodeVar} = ${fragmentName}.firstChild;`);
|
|
1755
|
+
b.append(`${lastNodeVar} = ${fragmentName}.lastChild;`);
|
|
1756
|
+
buildAddFragment(node, status, b, htmlParentName, "t_before");
|
|
1757
|
+
b.append(`if (${firstNodeVar} !== null && ${firstNodeVar}.parentNode !== ${htmlParentName}) {`);
|
|
1758
|
+
b.append(`${lastNodeVar} = ${htmlAnchorName}.previousSibling as ChildNode | null;`);
|
|
1759
|
+
b.append(`if (${lastNodeVar} !== null) {`);
|
|
1760
|
+
b.append(`${firstNodeVar} = ${lastNodeVar};`);
|
|
1761
|
+
b.append(`let t_scan: ChildNode | null = ${lastNodeVar};`);
|
|
1762
|
+
b.append(`while (t_scan !== null && t_scan.previousSibling !== null && t_scan.previousSibling !== ${htmlAnchorName} && (t_scan.previousSibling.nodeType !== 3 || (t_scan.previousSibling.textContent ?? "").trim() !== "")) {`);
|
|
1763
|
+
b.append(`t_scan = t_scan.previousSibling;`);
|
|
1764
|
+
b.append(`}`);
|
|
1765
|
+
b.append(`${firstNodeVar} = t_scan;`);
|
|
1766
|
+
b.append(`}`);
|
|
1767
|
+
b.append(`}`);
|
|
1768
|
+
b.append(`}${status.options.dev === true ? ", \"html\"" : ""});`);
|
|
1769
|
+
b.append("");
|
|
1236
1770
|
}
|
|
1237
|
-
|
|
1238
1771
|
//#endregion
|
|
1239
1772
|
//#region src/compile/build/client/buildIfNode.ts
|
|
1240
1773
|
function buildIfNode(node, status, b) {
|
|
@@ -1292,7 +1825,6 @@ function buildIfBranch(node, status, b, parentName, regionName, indexName, index
|
|
|
1292
1825
|
b.append(`${indexName} = ${index};`);
|
|
1293
1826
|
b.append("}");
|
|
1294
1827
|
}
|
|
1295
|
-
|
|
1296
1828
|
//#endregion
|
|
1297
1829
|
//#region src/compile/build/client/buildReplaceNode.ts
|
|
1298
1830
|
function buildReplaceNode(node, status, b) {
|
|
@@ -1332,7 +1864,6 @@ function buildReplaceBranch(node, status, b, parentName, regionName) {
|
|
|
1332
1864
|
buildAddFragment(node, status, b, parentName, "t_before");
|
|
1333
1865
|
b.append("t_pop_region(t_old_region);");
|
|
1334
1866
|
}
|
|
1335
|
-
|
|
1336
1867
|
//#endregion
|
|
1337
1868
|
//#region src/compile/build/client/buildScriptNode.ts
|
|
1338
1869
|
function buildScriptNode(node, status, b) {
|
|
@@ -1343,7 +1874,6 @@ function maybeAppend$1(text, end) {
|
|
|
1343
1874
|
if (!text.endsWith(end)) text += end;
|
|
1344
1875
|
return text;
|
|
1345
1876
|
}
|
|
1346
|
-
|
|
1347
1877
|
//#endregion
|
|
1348
1878
|
//#region src/compile/build/client/buildSwitchNode.ts
|
|
1349
1879
|
function buildSwitchNode(node, status, b) {
|
|
@@ -1355,7 +1885,7 @@ function buildSwitchNode(node, status, b) {
|
|
|
1355
1885
|
if (branches.findIndex((n) => n.operation === "@default") === -1) branches.push({
|
|
1356
1886
|
type: "control",
|
|
1357
1887
|
operation: "@default",
|
|
1358
|
-
statement: "default",
|
|
1888
|
+
statement: "default:",
|
|
1359
1889
|
children: [],
|
|
1360
1890
|
span: {
|
|
1361
1891
|
start: 0,
|
|
@@ -1406,7 +1936,45 @@ function buildSwitchBranch(node, status, b, parentName, regionName, indexName, i
|
|
|
1406
1936
|
break;
|
|
1407
1937
|
}`);
|
|
1408
1938
|
}
|
|
1409
|
-
|
|
1939
|
+
//#endregion
|
|
1940
|
+
//#region src/compile/build/client/buildTryNode.ts
|
|
1941
|
+
function buildTryNode(node, status, b) {
|
|
1942
|
+
const anchorName = node.varName ?? "null";
|
|
1943
|
+
const parentName = node.parentName || anchorName + ".parentNode";
|
|
1944
|
+
const regionName = nextVarName("try_region", status);
|
|
1945
|
+
const branches = node.children.filter((n) => isControlNode(n));
|
|
1946
|
+
const tryBranch = branches.find((n) => n.operation === "@try");
|
|
1947
|
+
const catchBranch = branches.find((n) => n.operation === "@catch");
|
|
1948
|
+
status.imports.add("t_region");
|
|
1949
|
+
status.imports.add("t_run_try");
|
|
1950
|
+
b.append("");
|
|
1951
|
+
b.append("/* @try */");
|
|
1952
|
+
addPushDevBoundary("control", "@try", status, b);
|
|
1953
|
+
b.append(`
|
|
1954
|
+
const ${regionName} = t_region(${status.options.dev === true ? `"try"` : ""});
|
|
1955
|
+
t_run_try(${regionName}, ${anchorName}, (${status.inHead ? "" : "t_before"}) => {`);
|
|
1956
|
+
buildTryBranchBody(tryBranch, status, b, parentName);
|
|
1957
|
+
const catchVar = catchBranch ? trimMatched(catchBranch.statement.substring(5).trim(), "(", ")") || "err" : null;
|
|
1958
|
+
if (catchBranch && catchVar) {
|
|
1959
|
+
addMappedText("},", `(${status.inHead ? "" : "t_before"}, ${replaceForVarNames(catchVar, status)}) => {`, "", catchBranch.span, status, b);
|
|
1960
|
+
buildTryBranchBody(catchBranch, status, b, parentName);
|
|
1961
|
+
b.append(`}${status.options.dev === true ? `, "runTry"` : ""});`);
|
|
1962
|
+
} else b.append(`}, null${status.options.dev === true ? `, "runTry"` : ""});`);
|
|
1963
|
+
addPopDevBoundary(status, b);
|
|
1964
|
+
b.append("");
|
|
1965
|
+
}
|
|
1966
|
+
function buildTryBranchBody(node, status, b, parentName) {
|
|
1967
|
+
if (node.children.length > 0) {
|
|
1968
|
+
buildFragment(node, status, b, parentName, "t_before");
|
|
1969
|
+
status.fragmentStack.push({
|
|
1970
|
+
fragment: node.fragment,
|
|
1971
|
+
path: ""
|
|
1972
|
+
});
|
|
1973
|
+
for (let child of node.children) buildNode(child, status, b, parentName, "t_before");
|
|
1974
|
+
status.fragmentStack.pop();
|
|
1975
|
+
buildAddFragment(node, status, b, parentName, "t_before");
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1410
1978
|
//#endregion
|
|
1411
1979
|
//#region src/compile/build/client/buildControlNode.ts
|
|
1412
1980
|
function buildControlNode(node, status, b) {
|
|
@@ -1431,8 +1999,12 @@ function buildControlNode(node, status, b) {
|
|
|
1431
1999
|
buildAwaitNode(node, status, b);
|
|
1432
2000
|
break;
|
|
1433
2001
|
case "@await":
|
|
1434
|
-
case "@
|
|
2002
|
+
case "@with":
|
|
2003
|
+
case "@try":
|
|
1435
2004
|
case "@catch": break;
|
|
2005
|
+
case "@try group":
|
|
2006
|
+
buildTryNode(node, status, b);
|
|
2007
|
+
break;
|
|
1436
2008
|
case "@replace group":
|
|
1437
2009
|
buildReplaceNode(node, status, b);
|
|
1438
2010
|
break;
|
|
@@ -1455,7 +2027,6 @@ function buildControlNode(node, status, b) {
|
|
|
1455
2027
|
default: throw new Error(`Invalid operation: ${node.operation}`);
|
|
1456
2028
|
}
|
|
1457
2029
|
}
|
|
1458
|
-
|
|
1459
2030
|
//#endregion
|
|
1460
2031
|
//#region src/compile/utils/trimEnd.ts
|
|
1461
2032
|
/**
|
|
@@ -1471,30 +2042,29 @@ function trimEnd(text, char) {
|
|
|
1471
2042
|
while (text[end - 1] === char) end -= 1;
|
|
1472
2043
|
return text.substring(0, end);
|
|
1473
2044
|
}
|
|
1474
|
-
|
|
1475
2045
|
//#endregion
|
|
1476
2046
|
//#region src/compile/build/client/buildMount.ts
|
|
1477
2047
|
function buildMount(_functionName, functionBody, status, b) {
|
|
1478
2048
|
functionBody = replaceForVarNames(functionBody, status);
|
|
1479
|
-
status.imports.add("$
|
|
1480
|
-
b.append("// @ts-ignore\n$
|
|
2049
|
+
status.imports.add("$onmount");
|
|
2050
|
+
b.append("// @ts-ignore\n$onmount(() => {");
|
|
1481
2051
|
b.append(functionBody);
|
|
1482
2052
|
b.append("});");
|
|
1483
2053
|
}
|
|
1484
|
-
|
|
1485
2054
|
//#endregion
|
|
1486
2055
|
//#region src/compile/build/client/stashRun.ts
|
|
1487
2056
|
function stashRun(fragment, functionStart, value, functionEnd, span, status) {
|
|
1488
2057
|
value = replaceForVarNames(value, status);
|
|
1489
2058
|
let functionBody = functionStart + value + functionEnd;
|
|
2059
|
+
let forVarMask = status.forVarNames.length > 0 ? forVarsReadIn(value, status.forVarNames) : void 0;
|
|
1490
2060
|
fragment.effects.push({
|
|
1491
2061
|
functionBody,
|
|
1492
2062
|
spans: [span],
|
|
1493
2063
|
offsets: [functionStart.length],
|
|
1494
|
-
lengths: [value.length]
|
|
2064
|
+
lengths: [value.length],
|
|
2065
|
+
forVarMask
|
|
1495
2066
|
});
|
|
1496
2067
|
}
|
|
1497
|
-
|
|
1498
2068
|
//#endregion
|
|
1499
2069
|
//#region src/compile/build/client/stashRunWithOffsets.ts
|
|
1500
2070
|
function stashRunWithOffsets(fragment, functionStart, value, functionEnd, spans, offsets, lengths, status) {
|
|
@@ -1507,17 +2077,18 @@ function stashRunWithOffsets(fragment, functionStart, value, functionEnd, spans,
|
|
|
1507
2077
|
lengths[i] += changedLength;
|
|
1508
2078
|
for (let j = i + 1; j < offsets.length; j++) offsets[j] += changedLength;
|
|
1509
2079
|
}
|
|
2080
|
+
let forVarMask = status.forVarNames.length > 0 ? forVarsReadIn(functionBody, status.forVarNames) : void 0;
|
|
1510
2081
|
fragment.effects.push({
|
|
1511
2082
|
functionBody,
|
|
1512
2083
|
spans,
|
|
1513
2084
|
offsets,
|
|
1514
|
-
lengths
|
|
2085
|
+
lengths,
|
|
2086
|
+
forVarMask
|
|
1515
2087
|
});
|
|
1516
2088
|
}
|
|
1517
|
-
|
|
1518
2089
|
//#endregion
|
|
1519
2090
|
//#region src/compile/build/client/buildElementNode.ts
|
|
1520
|
-
function buildElementNode(node, status, b, parentName
|
|
2091
|
+
function buildElementNode(node, status, b, parentName) {
|
|
1521
2092
|
if (status.inHead) {
|
|
1522
2093
|
if (node.tagName === "title") buildTitleNode(node, status, b);
|
|
1523
2094
|
else buildHeadNode(node, status, b);
|
|
@@ -1528,8 +2099,10 @@ function buildElementNode(node, status, b, parentName, root = false) {
|
|
|
1528
2099
|
if (svgElement) status.ns = true;
|
|
1529
2100
|
const varName = node.varName;
|
|
1530
2101
|
if (varName) {
|
|
1531
|
-
if (node.tagName === "@element")
|
|
1532
|
-
|
|
2102
|
+
if (node.tagName === "@element") {
|
|
2103
|
+
buildDynamicElementNode(node, status, b);
|
|
2104
|
+
return;
|
|
2105
|
+
}
|
|
1533
2106
|
buildElementAttributes$1(node, varName, status, b);
|
|
1534
2107
|
}
|
|
1535
2108
|
const oldPreserveWhitespace = status.preserveWhitespace;
|
|
@@ -1540,23 +2113,18 @@ function buildElementNode(node, status, b, parentName, root = false) {
|
|
|
1540
2113
|
}
|
|
1541
2114
|
function buildDynamicElementNode(node, status, b) {
|
|
1542
2115
|
let selfAttribute = node.attributes.find((a) => a.name === "self");
|
|
1543
|
-
if (selfAttribute && selfAttribute.value
|
|
1544
|
-
status.imports.add("$run");
|
|
2116
|
+
if (selfAttribute && selfAttribute.value) {
|
|
1545
2117
|
status.imports.add("t_dynamic");
|
|
1546
2118
|
let selfValue = selfAttribute.value;
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
path: ""
|
|
1554
|
-
});
|
|
1555
|
-
for (let child of node.children) buildNode(child, status, b, parentName, "null");
|
|
1556
|
-
status.fragmentStack.pop();
|
|
1557
|
-
buildAddFragment(node, status, b, parentName, "null");
|
|
1558
|
-
b.append(`}${status.options.dev === true ? `, "setDynamic"` : ""});`);
|
|
2119
|
+
if (selfAttribute.fullyReactive) {
|
|
2120
|
+
status.imports.add("$run");
|
|
2121
|
+
b.append("$run(() => {");
|
|
2122
|
+
b.append(`${node.varName} = t_dynamic(${node.varName}, ${selfValue});`);
|
|
2123
|
+
b.append(`}${status.options.dev === true ? `, "setDynamic"` : ""});`);
|
|
2124
|
+
} else b.append(`${node.varName} = t_dynamic(${node.varName}, ${selfValue});`);
|
|
1559
2125
|
}
|
|
2126
|
+
buildElementAttributes$1(node, node.varName, status, b);
|
|
2127
|
+
for (let child of node.children) buildNode(child, status, b, node.varName, "null");
|
|
1560
2128
|
}
|
|
1561
2129
|
function buildTitleNode(node, status, b) {
|
|
1562
2130
|
let content = node.children[0].content || "";
|
|
@@ -1599,11 +2167,6 @@ function buildHeadNode(node, status, b) {
|
|
|
1599
2167
|
function buildElementAttributes$1(node, varName, status, b) {
|
|
1600
2168
|
let fragment = status.fragmentStack.at(-1)?.fragment;
|
|
1601
2169
|
if (!fragment) throw new Error("No fragment on stack");
|
|
1602
|
-
const refAttribute = node.attributes.find((a) => a.name === "&ref");
|
|
1603
|
-
if (refAttribute) {
|
|
1604
|
-
let { value, fullyReactive } = refAttribute;
|
|
1605
|
-
if (value != null && fullyReactive) b.append(`${value} = ${varName};`);
|
|
1606
|
-
}
|
|
1607
2170
|
for (let { name, value, reactive, fullyReactive, span } of node.attributes) if (name === "self" && node.tagName === "@element") {} else if (name === "&ref") {} else if (value != null && fullyReactive) if (name === "&group") buildBindGroupAttribute(node, varName, value, status, b);
|
|
1608
2171
|
else if (name === "&value" || name === "&checked") buildBindAttribute(node, varName, name, value, status, b);
|
|
1609
2172
|
else if (name === "onmount") buildMount("elMount", `return (${trimEnd(value.trim(), ";")})(${varName});`, status, b);
|
|
@@ -1616,9 +2179,8 @@ function buildElementAttributes$1(node, varName, status, b) {
|
|
|
1616
2179
|
const propName = status.ns ? "className.baseVal" : "className";
|
|
1617
2180
|
stashRun(fragment, `${status.ns ? "// @ts-ignore\n" : ""}${varName}.${propName} = t_class(`, params.join(", "), ");", span, status);
|
|
1618
2181
|
} else if (name === "style") {
|
|
1619
|
-
status.imports.add("t_attribute");
|
|
1620
2182
|
status.imports.add("t_style");
|
|
1621
|
-
stashRun(fragment,
|
|
2183
|
+
stashRun(fragment, `${varName}.style.cssText += t_style(`, value, ");", span, status);
|
|
1622
2184
|
} else if (name.includes("-")) {
|
|
1623
2185
|
status.imports.add("t_attribute");
|
|
1624
2186
|
stashRun(fragment, `t_attribute(${varName}, "${name}", `, value, ");", span, status);
|
|
@@ -1637,7 +2199,7 @@ function buildElementAttributes$1(node, varName, status, b) {
|
|
|
1637
2199
|
stashRunWithOffsets(fragment, `${status.ns ? "// @ts-ignore\n" : ""}${varName}.${propName} = t_class(`, params.join(", "), ");", spans, offsets, lengths, status);
|
|
1638
2200
|
} else if (name === "style") {
|
|
1639
2201
|
status.imports.add("t_style");
|
|
1640
|
-
stashRunWithOffsets(fragment, `${varName}.
|
|
2202
|
+
stashRunWithOffsets(fragment, `${varName}.style.cssText += t_style(`, value, ");", spans, offsets, lengths, status);
|
|
1641
2203
|
} else if (name.includes("-")) {
|
|
1642
2204
|
status.imports.add("t_attribute");
|
|
1643
2205
|
stashRunWithOffsets(fragment, `t_attribute(${varName}, "${name}", `, value, ");", spans, offsets, lengths, status);
|
|
@@ -1680,9 +2242,18 @@ function buildBindAttribute(node, varName, name, value, status, b) {
|
|
|
1680
2242
|
inputValue = "e.target.value";
|
|
1681
2243
|
break;
|
|
1682
2244
|
}
|
|
1683
|
-
} else if (node.tagName === "select")
|
|
2245
|
+
} else if (node.tagName === "select") {
|
|
2246
|
+
eventName = "change";
|
|
2247
|
+
if (node.attributes.find((a) => a.name === "multiple")) {
|
|
2248
|
+
buildRun("setBinding", `Array.from(${varName}.options).forEach((opt) => opt.selected = Array.isArray(${value}) && ${value}.includes(opt.value));`, status, b);
|
|
2249
|
+
status.imports.add("t_event");
|
|
2250
|
+
b.append(`t_event(${varName}, "change", (e) => ${value} = Array.from(e.target.selectedOptions).map((opt) => opt.value));`);
|
|
2251
|
+
return;
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
1684
2254
|
let set = `${value} || ${defaultValue}`;
|
|
1685
|
-
|
|
2255
|
+
const propName = name.substring(1);
|
|
2256
|
+
buildRun("setBinding", `${propName === "value" ? `${varName}.${propName} = String(${set})` : `${varName}.${propName} = ${set}`};`, status, b);
|
|
1686
2257
|
status.imports.add("t_event");
|
|
1687
2258
|
b.append(`t_event(${varName}, "${eventName}", (e) => ${value} = ${inputValue});`);
|
|
1688
2259
|
}
|
|
@@ -1718,7 +2289,6 @@ function buildTransitionAttribute(node, varName, name, value, status, b) {
|
|
|
1718
2289
|
b.append(`t_animate(${varName}, null, ${entryVarName});`);
|
|
1719
2290
|
}
|
|
1720
2291
|
}
|
|
1721
|
-
|
|
1722
2292
|
//#endregion
|
|
1723
2293
|
//#region src/compile/build/client/buildRootNode.ts
|
|
1724
2294
|
function buildRootNode(node, status, b, parentName, anchorName) {
|
|
@@ -1731,13 +2301,12 @@ function buildRootNode(node, status, b, parentName, anchorName) {
|
|
|
1731
2301
|
status.fragmentStack.pop();
|
|
1732
2302
|
buildAddFragment(node, status, b, parentName, anchorName);
|
|
1733
2303
|
}
|
|
1734
|
-
|
|
1735
2304
|
//#endregion
|
|
1736
2305
|
//#region src/compile/build/client/buildSlotNode.ts
|
|
1737
2306
|
function buildSlotNode(node, status, b) {
|
|
1738
2307
|
let slotName = node.attributes.find((a) => a.name === "name")?.value;
|
|
1739
2308
|
slotName = slotName ? trimQuotes(slotName) : "_";
|
|
1740
|
-
const propsName = nextVarName("
|
|
2309
|
+
const propsName = nextVarName("slot_props", status);
|
|
1741
2310
|
const slotAttributes = node.attributes.filter((a) => a.name !== "name");
|
|
1742
2311
|
const slotHasProps = slotAttributes.length;
|
|
1743
2312
|
if (slotHasProps) {
|
|
@@ -1774,7 +2343,7 @@ function buildSlotNode(node, status, b) {
|
|
|
1774
2343
|
"$context"
|
|
1775
2344
|
];
|
|
1776
2345
|
b.append(`$slots["${slotName}"](${params.join(", ")})`);
|
|
1777
|
-
const fill = node.children.find((c) => isSpecialNode(c) && c.tagName === "fill");
|
|
2346
|
+
const fill = node.children.find((c) => isSpecialNode(c) && (c.tagName === "fill" || c.tagName === "filldef"));
|
|
1778
2347
|
if (fill && isSpecialNode(fill) && fill.children.length) {
|
|
1779
2348
|
b.append(`} else {`);
|
|
1780
2349
|
buildFragment(fill, status, b, slotParentName, slotAnchorName);
|
|
@@ -1788,7 +2357,6 @@ function buildSlotNode(node, status, b) {
|
|
|
1788
2357
|
}
|
|
1789
2358
|
b.append(`}`);
|
|
1790
2359
|
}
|
|
1791
|
-
|
|
1792
2360
|
//#endregion
|
|
1793
2361
|
//#region src/compile/build/client/buildSpecialNode.ts
|
|
1794
2362
|
function buildSpecialNode(node, status, b, parentName, root = false) {
|
|
@@ -1796,9 +2364,10 @@ function buildSpecialNode(node, status, b, parentName, root = false) {
|
|
|
1796
2364
|
case "slot":
|
|
1797
2365
|
buildSlotNode(node, status, b);
|
|
1798
2366
|
break;
|
|
1799
|
-
case "fill":
|
|
2367
|
+
case "fill":
|
|
2368
|
+
case "filldef": break;
|
|
1800
2369
|
case "@element":
|
|
1801
|
-
buildElementNode(node, status, b, parentName
|
|
2370
|
+
buildElementNode(node, status, b, parentName);
|
|
1802
2371
|
break;
|
|
1803
2372
|
case "@component":
|
|
1804
2373
|
buildComponentNode(node, status, b, root);
|
|
@@ -1807,7 +2376,6 @@ function buildSpecialNode(node, status, b, parentName, root = false) {
|
|
|
1807
2376
|
}
|
|
1808
2377
|
return "";
|
|
1809
2378
|
}
|
|
1810
|
-
|
|
1811
2379
|
//#endregion
|
|
1812
2380
|
//#region src/compile/build/client/buildTextNode.ts
|
|
1813
2381
|
function buildTextNode(node, status) {
|
|
@@ -1824,7 +2392,6 @@ function buildTextNode(node, status) {
|
|
|
1824
2392
|
let maxLevel = 0;
|
|
1825
2393
|
for (let i = 0; i < content.length; i++) {
|
|
1826
2394
|
const char = content[i];
|
|
1827
|
-
const nextChar = content[i + 1];
|
|
1828
2395
|
if (char === "{") {
|
|
1829
2396
|
level++;
|
|
1830
2397
|
maxLevel = Math.max(level, maxLevel);
|
|
@@ -1843,25 +2410,10 @@ function buildTextNode(node, status) {
|
|
|
1843
2410
|
}
|
|
1844
2411
|
} else if (char === "`" && level === 0) textContent += "\\";
|
|
1845
2412
|
else if (level > 0) {
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
continue;
|
|
1851
|
-
} else if (char === "/" && nextChar === "*") {
|
|
1852
|
-
let start = i;
|
|
1853
|
-
i = content.indexOf("*/", i) + 1;
|
|
1854
|
-
textContent += content.substring(start, i + 1);
|
|
1855
|
-
continue;
|
|
1856
|
-
} else if (char === "\"" || char === "'") {
|
|
1857
|
-
let start = i;
|
|
1858
|
-
i = endOfString(char, content, i);
|
|
1859
|
-
textContent += content.substring(start, i + 1);
|
|
1860
|
-
continue;
|
|
1861
|
-
} else if (char === "`") {
|
|
1862
|
-
let start = i;
|
|
1863
|
-
i = endOfTemplateString(content, i);
|
|
1864
|
-
textContent += content.substring(start, i + 1);
|
|
2413
|
+
const skipped = skipStringOrComment(content, i);
|
|
2414
|
+
if (skipped !== -1) {
|
|
2415
|
+
textContent += content.substring(i, skipped);
|
|
2416
|
+
i = skipped - 1;
|
|
1865
2417
|
continue;
|
|
1866
2418
|
}
|
|
1867
2419
|
}
|
|
@@ -1881,7 +2433,6 @@ function buildTextNode(node, status) {
|
|
|
1881
2433
|
stashRunWithOffsets(fragment, `${node.varName}.textContent = `, textContent, ";", node.spans, offsets, lengths, status);
|
|
1882
2434
|
}
|
|
1883
2435
|
}
|
|
1884
|
-
|
|
1885
2436
|
//#endregion
|
|
1886
2437
|
//#region src/compile/build/client/buildNode.ts
|
|
1887
2438
|
function buildNode(node, status, b, parentName, anchorName, root = false) {
|
|
@@ -1900,7 +2451,7 @@ function buildNode(node, status, b, parentName, anchorName, root = false) {
|
|
|
1900
2451
|
buildComponentNode(node, status, b, root);
|
|
1901
2452
|
break;
|
|
1902
2453
|
case "element":
|
|
1903
|
-
buildElementNode(node, status, b, parentName
|
|
2454
|
+
buildElementNode(node, status, b, parentName);
|
|
1904
2455
|
break;
|
|
1905
2456
|
case "text":
|
|
1906
2457
|
buildTextNode(node, status);
|
|
@@ -1908,28 +2459,40 @@ function buildNode(node, status, b, parentName, anchorName, root = false) {
|
|
|
1908
2459
|
case "special":
|
|
1909
2460
|
buildSpecialNode(node, status, b, parentName, root);
|
|
1910
2461
|
break;
|
|
2462
|
+
case "comment": break;
|
|
1911
2463
|
default: throw new Error(`Invalid node type: ${node.type}`);
|
|
1912
2464
|
}
|
|
1913
2465
|
}
|
|
1914
|
-
|
|
1915
2466
|
//#endregion
|
|
1916
2467
|
//#region src/compile/build/client/buildCode.ts
|
|
1917
2468
|
const importsMap$1 = {
|
|
1918
2469
|
$watch: "import { $watch } from \"${folder}\";",
|
|
2470
|
+
$bind: "import { $bind } from \"${folder}\";",
|
|
1919
2471
|
$cache: "import { $cache } from \"${folder}\";",
|
|
2472
|
+
$async: "import { $async } from \"${folder}\";",
|
|
2473
|
+
$pending: "import { $pending } from \"${folder}\";",
|
|
2474
|
+
$refresh: "import { $refresh } from \"${folder}\";",
|
|
1920
2475
|
$run: "import { $run } from \"${folder}\";",
|
|
1921
|
-
$
|
|
2476
|
+
$onmount: "import { $onmount } from \"${folder}\";",
|
|
2477
|
+
$stream: "import { $stream } from \"${folder}\";",
|
|
2478
|
+
fromElement: "import { fromElement } from \"${folder}\";",
|
|
2479
|
+
fromServer: "import { fromServer } from \"${folder}\";",
|
|
2480
|
+
fromWebSocket: "import { fromWebSocket } from \"${folder}\";",
|
|
1922
2481
|
$unwrap: "import { $unwrap } from \"${folder}\";",
|
|
1923
2482
|
$peek: "import { $peek } from \"${folder}\";",
|
|
1924
2483
|
$batch: "import { $batch } from \"${folder}\";",
|
|
1925
2484
|
t_region: "import { t_region } from \"${folder}\";",
|
|
1926
2485
|
t_push_region: "import { t_push_region } from \"${folder}\";",
|
|
1927
2486
|
t_pop_region: "import { t_pop_region } from \"${folder}\";",
|
|
2487
|
+
t_rerun_region_effects: "import { t_rerun_region_effects } from \"${folder}\";",
|
|
1928
2488
|
t_run_control: "import { t_run_control } from \"${folder}\";",
|
|
1929
2489
|
t_run_branch: "import { t_run_branch } from \"${folder}\";",
|
|
1930
2490
|
t_list_item: "import { t_list_item } from \"${folder}\";",
|
|
1931
2491
|
t_run_list: "import { t_run_list } from \"${folder}\";",
|
|
2492
|
+
t_run_await: "import { t_run_await } from \"${folder}\";",
|
|
2493
|
+
t_run_try: "import { t_run_try } from \"${folder}\";",
|
|
1932
2494
|
t_add_fragment: "import { t_add_fragment } from \"${folder}\";",
|
|
2495
|
+
t_add_element: "import { t_add_element } from \"${folder}\";",
|
|
1933
2496
|
t_apply_props: "import { t_apply_props } from \"${folder}\";",
|
|
1934
2497
|
t_class: "import { t_class } from \"${folder}\";",
|
|
1935
2498
|
t_style: "import { t_style } from \"${folder}\";",
|
|
@@ -1937,19 +2500,24 @@ const importsMap$1 = {
|
|
|
1937
2500
|
t_dynamic: "import { t_dynamic } from \"${folder}\";",
|
|
1938
2501
|
t_fmt: "import { t_fmt } from \"${folder}\";",
|
|
1939
2502
|
t_fragment: "import { t_fragment } from \"${folder}\";",
|
|
2503
|
+
t_fragment_el: "import { t_fragment_el } from \"${folder}\";",
|
|
1940
2504
|
t_event: "import { t_event } from \"${folder}\";",
|
|
1941
2505
|
t_animate: "import { t_animate } from \"${folder}\";",
|
|
1942
2506
|
t_root: "import { t_root } from \"${folder}\";",
|
|
2507
|
+
t_root_el: "import { t_root_el } from \"${folder}\";",
|
|
1943
2508
|
t_anchor: "import { t_anchor } from \"${folder}\";",
|
|
1944
2509
|
t_child: "import { t_child } from \"${folder}\";",
|
|
1945
2510
|
t_next: "import { t_next } from \"${folder}\";",
|
|
1946
2511
|
t_skip: "import { t_skip } from \"${folder}\";",
|
|
1947
2512
|
t_frg: "import { t_frg } from \"${folder}\";",
|
|
2513
|
+
t_save_hydration: "import { t_save_hydration } from \"${folder}\";",
|
|
2514
|
+
t_restore_hydration: "import { t_restore_hydration } from \"${folder}\";",
|
|
1948
2515
|
t_elm: "import { t_elm } from \"${folder}\";",
|
|
1949
2516
|
t_txt: "import { t_txt } from \"${folder}\";",
|
|
1950
2517
|
t_cmt: "import { t_cmt } from \"${folder}\";",
|
|
1951
2518
|
t_print: "import { t_print } from \"${folder}\";",
|
|
1952
2519
|
ListItem: "import { type ListItem } from \"${folder}\";",
|
|
2520
|
+
ListItemSpec: "import { type ListItemSpec } from \"${folder}\";",
|
|
1953
2521
|
SlotRender: "import { type SlotRender } from \"${folder}\";",
|
|
1954
2522
|
devContext: "import { devContext } from \"${folder}/dev\";",
|
|
1955
2523
|
t_push_dev_bound: "import { t_push_dev_bound } from \"${folder}/dev\";",
|
|
@@ -1962,13 +2530,10 @@ function buildCode(template, map, options) {
|
|
|
1962
2530
|
buildTemplate(template, map, imports, b, options);
|
|
1963
2531
|
let startSize = b.toString().length;
|
|
1964
2532
|
if (imports.size) {
|
|
2533
|
+
const folder = options?.renderFolder ?? "@torpor/view";
|
|
2534
|
+
const sortedImports = Array.from(imports).map((imp) => (importsMap$1[imp] ?? imp).replace("${folder}", folder)).sort().reverse();
|
|
1965
2535
|
b.prepend("");
|
|
1966
|
-
for (let imp of
|
|
1967
|
-
let importStatement = importsMap$1[imp] ?? imp;
|
|
1968
|
-
let folder = options?.renderFolder ?? "@torpor/view";
|
|
1969
|
-
importStatement = importStatement.replace("${folder}", folder);
|
|
1970
|
-
b.prepend(importStatement);
|
|
1971
|
-
}
|
|
2536
|
+
for (let imp of sortedImports) b.prepend(imp);
|
|
1972
2537
|
}
|
|
1973
2538
|
if (options?.mapped) {
|
|
1974
2539
|
let diff = b.toString().length - startSize + 1;
|
|
@@ -1981,13 +2546,27 @@ function buildCode(template, map, options) {
|
|
|
1981
2546
|
}
|
|
1982
2547
|
function buildTemplate(template, map, imports, b, options) {
|
|
1983
2548
|
let script = template.script.map((s) => s.script).join("\n");
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
if (/\$
|
|
2549
|
+
for (const component of template.components) {
|
|
2550
|
+
if (component.markup) script += "\n" + collectMarkupExpressions(component.markup);
|
|
2551
|
+
if (component.error) script += "\n" + collectMarkupExpressions(component.error);
|
|
2552
|
+
if (component.head) script += "\n" + collectMarkupExpressions(component.head);
|
|
2553
|
+
}
|
|
2554
|
+
let scriptCode = codeRanges(script).map(([start, end]) => script.substring(start, end)).join("");
|
|
2555
|
+
if (/\$watch\b/.test(scriptCode)) imports.add("$watch");
|
|
2556
|
+
if (/\$bind\b/.test(scriptCode)) imports.add("$bind");
|
|
2557
|
+
if (/\$cache\b/.test(scriptCode)) imports.add("$cache");
|
|
2558
|
+
if (/\$async\b/.test(scriptCode)) imports.add("$async");
|
|
2559
|
+
if (/\$pending\b/.test(scriptCode)) imports.add("$pending");
|
|
2560
|
+
if (/\$refresh\b/.test(scriptCode)) imports.add("$refresh");
|
|
2561
|
+
if (/\$run\b/.test(scriptCode)) imports.add("$run");
|
|
2562
|
+
if (/\$onmount\b/.test(scriptCode)) imports.add("$onmount");
|
|
2563
|
+
if (/\$stream\b/.test(scriptCode)) imports.add("$stream");
|
|
2564
|
+
if (/\bfromElement\b/.test(scriptCode)) imports.add("fromElement");
|
|
2565
|
+
if (/\bfromServer\b/.test(scriptCode)) imports.add("fromServer");
|
|
2566
|
+
if (/\bfromWebSocket\b/.test(scriptCode)) imports.add("fromWebSocket");
|
|
2567
|
+
if (/\$unwrap\b/.test(scriptCode)) imports.add("$unwrap");
|
|
2568
|
+
if (/\$peek\b/.test(scriptCode)) imports.add("$peek");
|
|
2569
|
+
if (/\$batch\b/.test(scriptCode)) imports.add("$batch");
|
|
1991
2570
|
let currentIndex = 0;
|
|
1992
2571
|
let current = template.components[0];
|
|
1993
2572
|
let status = makeStatus(imports, map, current, options);
|
|
@@ -1999,10 +2578,10 @@ function buildTemplate(template, map, imports, b, options) {
|
|
|
1999
2578
|
${current.markup ? "$parent" : "_$parent"}: ParentNode,
|
|
2000
2579
|
${current.markup ? "$anchor" : "_$anchor"}: Node | null,`);
|
|
2001
2580
|
if (current.params !== void 0) addMappedText("", current.params, ",", chunk.span, status, b);
|
|
2002
|
-
else b.append(`${current.props?.length ? "$props: Record<PropertyKey, any>" : "
|
|
2581
|
+
else b.append(`${current.props?.length ? "$props: Record<PropertyKey, any>" : "_$props?: Record<PropertyKey, any>"},`);
|
|
2003
2582
|
b.append(`
|
|
2004
|
-
|
|
2005
|
-
|
|
2583
|
+
${current.contextProps?.length || current.markup && markupRendersComponent(current.markup) || current.error && markupRendersComponent(current.error) ? "$context" : "_$context"}?: Record<PropertyKey, any>,
|
|
2584
|
+
${current.slotProps?.length ? "$slots" : "_$slots"}?: Record<string, SlotRender>,`);
|
|
2006
2585
|
} else if (chunk.script === ") /* @return_type */ {") b.append("): void {");
|
|
2007
2586
|
else if (chunk.script === "/* @start */") {
|
|
2008
2587
|
addPushDevBoundary("component", current.name || "Anon Component", status, b);
|
|
@@ -2011,10 +2590,26 @@ function buildTemplate(template, map, imports, b, options) {
|
|
|
2011
2590
|
if (current.markup) {
|
|
2012
2591
|
b.append("");
|
|
2013
2592
|
b.append("/* User interface */");
|
|
2593
|
+
if (current.error) {
|
|
2594
|
+
status.imports.add("t_region");
|
|
2595
|
+
status.imports.add("t_run_try");
|
|
2596
|
+
b.append(`const t_error_region = t_region();`);
|
|
2597
|
+
b.append(`t_run_try(t_error_region, $anchor, () => {`);
|
|
2598
|
+
}
|
|
2014
2599
|
buildFragmentText(current.markup, status, b);
|
|
2015
2600
|
b.append("");
|
|
2016
2601
|
buildNode(current.markup, status, b, "$parent", "$anchor", true);
|
|
2017
2602
|
}
|
|
2603
|
+
} else if (chunk.script === "/* @error */") {
|
|
2604
|
+
if (current.markup && current.error) {
|
|
2605
|
+
b.append(`}, (_, ${current.errorVar ?? "err"}) => {`);
|
|
2606
|
+
b.append("");
|
|
2607
|
+
b.append("/* User interface error */");
|
|
2608
|
+
buildFragmentText(current.error, status, b);
|
|
2609
|
+
b.append("");
|
|
2610
|
+
buildNode(current.error, status, b, "$parent", "$anchor", true);
|
|
2611
|
+
b.append(`}${status.options.dev === true ? ", \"runError\"" : ""});`);
|
|
2612
|
+
}
|
|
2018
2613
|
} else if (chunk.script === "/* @head */") {
|
|
2019
2614
|
if (current.head) {
|
|
2020
2615
|
b.append("");
|
|
@@ -2046,15 +2641,26 @@ function makeStatus(imports, map, current, options) {
|
|
|
2046
2641
|
options: options ?? {}
|
|
2047
2642
|
};
|
|
2048
2643
|
}
|
|
2049
|
-
|
|
2050
2644
|
//#endregion
|
|
2051
2645
|
//#region src/compile/build/client/buildStyles.ts
|
|
2052
2646
|
function buildStyles(style, styleHash) {
|
|
2053
2647
|
const b = new Builder();
|
|
2054
|
-
for (let block of style.
|
|
2648
|
+
for (let block of style.children) buildStyleNode(block, b, styleHash);
|
|
2055
2649
|
return b.toString();
|
|
2056
2650
|
}
|
|
2057
|
-
function
|
|
2651
|
+
function buildStyleNode(node, b, styleHash) {
|
|
2652
|
+
switch (node.type) {
|
|
2653
|
+
case "block":
|
|
2654
|
+
buildBlockNode(node, b, styleHash);
|
|
2655
|
+
break;
|
|
2656
|
+
case "attribute":
|
|
2657
|
+
buildAttributeNode(node, b);
|
|
2658
|
+
break;
|
|
2659
|
+
case "comment": break;
|
|
2660
|
+
default: throw new Error(`Invalid style node type: ${node.type}`);
|
|
2661
|
+
}
|
|
2662
|
+
}
|
|
2663
|
+
function buildBlockNode(block, b, styleHash) {
|
|
2058
2664
|
if (block.selector.startsWith("@")) b.append(`${block.selector} {`);
|
|
2059
2665
|
else {
|
|
2060
2666
|
block.selector = block.selector.replaceAll(/\s+/g, " ");
|
|
@@ -2080,8 +2686,7 @@ function buildStyleBlock(block, b, styleHash) {
|
|
|
2080
2686
|
}
|
|
2081
2687
|
b.append(`${selectors.join(" ").replaceAll(" ,", ",")} {`);
|
|
2082
2688
|
}
|
|
2083
|
-
for (let
|
|
2084
|
-
for (let child of block.children) buildStyleBlock(child, b, styleHash);
|
|
2689
|
+
for (let child of block.children) buildStyleNode(child, b, styleHash);
|
|
2085
2690
|
b.append(`}`);
|
|
2086
2691
|
}
|
|
2087
2692
|
function addSelector(selectors, selector, styleHash, start, end, global) {
|
|
@@ -2100,10 +2705,9 @@ function addSelector(selectors, selector, styleHash, start, end, global) {
|
|
|
2100
2705
|
selectors.push(s);
|
|
2101
2706
|
}
|
|
2102
2707
|
}
|
|
2103
|
-
function
|
|
2708
|
+
function buildAttributeNode(attribute, b) {
|
|
2104
2709
|
b.append(`${attribute.name}: ${attribute.value};`);
|
|
2105
2710
|
}
|
|
2106
|
-
|
|
2107
2711
|
//#endregion
|
|
2108
2712
|
//#region src/compile/build/server/buildServerComponentNode.ts
|
|
2109
2713
|
function buildServerComponentNode(node, status, b) {
|
|
@@ -2119,20 +2723,16 @@ function buildServerComponentNode(node, status, b) {
|
|
|
2119
2723
|
for (let { name, value } of node.attributes) {
|
|
2120
2724
|
if (name.startsWith("&")) name = name.substring(1);
|
|
2121
2725
|
if (name === "class" && value != null) {
|
|
2122
|
-
status.
|
|
2123
|
-
const params = [value];
|
|
2124
|
-
if (node.scopeStyles) params.push(`"torp-${status.styleHash}"`);
|
|
2726
|
+
if (node.scopeStyles) value = `[${value}, "torp-${status.styleHash}"]`;
|
|
2125
2727
|
props.push({
|
|
2126
2728
|
name,
|
|
2127
|
-
value
|
|
2729
|
+
value
|
|
2128
2730
|
});
|
|
2129
|
-
} else if (name === "style" && value != null) {
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
});
|
|
2135
|
-
} else if (value != null) props.push({
|
|
2731
|
+
} else if (name === "style" && value != null) props.push({
|
|
2732
|
+
name,
|
|
2733
|
+
value
|
|
2734
|
+
});
|
|
2735
|
+
else if (value != null) props.push({
|
|
2136
2736
|
name,
|
|
2137
2737
|
value
|
|
2138
2738
|
});
|
|
@@ -2142,17 +2742,17 @@ function buildServerComponentNode(node, status, b) {
|
|
|
2142
2742
|
});
|
|
2143
2743
|
}
|
|
2144
2744
|
b.append(`const ${propsName} = {`);
|
|
2145
|
-
for (let p of props) b.append(`${p.name}: ${p.value},`);
|
|
2745
|
+
for (let p of props) b.append(p.name ? `${p.name}: ${p.value},` : `${p.value},`);
|
|
2146
2746
|
b.append("};");
|
|
2147
2747
|
}
|
|
2148
2748
|
const componentHasSlots = node.children.length;
|
|
2149
2749
|
const slotsName = componentHasSlots ? nextVarName("slots", status) : "undefined";
|
|
2150
2750
|
if (componentHasSlots) {
|
|
2151
2751
|
b.append(`const ${slotsName}: Record<string, ServerSlotRender> = {};`);
|
|
2152
|
-
for (let slot of node.children) if (isSpecialNode(slot) && slot.tagName === "fill") {
|
|
2752
|
+
for (let slot of node.children) if (isSpecialNode(slot) && (slot.tagName === "fill" || slot.tagName === "filldef")) {
|
|
2153
2753
|
const nameAttribute = slot.attributes.find((a) => a.name === "name");
|
|
2154
2754
|
const slotName = nameAttribute?.value ? trimQuotes(nameAttribute.value) : "_";
|
|
2155
|
-
const slotParams = [slot.hasSlotProps ? "$
|
|
2755
|
+
const slotParams = [slot.hasSlotProps ? "$slot: Record<PropertyKey, any>" : "_$slot?: Record<PropertyKey, any>", "// @ts-ignore\n// eslint-disable-next-line no-unused-vars\n$context?: Record<PropertyKey, any>"];
|
|
2156
2756
|
b.append(`${slot.hasSlotProps ? "// @ts-ignore\n" : ""}${slotsName}["${slotName}"] = (\n${slotParams.join(",\n")}\n) => {`);
|
|
2157
2757
|
b.append(`let t_body = "";`);
|
|
2158
2758
|
for (let child of slot.children) buildServerNode(child, status, b);
|
|
@@ -2176,9 +2776,8 @@ function buildServerComponentNode(node, status, b) {
|
|
|
2176
2776
|
b.append(`t_body += ${componentResult}.body;`);
|
|
2177
2777
|
b.append(`t_head += ${componentResult}.head;`);
|
|
2178
2778
|
status.output += HYDRATION_END_COMMENT;
|
|
2179
|
-
status.output +=
|
|
2779
|
+
status.output += "<!>";
|
|
2180
2780
|
}
|
|
2181
|
-
|
|
2182
2781
|
//#endregion
|
|
2183
2782
|
//#region src/compile/build/server/buildServerAwaitNode.ts
|
|
2184
2783
|
function buildServerAwaitNode(node, status, b) {
|
|
@@ -2187,9 +2786,9 @@ function buildServerAwaitNode(node, status, b) {
|
|
|
2187
2786
|
b.append(`t_body += \`${status.output}\`;`);
|
|
2188
2787
|
status.output = "";
|
|
2189
2788
|
}
|
|
2190
|
-
for (let branch of node.children) if (isControlNode(branch) && branch.operation === "@
|
|
2789
|
+
for (let branch of node.children) if (isControlNode(branch) && branch.operation === "@with") buildServerAwaitBranch(branch, status, b);
|
|
2191
2790
|
status.output += HYDRATION_END_COMMENT;
|
|
2192
|
-
status.output +=
|
|
2791
|
+
status.output += "<!>";
|
|
2193
2792
|
}
|
|
2194
2793
|
function buildServerAwaitBranch(node, status, b) {
|
|
2195
2794
|
for (let child of node.children) buildServerNode(child, status, b);
|
|
@@ -2198,7 +2797,6 @@ function buildServerAwaitBranch(node, status, b) {
|
|
|
2198
2797
|
status.output = "";
|
|
2199
2798
|
}
|
|
2200
2799
|
}
|
|
2201
|
-
|
|
2202
2800
|
//#endregion
|
|
2203
2801
|
//#region src/compile/build/server/buildServerForNode.ts
|
|
2204
2802
|
function buildServerForNode(node, status, b) {
|
|
@@ -2209,7 +2807,7 @@ function buildServerForNode(node, status, b) {
|
|
|
2209
2807
|
}
|
|
2210
2808
|
for (let branch of node.children) if (isControlNode(branch)) buildServerForBranch(branch, status, b);
|
|
2211
2809
|
status.output += HYDRATION_END_COMMENT;
|
|
2212
|
-
status.output +=
|
|
2810
|
+
status.output += "<!>";
|
|
2213
2811
|
}
|
|
2214
2812
|
function buildServerForBranch(node, status, b) {
|
|
2215
2813
|
b.append(`${node.statement} {`);
|
|
@@ -2221,16 +2819,14 @@ function buildServerForBranch(node, status, b) {
|
|
|
2221
2819
|
}
|
|
2222
2820
|
b.append("}");
|
|
2223
2821
|
}
|
|
2224
|
-
|
|
2225
2822
|
//#endregion
|
|
2226
2823
|
//#region src/compile/build/server/buildServerHtmlNode.ts
|
|
2227
2824
|
function buildServerHtmlNode(node, status) {
|
|
2228
2825
|
status.output += HYDRATION_START_COMMENT;
|
|
2229
2826
|
for (let branch of node.children) if (isControlNode(branch)) status.output += `\${${branch.statement}}`;
|
|
2230
2827
|
status.output += HYDRATION_END_COMMENT;
|
|
2231
|
-
status.output +=
|
|
2828
|
+
status.output += "<!>";
|
|
2232
2829
|
}
|
|
2233
|
-
|
|
2234
2830
|
//#endregion
|
|
2235
2831
|
//#region src/compile/build/server/buildServerIfNode.ts
|
|
2236
2832
|
function buildServerIfNode(node, status, b) {
|
|
@@ -2241,7 +2837,7 @@ function buildServerIfNode(node, status, b) {
|
|
|
2241
2837
|
}
|
|
2242
2838
|
for (let branch of node.children) if (isControlNode(branch)) buildServerIfBranch(branch, status, b);
|
|
2243
2839
|
status.output += HYDRATION_END_COMMENT;
|
|
2244
|
-
status.output +=
|
|
2840
|
+
status.output += "<!>";
|
|
2245
2841
|
}
|
|
2246
2842
|
function buildServerIfBranch(node, status, b) {
|
|
2247
2843
|
b.append(`${node.statement} {`);
|
|
@@ -2253,7 +2849,6 @@ function buildServerIfBranch(node, status, b) {
|
|
|
2253
2849
|
}
|
|
2254
2850
|
b.append("}");
|
|
2255
2851
|
}
|
|
2256
|
-
|
|
2257
2852
|
//#endregion
|
|
2258
2853
|
//#region src/compile/build/server/buildServerReplaceNode.ts
|
|
2259
2854
|
function buildServerReplaceNode(node, status, b) {
|
|
@@ -2264,7 +2859,7 @@ function buildServerReplaceNode(node, status, b) {
|
|
|
2264
2859
|
}
|
|
2265
2860
|
for (let branch of node.children) if (isControlNode(branch)) buildServerReplaceBranch(branch, status, b);
|
|
2266
2861
|
status.output += HYDRATION_END_COMMENT;
|
|
2267
|
-
status.output +=
|
|
2862
|
+
status.output += "<!>";
|
|
2268
2863
|
}
|
|
2269
2864
|
function buildServerReplaceBranch(node, status, b) {
|
|
2270
2865
|
b.append(`${node.statement};`);
|
|
@@ -2274,7 +2869,6 @@ function buildServerReplaceBranch(node, status, b) {
|
|
|
2274
2869
|
status.output = "";
|
|
2275
2870
|
}
|
|
2276
2871
|
}
|
|
2277
|
-
|
|
2278
2872
|
//#endregion
|
|
2279
2873
|
//#region src/compile/build/server/buildServerScriptNode.ts
|
|
2280
2874
|
function buildServerScriptNode(node, b) {
|
|
@@ -2284,7 +2878,6 @@ function maybeAppend(text, end) {
|
|
|
2284
2878
|
if (!text.endsWith(end)) text += end;
|
|
2285
2879
|
return text;
|
|
2286
2880
|
}
|
|
2287
|
-
|
|
2288
2881
|
//#endregion
|
|
2289
2882
|
//#region src/compile/build/server/buildServerSwitchNode.ts
|
|
2290
2883
|
function buildServerSwitchNode(node, status, b) {
|
|
@@ -2297,7 +2890,7 @@ function buildServerSwitchNode(node, status, b) {
|
|
|
2297
2890
|
for (let branch of node.children.filter((c) => isControlNode(c))) if (isControlNode(branch)) buildServerSwitchBranch(branch, status, b);
|
|
2298
2891
|
b.append("}");
|
|
2299
2892
|
status.output += HYDRATION_END_COMMENT;
|
|
2300
|
-
status.output +=
|
|
2893
|
+
status.output += "<!>";
|
|
2301
2894
|
}
|
|
2302
2895
|
function buildServerSwitchBranch(node, status, b) {
|
|
2303
2896
|
b.append(`${node.statement} {`);
|
|
@@ -2310,7 +2903,39 @@ function buildServerSwitchBranch(node, status, b) {
|
|
|
2310
2903
|
b.append("break;");
|
|
2311
2904
|
b.append("}");
|
|
2312
2905
|
}
|
|
2313
|
-
|
|
2906
|
+
//#endregion
|
|
2907
|
+
//#region src/compile/build/server/buildServerTryNode.ts
|
|
2908
|
+
function buildServerTryNode(node, status, b) {
|
|
2909
|
+
status.output += HYDRATION_START_COMMENT;
|
|
2910
|
+
if (status.output) {
|
|
2911
|
+
b.append(`t_body += \`${status.output}\`;`);
|
|
2912
|
+
status.output = "";
|
|
2913
|
+
}
|
|
2914
|
+
const branches = node.children.filter((n) => isControlNode(n));
|
|
2915
|
+
let tryBranch = branches.find((n) => n.operation === "@try");
|
|
2916
|
+
let catchBranch = branches.find((n) => n.operation === "@catch");
|
|
2917
|
+
if (catchBranch) {
|
|
2918
|
+
b.append(`const t_try_body = t_body;`);
|
|
2919
|
+
b.append(`const t_try_head = t_head;`);
|
|
2920
|
+
b.append("try {");
|
|
2921
|
+
if (tryBranch) buildServerTryBranch(tryBranch, status, b);
|
|
2922
|
+
const catchVar = trimMatched(catchBranch.statement.substring(5).trim(), "(", ")");
|
|
2923
|
+
b.append(`} catch (${catchVar || "err"}) {`);
|
|
2924
|
+
b.append("t_body = t_try_body;");
|
|
2925
|
+
b.append("t_head = t_try_head;");
|
|
2926
|
+
buildServerTryBranch(catchBranch, status, b);
|
|
2927
|
+
b.append("}");
|
|
2928
|
+
} else if (tryBranch) buildServerTryBranch(tryBranch, status, b);
|
|
2929
|
+
status.output += HYDRATION_END_COMMENT;
|
|
2930
|
+
status.output += "<!>";
|
|
2931
|
+
}
|
|
2932
|
+
function buildServerTryBranch(node, status, b) {
|
|
2933
|
+
for (let child of node.children) buildServerNode(child, status, b);
|
|
2934
|
+
if (status.output) {
|
|
2935
|
+
b.append(`t_body += \`${status.output}\`;`);
|
|
2936
|
+
status.output = "";
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2314
2939
|
//#endregion
|
|
2315
2940
|
//#region src/compile/build/server/buildServerControlNode.ts
|
|
2316
2941
|
function buildServerControlNode(node, status, b) {
|
|
@@ -2335,8 +2960,12 @@ function buildServerControlNode(node, status, b) {
|
|
|
2335
2960
|
buildServerAwaitNode(node, status, b);
|
|
2336
2961
|
break;
|
|
2337
2962
|
case "@await":
|
|
2338
|
-
case "@
|
|
2963
|
+
case "@with":
|
|
2964
|
+
case "@try":
|
|
2339
2965
|
case "@catch": break;
|
|
2966
|
+
case "@try group":
|
|
2967
|
+
buildServerTryNode(node, status, b);
|
|
2968
|
+
break;
|
|
2340
2969
|
case "@replace group":
|
|
2341
2970
|
buildServerReplaceNode(node, status, b);
|
|
2342
2971
|
break;
|
|
@@ -2364,30 +2993,9 @@ function buildServerControlNode(node, status, b) {
|
|
|
2364
2993
|
break;
|
|
2365
2994
|
case "@console":
|
|
2366
2995
|
case "@debugger": break;
|
|
2367
|
-
default: throw new Error(`Invalid operation: ${node.operation}`);
|
|
2368
|
-
}
|
|
2369
|
-
}
|
|
2370
|
-
|
|
2371
|
-
//#endregion
|
|
2372
|
-
//#region src/compile/utils/voidTags.ts
|
|
2373
|
-
const voidTags = [
|
|
2374
|
-
"area",
|
|
2375
|
-
"base",
|
|
2376
|
-
"br",
|
|
2377
|
-
"col",
|
|
2378
|
-
"embed",
|
|
2379
|
-
"hr",
|
|
2380
|
-
"img",
|
|
2381
|
-
"input",
|
|
2382
|
-
"link",
|
|
2383
|
-
"meta",
|
|
2384
|
-
"param",
|
|
2385
|
-
"source",
|
|
2386
|
-
"track",
|
|
2387
|
-
"wbr"
|
|
2388
|
-
];
|
|
2389
|
-
var voidTags_default = voidTags;
|
|
2390
|
-
|
|
2996
|
+
default: throw new Error(`Invalid operation: ${node.operation}`);
|
|
2997
|
+
}
|
|
2998
|
+
}
|
|
2391
2999
|
//#endregion
|
|
2392
3000
|
//#region src/compile/build/server/buildServerElementNode.ts
|
|
2393
3001
|
function buildServerElementNode(node, status, b) {
|
|
@@ -2399,7 +3007,7 @@ function buildServerElementNode(node, status, b) {
|
|
|
2399
3007
|
if (selfAttribute) tagName = `$\{${selfAttribute.value}}`;
|
|
2400
3008
|
}
|
|
2401
3009
|
status.output += `<${tagName}${attributes}>`;
|
|
2402
|
-
if (!
|
|
3010
|
+
if (!voidTags.includes(tagName)) {
|
|
2403
3011
|
const oldPreserveWhitespace = status.preserveWhitespace;
|
|
2404
3012
|
status.preserveWhitespace = ["pre", "code"].includes(tagName);
|
|
2405
3013
|
for (let child of node.children) buildServerNode(child, status, b);
|
|
@@ -2411,6 +3019,7 @@ function buildElementAttributes(node, status) {
|
|
|
2411
3019
|
let needsClass = node.scopeStyles;
|
|
2412
3020
|
let attributes = [];
|
|
2413
3021
|
for (let { name, value, reactive, fullyReactive } of node.attributes) if (name === "self" && node.tagName === "@element") {} else if (name === "&ref") {} else if (name.startsWith("on") || name.startsWith(":on")) {} else if (name.startsWith("transition") && value) {} else if (value != null && fullyReactive) if (name === "&value" || name === "&checked" || name === "&group") {
|
|
3022
|
+
if (name === "&value" && node.tagName === "select" && node.attributes.some((a) => a.name === "multiple")) continue;
|
|
2414
3023
|
let defaultValue = "";
|
|
2415
3024
|
let typeAttribute = node.attributes.find((a) => a.name === "type");
|
|
2416
3025
|
if (typeAttribute && typeAttribute.value) switch (trimQuotes(typeAttribute.value)) {
|
|
@@ -2457,13 +3066,11 @@ function buildElementAttributes(node, status) {
|
|
|
2457
3066
|
if (needsClass) attributes.push(`class="torp-${status.styleHash}"`);
|
|
2458
3067
|
return attributes.join(" ");
|
|
2459
3068
|
}
|
|
2460
|
-
|
|
2461
3069
|
//#endregion
|
|
2462
3070
|
//#region src/compile/build/server/buildServerRootNode.ts
|
|
2463
3071
|
function buildServerRootNode(node, status, b) {
|
|
2464
3072
|
for (let child of node.children) buildServerNode(child, status, b);
|
|
2465
3073
|
}
|
|
2466
|
-
|
|
2467
3074
|
//#endregion
|
|
2468
3075
|
//#region src/compile/build/server/buildServerSlotNode.ts
|
|
2469
3076
|
function buildServerSlotNode(node, status, b) {
|
|
@@ -2474,7 +3081,7 @@ function buildServerSlotNode(node, status, b) {
|
|
|
2474
3081
|
}
|
|
2475
3082
|
let slotName = node.attributes.find((a) => a.name === "name")?.value;
|
|
2476
3083
|
slotName = slotName ? trimQuotes(slotName) : "_";
|
|
2477
|
-
const propsName = nextVarName("
|
|
3084
|
+
const propsName = nextVarName("slot_props", status);
|
|
2478
3085
|
const slotAttributes = node.attributes.filter((a) => a.name !== "name");
|
|
2479
3086
|
const slotHasProps = slotAttributes.length;
|
|
2480
3087
|
if (slotHasProps) {
|
|
@@ -2484,7 +3091,7 @@ function buildServerSlotNode(node, status, b) {
|
|
|
2484
3091
|
}
|
|
2485
3092
|
b.append(`if ($slots && $slots["${slotName}"]) {`);
|
|
2486
3093
|
b.append(`t_body += $slots["${slotName}"](${slotHasProps ? propsName : "undefined"}, $context);`);
|
|
2487
|
-
const fill = node.children.find((c) => isSpecialNode(c) && c.tagName === "fill");
|
|
3094
|
+
const fill = node.children.find((c) => isSpecialNode(c) && (c.tagName === "fill" || c.tagName === "filldef"));
|
|
2488
3095
|
if (fill && isSpecialNode(fill) && fill.children.length) {
|
|
2489
3096
|
b.append(`} else {`);
|
|
2490
3097
|
for (let child of fill.children) buildServerNode(child, status, b);
|
|
@@ -2495,9 +3102,8 @@ function buildServerSlotNode(node, status, b) {
|
|
|
2495
3102
|
}
|
|
2496
3103
|
b.append(`}`);
|
|
2497
3104
|
status.output += HYDRATION_END_COMMENT;
|
|
2498
|
-
status.output +=
|
|
3105
|
+
status.output += "<!>";
|
|
2499
3106
|
}
|
|
2500
|
-
|
|
2501
3107
|
//#endregion
|
|
2502
3108
|
//#region src/compile/build/server/buildServerSpecialNode.ts
|
|
2503
3109
|
function buildServerSpecialNode(node, status, b) {
|
|
@@ -2505,7 +3111,8 @@ function buildServerSpecialNode(node, status, b) {
|
|
|
2505
3111
|
case "slot":
|
|
2506
3112
|
buildServerSlotNode(node, status, b);
|
|
2507
3113
|
break;
|
|
2508
|
-
case "fill":
|
|
3114
|
+
case "fill":
|
|
3115
|
+
case "filldef": break;
|
|
2509
3116
|
case "@element":
|
|
2510
3117
|
buildServerElementNode(node, status, b);
|
|
2511
3118
|
break;
|
|
@@ -2515,7 +3122,6 @@ function buildServerSpecialNode(node, status, b) {
|
|
|
2515
3122
|
default: throw new Error(`Invalid special node: ${node.tagName}`);
|
|
2516
3123
|
}
|
|
2517
3124
|
}
|
|
2518
|
-
|
|
2519
3125
|
//#endregion
|
|
2520
3126
|
//#region src/compile/build/server/buildServerTextNode.ts
|
|
2521
3127
|
function buildServerTextNode(node, status) {
|
|
@@ -2524,7 +3130,6 @@ function buildServerTextNode(node, status) {
|
|
|
2524
3130
|
let level = 0;
|
|
2525
3131
|
for (let i = 0; i < content.length; i++) {
|
|
2526
3132
|
const char = content[i];
|
|
2527
|
-
const nextChar = content[i + 1];
|
|
2528
3133
|
if (char === "{") {
|
|
2529
3134
|
level++;
|
|
2530
3135
|
if (level === 1) {
|
|
@@ -2537,35 +3142,16 @@ function buildServerTextNode(node, status) {
|
|
|
2537
3142
|
if (level === 0) status.output += ")";
|
|
2538
3143
|
} else if (char === "`" && level === 0) status.output += "\\";
|
|
2539
3144
|
else if (level > 0) {
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
continue;
|
|
2545
|
-
} else if (char === "/" && nextChar === "*") {
|
|
2546
|
-
let start = i;
|
|
2547
|
-
i = content.indexOf("*/", i) + 1;
|
|
2548
|
-
status.output += escapeHtml(content.substring(start, i + 1));
|
|
2549
|
-
continue;
|
|
2550
|
-
} else if (char === "\"" || char === "'") {
|
|
2551
|
-
let start = i;
|
|
2552
|
-
i = endOfString(char, content, i);
|
|
2553
|
-
status.output += escapeHtml(content.substring(start, i + 1));
|
|
2554
|
-
continue;
|
|
2555
|
-
} else if (char === "`") {
|
|
2556
|
-
let start = i;
|
|
2557
|
-
i = endOfTemplateString(content, i);
|
|
2558
|
-
status.output += escapeHtml(content.substring(start, i + 1));
|
|
3145
|
+
const skipped = skipStringOrComment(content, i);
|
|
3146
|
+
if (skipped !== -1) {
|
|
3147
|
+
status.output += content.substring(i, skipped);
|
|
3148
|
+
i = skipped - 1;
|
|
2559
3149
|
continue;
|
|
2560
3150
|
}
|
|
2561
3151
|
}
|
|
2562
3152
|
status.output += char;
|
|
2563
3153
|
}
|
|
2564
3154
|
}
|
|
2565
|
-
function escapeHtml(text) {
|
|
2566
|
-
return text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
2567
|
-
}
|
|
2568
|
-
|
|
2569
3155
|
//#endregion
|
|
2570
3156
|
//#region src/compile/build/server/buildServerNode.ts
|
|
2571
3157
|
function buildServerNode(node, status, b) {
|
|
@@ -2588,17 +3174,25 @@ function buildServerNode(node, status, b) {
|
|
|
2588
3174
|
case "special":
|
|
2589
3175
|
buildServerSpecialNode(node, status, b);
|
|
2590
3176
|
break;
|
|
3177
|
+
case "comment": break;
|
|
2591
3178
|
default: throw new Error(`Invalid node type: ${node.type}`);
|
|
2592
3179
|
}
|
|
2593
3180
|
}
|
|
2594
|
-
|
|
2595
3181
|
//#endregion
|
|
2596
3182
|
//#region src/compile/build/server/buildServerCode.ts
|
|
2597
3183
|
const importsMap = {
|
|
2598
3184
|
$watch: "import { $watch } from \"${folder}\";",
|
|
3185
|
+
$bind: "import { $bind } from \"${folder}\";",
|
|
2599
3186
|
$cache: "import { $cache } from \"${folder}\";",
|
|
3187
|
+
$async: "import { $async } from \"${folder}\";",
|
|
3188
|
+
$pending: "import { $pending } from \"${folder}\";",
|
|
3189
|
+
$refresh: "import { $refresh } from \"${folder}\";",
|
|
2600
3190
|
$run: "import { $run } from \"${folder}\";",
|
|
2601
|
-
$
|
|
3191
|
+
$onmount: "import { $onmount } from \"${folder}\";",
|
|
3192
|
+
$stream: "import { $stream } from \"${folder}\";",
|
|
3193
|
+
fromElement: "import { fromElement } from \"${folder}\";",
|
|
3194
|
+
fromServer: "import { fromServer } from \"${folder}\";",
|
|
3195
|
+
fromWebSocket: "import { fromWebSocket } from \"${folder}\";",
|
|
2602
3196
|
$unwrap: "import { $unwrap } from \"${folder}\";",
|
|
2603
3197
|
$peek: "import { $peek } from \"${folder}\";",
|
|
2604
3198
|
$batch: "import { $batch } from \"${folder}\";",
|
|
@@ -2614,33 +3208,46 @@ function buildServerCode(template, options) {
|
|
|
2614
3208
|
imports.add("ServerSlotRender");
|
|
2615
3209
|
buildServerTemplate(template, imports, b, options);
|
|
2616
3210
|
if (imports.size) {
|
|
3211
|
+
const folder = options?.renderFolder ?? "@torpor/view/ssr";
|
|
3212
|
+
const sortedImports = Array.from(imports).map((imp) => (importsMap[imp] ?? imp).replace("${folder}", folder)).sort().reverse();
|
|
2617
3213
|
b.prepend("");
|
|
2618
|
-
for (let imp of
|
|
2619
|
-
imp = importsMap[imp] || imp;
|
|
2620
|
-
b.prepend(imp.replace("${folder}", options?.renderFolder || "@torpor/view/ssr"));
|
|
2621
|
-
}
|
|
3214
|
+
for (let imp of sortedImports) b.prepend(imp);
|
|
2622
3215
|
}
|
|
2623
3216
|
return b.toString();
|
|
2624
3217
|
}
|
|
2625
3218
|
function buildServerTemplate(template, imports, b, options) {
|
|
2626
3219
|
let script = template.script.map((s) => s.script).join("\n");
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
if (/\$
|
|
3220
|
+
for (const component of template.components) {
|
|
3221
|
+
if (component.markup) script += "\n" + collectMarkupExpressions(component.markup);
|
|
3222
|
+
if (component.error) script += "\n" + collectMarkupExpressions(component.error);
|
|
3223
|
+
if (component.head) script += "\n" + collectMarkupExpressions(component.head);
|
|
3224
|
+
}
|
|
3225
|
+
let scriptCode = codeRanges(script).map(([start, end]) => script.substring(start, end)).join("");
|
|
3226
|
+
if (/\$watch\b/.test(scriptCode)) imports.add("$watch");
|
|
3227
|
+
if (/\$bind\b/.test(scriptCode)) imports.add("$bind");
|
|
3228
|
+
if (/\$cache\b/.test(scriptCode)) imports.add("$cache");
|
|
3229
|
+
if (/\$async\b/.test(scriptCode)) imports.add("$async");
|
|
3230
|
+
if (/\$pending\b/.test(scriptCode)) imports.add("$pending");
|
|
3231
|
+
if (/\$refresh\b/.test(scriptCode)) imports.add("$refresh");
|
|
3232
|
+
if (/\$run\b/.test(scriptCode)) imports.add("$run");
|
|
3233
|
+
if (/\$onmount\b/.test(scriptCode)) imports.add("$onmount");
|
|
3234
|
+
if (/\$stream\b/.test(scriptCode)) imports.add("$stream");
|
|
3235
|
+
if (/\bfromElement\b/.test(scriptCode)) imports.add("fromElement");
|
|
3236
|
+
if (/\bfromServer\b/.test(scriptCode)) imports.add("fromServer");
|
|
3237
|
+
if (/\bfromWebSocket\b/.test(scriptCode)) imports.add("fromWebSocket");
|
|
3238
|
+
if (/\$unwrap\b/.test(scriptCode)) imports.add("$unwrap");
|
|
3239
|
+
if (/\$peek\b/.test(scriptCode)) imports.add("$peek");
|
|
3240
|
+
if (/\$batch\b/.test(scriptCode)) imports.add("$batch");
|
|
2634
3241
|
let currentIndex = 0;
|
|
2635
3242
|
let current = template.components[0];
|
|
2636
3243
|
for (let chunk of template.script) if (chunk.script === "/* @params */") {
|
|
2637
3244
|
let params = [
|
|
2638
|
-
current.params ?? `${current.props?.length ? "$props: Record<PropertyKey, any>" : "
|
|
2639
|
-
`${current.contextProps?.length ? "$context" : "
|
|
2640
|
-
`${current.slotProps?.length ? "$slots" : "
|
|
3245
|
+
current.params ?? `${current.props?.length ? "$props: Record<PropertyKey, any>" : "_$props?: Record<PropertyKey, any>"}`,
|
|
3246
|
+
`${current.contextProps?.length || current.markup && markupRendersComponent(current.markup) || current.error && markupRendersComponent(current.error) ? "$context" : "_$context"}?: Record<PropertyKey, any>`,
|
|
3247
|
+
`${current.slotProps?.length ? "$slots" : "_$slots"}?: Record<string, ServerSlotRender>`
|
|
2641
3248
|
];
|
|
2642
|
-
b.append(params.join(",\n"));
|
|
2643
|
-
} else if (chunk.script === ") /* @return_type */ {") b.append("): { body: string
|
|
3249
|
+
b.append(params.join(",\n") + ",");
|
|
3250
|
+
} else if (chunk.script === ") /* @return_type */ {") b.append("): { body: string; head: string } {");
|
|
2644
3251
|
else if (chunk.script === "/* @start */") {
|
|
2645
3252
|
if (current.contextProps?.length) b.append(`$context = Object.assign({}, $context);`);
|
|
2646
3253
|
b.append(`let t_body = "";`);
|
|
@@ -2657,12 +3264,38 @@ function buildServerTemplate(template, imports, b, options) {
|
|
|
2657
3264
|
};
|
|
2658
3265
|
b.append("");
|
|
2659
3266
|
b.append("/* User interface */");
|
|
3267
|
+
if (current.error) {
|
|
3268
|
+
b.append(`const t_try_body = t_body;`);
|
|
3269
|
+
b.append(`const t_try_head = t_head;`);
|
|
3270
|
+
b.append("try {");
|
|
3271
|
+
}
|
|
2660
3272
|
buildServerNode(current.markup, status, b);
|
|
2661
3273
|
if (status.output) {
|
|
2662
3274
|
b.append(`t_body += \`${status.output}\`;`);
|
|
2663
3275
|
status.output = "";
|
|
2664
3276
|
}
|
|
2665
3277
|
}
|
|
3278
|
+
} else if (chunk.script === "/* @error */") {
|
|
3279
|
+
if (current.markup && current.error) {
|
|
3280
|
+
const status = {
|
|
3281
|
+
imports,
|
|
3282
|
+
output: "",
|
|
3283
|
+
styleHash: current.style?.hash || "",
|
|
3284
|
+
varNames: {},
|
|
3285
|
+
preserveWhitespace: false,
|
|
3286
|
+
options
|
|
3287
|
+
};
|
|
3288
|
+
b.append(`} catch (${current.errorVar}) {`);
|
|
3289
|
+
b.append("t_body = t_try_body;");
|
|
3290
|
+
b.append("t_head = t_try_head;");
|
|
3291
|
+
b.append("/* User interface error */");
|
|
3292
|
+
buildServerNode(current.error, status, b);
|
|
3293
|
+
if (status.output) {
|
|
3294
|
+
b.append(`t_body += \`${status.output}\`;`);
|
|
3295
|
+
status.output = "";
|
|
3296
|
+
}
|
|
3297
|
+
b.append("}");
|
|
3298
|
+
}
|
|
2666
3299
|
} else if (chunk.script === "/* @head */") {} else if (chunk.script === "/* @style */") {
|
|
2667
3300
|
if (current.style) {
|
|
2668
3301
|
b.append("");
|
|
@@ -2676,7 +3309,109 @@ function buildServerTemplate(template, imports, b, options) {
|
|
|
2676
3309
|
current = template.components[currentIndex];
|
|
2677
3310
|
} else b.append(chunk.script);
|
|
2678
3311
|
}
|
|
2679
|
-
|
|
3312
|
+
//#endregion
|
|
3313
|
+
//#region src/compile/utils/trimWhitespace.ts
|
|
3314
|
+
/**
|
|
3315
|
+
* Tag names whose contents should keep all whitespace untouched, matching
|
|
3316
|
+
* native HTML rendering of `pre`/`textarea` and the existing build-time
|
|
3317
|
+
* `preserveWhitespace` behavior for `code`.
|
|
3318
|
+
*/
|
|
3319
|
+
const PRESERVE_TAGS = /* @__PURE__ */ new Set([
|
|
3320
|
+
"pre",
|
|
3321
|
+
"textarea",
|
|
3322
|
+
"code"
|
|
3323
|
+
]);
|
|
3324
|
+
/**
|
|
3325
|
+
* Container tags whose child layout ignores inter-child whitespace entirely
|
|
3326
|
+
* (table layout collapses it). Inside these, whitespace-only text nodes
|
|
3327
|
+
* between children are removed rather than collapsed to a single space.
|
|
3328
|
+
*/
|
|
3329
|
+
const WHITESPACE_INSIGNIFICANT_TAGS = /* @__PURE__ */ new Set([
|
|
3330
|
+
"tr",
|
|
3331
|
+
"tbody",
|
|
3332
|
+
"thead",
|
|
3333
|
+
"tfoot",
|
|
3334
|
+
"colgroup",
|
|
3335
|
+
"table",
|
|
3336
|
+
"ul",
|
|
3337
|
+
"ol",
|
|
3338
|
+
"select"
|
|
3339
|
+
]);
|
|
3340
|
+
/**
|
|
3341
|
+
* Trims and collapses whitespace text nodes in a parsed markup tree,
|
|
3342
|
+
* Svelte 5-style:
|
|
3343
|
+
*
|
|
3344
|
+
* - pure-whitespace text nodes at the start/end of a container are removed
|
|
3345
|
+
* - pure-whitespace text nodes between siblings are collapsed to a single space
|
|
3346
|
+
* (or removed entirely inside table/list containers where whitespace is
|
|
3347
|
+
* insignificant — see WHITESPACE_INSIGNIFICANT_TAGS)
|
|
3348
|
+
* - whitespace inside `pre`, `textarea`, and `code` is preserved
|
|
3349
|
+
*
|
|
3350
|
+
* Whitespace adjacent to non-rendering control nodes (e.g. `@key`, `@const`)
|
|
3351
|
+
* is treated as leading/trailing whitespace of the container and removed,
|
|
3352
|
+
* because those nodes produce no DOM output — without this rule, every row
|
|
3353
|
+
* of `@for { @key = ...; <tr>...</tr> }` would emit a phantom leading
|
|
3354
|
+
* whitespace text node.
|
|
3355
|
+
*
|
|
3356
|
+
* Mixed text nodes (containing non-whitespace content) are left untouched;
|
|
3357
|
+
* their internal whitespace continues to be collapsed at build time. The
|
|
3358
|
+
* walk mutates the tree in place and is idempotent.
|
|
3359
|
+
*
|
|
3360
|
+
* @param node The root node of the markup to trim (a component's markup or head)
|
|
3361
|
+
*/
|
|
3362
|
+
function trimWhitespace(node) {
|
|
3363
|
+
trimChildren(node.children, false, void 0);
|
|
3364
|
+
}
|
|
3365
|
+
function trimChildren(children, preserveAll, parentTag) {
|
|
3366
|
+
for (const child of children) {
|
|
3367
|
+
const grandChildren = getChildren(child);
|
|
3368
|
+
if (grandChildren) {
|
|
3369
|
+
const childTag = getTagName(child);
|
|
3370
|
+
trimChildren(grandChildren, preserveAll || PRESERVE_TAGS.has(childTag ?? ""), childTag);
|
|
3371
|
+
}
|
|
3372
|
+
}
|
|
3373
|
+
if (preserveAll) return;
|
|
3374
|
+
if (parentTag !== void 0 && WHITESPACE_INSIGNIFICANT_TAGS.has(parentTag)) {
|
|
3375
|
+
for (let i = children.length - 1; i >= 0; i--) if (isWhitespaceText(children[i])) children.splice(i, 1);
|
|
3376
|
+
return;
|
|
3377
|
+
}
|
|
3378
|
+
const length = children.length;
|
|
3379
|
+
const hasRenderingBefore = Array.from({ length });
|
|
3380
|
+
const hasRenderingAfter = Array.from({ length });
|
|
3381
|
+
let seenBefore = false;
|
|
3382
|
+
let seenAfter = false;
|
|
3383
|
+
for (let i = 0; i < length; i++) {
|
|
3384
|
+
hasRenderingBefore[i] = seenBefore;
|
|
3385
|
+
if (isRenderingSibling(children[i])) seenBefore = true;
|
|
3386
|
+
const j = length - 1 - i;
|
|
3387
|
+
hasRenderingAfter[j] = seenAfter;
|
|
3388
|
+
if (isRenderingSibling(children[j])) seenAfter = true;
|
|
3389
|
+
}
|
|
3390
|
+
for (let i = length - 1; i >= 0; i--) if (isWhitespaceText(children[i]) && (!hasRenderingBefore[i] || !hasRenderingAfter[i])) children.splice(i, 1);
|
|
3391
|
+
for (const child of children) if (isTextNode(child) && child.content !== "" && isSpace(child.content)) child.content = " ";
|
|
3392
|
+
}
|
|
3393
|
+
/**
|
|
3394
|
+
* Returns true if `node` is a "rendering sibling" — i.e. a node that produces
|
|
3395
|
+
* visible DOM output and therefore acts as a boundary between leading,
|
|
3396
|
+
* inter-sibling, and trailing whitespace. Whitespace text nodes themselves
|
|
3397
|
+
* (the subject of the trim) and non-rendering control nodes (e.g. @key,
|
|
3398
|
+
* @const) are not rendering siblings.
|
|
3399
|
+
*/
|
|
3400
|
+
function isRenderingSibling(node) {
|
|
3401
|
+
if (isWhitespaceText(node)) return false;
|
|
3402
|
+
if (isControlNode(node) && NON_RENDERING_OPERATIONS.has(node.operation)) return false;
|
|
3403
|
+
return true;
|
|
3404
|
+
}
|
|
3405
|
+
function getChildren(node) {
|
|
3406
|
+
if (node.type === "text" || node.type === "comment") return void 0;
|
|
3407
|
+
return node.children;
|
|
3408
|
+
}
|
|
3409
|
+
function getTagName(node) {
|
|
3410
|
+
if (node.type === "element" || node.type === "component" || node.type === "special") return node.tagName;
|
|
3411
|
+
}
|
|
3412
|
+
function isWhitespaceText(node) {
|
|
3413
|
+
return isTextNode(node) && node.content !== "" && isSpace(node.content);
|
|
3414
|
+
}
|
|
2680
3415
|
//#endregion
|
|
2681
3416
|
//#region src/compile/build.ts
|
|
2682
3417
|
/**
|
|
@@ -2687,6 +3422,11 @@ function buildServerTemplate(template, imports, b, options) {
|
|
|
2687
3422
|
* @returns The component's code and styles
|
|
2688
3423
|
*/
|
|
2689
3424
|
function build(template, options) {
|
|
3425
|
+
if (!options?.preserveWhitespace) for (const component of template.components) {
|
|
3426
|
+
if (component.markup) trimWhitespace(component.markup);
|
|
3427
|
+
if (component.error) trimWhitespace(component.error);
|
|
3428
|
+
if (component.head) trimWhitespace(component.head);
|
|
3429
|
+
}
|
|
2690
3430
|
let map = [];
|
|
2691
3431
|
return {
|
|
2692
3432
|
code: options?.server ? buildServerCode(template, options) : buildCode(template, map, options),
|
|
@@ -2697,7 +3437,6 @@ function build(template, options) {
|
|
|
2697
3437
|
map
|
|
2698
3438
|
};
|
|
2699
3439
|
}
|
|
2700
|
-
|
|
2701
3440
|
//#endregion
|
|
2702
3441
|
//#region src/compile/buildType.ts
|
|
2703
3442
|
/**
|
|
@@ -2719,7 +3458,7 @@ function buildType(template, options) {
|
|
|
2719
3458
|
}
|
|
2720
3459
|
b.append(`
|
|
2721
3460
|
${component.documentation ?? ""}
|
|
2722
|
-
${component.default ? "declare" : "export declare"} function ${component.name}(
|
|
3461
|
+
${component.default ? "declare" : "export declare"} function ${component.name}${component.typeParams ?? ""}(
|
|
2723
3462
|
$parent: ParentNode,
|
|
2724
3463
|
$anchor: Node | null,
|
|
2725
3464
|
${component.params || "$props?: Record<PropertyKey, any>"},
|
|
@@ -2731,13 +3470,29 @@ function buildType(template, options) {
|
|
|
2731
3470
|
}
|
|
2732
3471
|
return b.toString();
|
|
2733
3472
|
}
|
|
2734
|
-
|
|
3473
|
+
//#endregion
|
|
3474
|
+
//#region src/compile/utils/endOfTemplateString.ts
|
|
3475
|
+
/**
|
|
3476
|
+
* Returns the index of the closing backtick of the template string that
|
|
3477
|
+
* starts at `start`.
|
|
3478
|
+
*
|
|
3479
|
+
* NOTE: For scanning JavaScript expression text, use `codeScanner.ts` instead
|
|
3480
|
+
* -- this variant doesn't handle escaped characters or nested templates in
|
|
3481
|
+
* interpolations, and is kept only for non-JS contexts (CSS in `@style`
|
|
3482
|
+
* blocks) where a simpler scan is wanted.
|
|
3483
|
+
*/
|
|
3484
|
+
function endOfTemplateString(content, start) {
|
|
3485
|
+
let level = 0;
|
|
3486
|
+
for (let j = start + 1; j < content.length; j++) if (content[j] === "`" && content[j - 1] !== "\\" && level === 0) return j;
|
|
3487
|
+
else if (content[j] === "{" && (level > 0 || content[j - 1] === "$")) level += 1;
|
|
3488
|
+
else if (content[j] === "}" && level > 0) level -= 1;
|
|
3489
|
+
return content.length;
|
|
3490
|
+
}
|
|
2735
3491
|
//#endregion
|
|
2736
3492
|
//#region src/compile/utils/isSpaceNode.ts
|
|
2737
3493
|
function isSpaceNode(node) {
|
|
2738
3494
|
return isTextNode(node) && isSpace(node.content);
|
|
2739
3495
|
}
|
|
2740
|
-
|
|
2741
3496
|
//#endregion
|
|
2742
3497
|
//#region src/compile/parse/utils/accept.ts
|
|
2743
3498
|
/**
|
|
@@ -2758,25 +3513,24 @@ function accept(value, status, advance = true) {
|
|
|
2758
3513
|
}
|
|
2759
3514
|
return false;
|
|
2760
3515
|
}
|
|
2761
|
-
|
|
2762
3516
|
//#endregion
|
|
2763
3517
|
//#region src/compile/parse/parseInlineScript.ts
|
|
2764
3518
|
function parseInlineScript(status) {
|
|
2765
3519
|
const start = status.i;
|
|
2766
3520
|
let braceCount = 0;
|
|
2767
|
-
while (status.i < status.source.length)
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
3521
|
+
while (status.i < status.source.length) {
|
|
3522
|
+
const skipped = skipStringOrComment(status.source, status.i);
|
|
3523
|
+
if (skipped !== -1) {
|
|
3524
|
+
status.i = skipped;
|
|
3525
|
+
continue;
|
|
3526
|
+
}
|
|
3527
|
+
if (accept("{", status)) braceCount += 1;
|
|
3528
|
+
else if (accept("}", status)) if (braceCount > 0) braceCount -= 1;
|
|
3529
|
+
else return status.source.substring(start, status.i - 1);
|
|
3530
|
+
else status.i += 1;
|
|
3531
|
+
}
|
|
2777
3532
|
return "";
|
|
2778
3533
|
}
|
|
2779
|
-
|
|
2780
3534
|
//#endregion
|
|
2781
3535
|
//#region src/compile/utils/trimStart.ts
|
|
2782
3536
|
/**
|
|
@@ -2792,7 +3546,6 @@ function trimStart(text, char) {
|
|
|
2792
3546
|
while (text[start] === char) start += 1;
|
|
2793
3547
|
return text.substring(start);
|
|
2794
3548
|
}
|
|
2795
|
-
|
|
2796
3549
|
//#endregion
|
|
2797
3550
|
//#region src/compile/parse/utils/consumeSpace.ts
|
|
2798
3551
|
/**
|
|
@@ -2809,7 +3562,6 @@ function consumeSpace(status) {
|
|
|
2809
3562
|
for (status.i; status.i < status.source.length; status.i++) if (!isSpaceChar(status.source, status.i)) return status.source.substring(start, status.i);
|
|
2810
3563
|
return "";
|
|
2811
3564
|
}
|
|
2812
|
-
|
|
2813
3565
|
//#endregion
|
|
2814
3566
|
//#region src/compile/parse/addSpaceElement.ts
|
|
2815
3567
|
function addSpaceElement(parent, status) {
|
|
@@ -2827,7 +3579,6 @@ function addSpaceElement(parent, status) {
|
|
|
2827
3579
|
}
|
|
2828
3580
|
}
|
|
2829
3581
|
}
|
|
2830
|
-
|
|
2831
3582
|
//#endregion
|
|
2832
3583
|
//#region src/compile/utils/isFullyReactive.ts
|
|
2833
3584
|
/**
|
|
@@ -2840,7 +3591,41 @@ function addSpaceElement(parent, status) {
|
|
|
2840
3591
|
function isFullyReactive(text) {
|
|
2841
3592
|
return text.trim().startsWith("{") && text.trim().endsWith("}");
|
|
2842
3593
|
}
|
|
2843
|
-
|
|
3594
|
+
//#endregion
|
|
3595
|
+
//#region src/compile/parse/utils/addError.ts
|
|
3596
|
+
/**
|
|
3597
|
+
* Adds an error to the parse status
|
|
3598
|
+
*
|
|
3599
|
+
* @param status The parse status
|
|
3600
|
+
* @param message The error message to add
|
|
3601
|
+
* @param start (Optional) The start position of the error (defaults to the
|
|
3602
|
+
* current parse position)
|
|
3603
|
+
*/
|
|
3604
|
+
function addError(status, message, start = status.i, end = status.i) {
|
|
3605
|
+
let startLine = 0;
|
|
3606
|
+
let lastLineStart = 0;
|
|
3607
|
+
let i = 0;
|
|
3608
|
+
for (; i < start; i++) if (status.source[i] === "\n") {
|
|
3609
|
+
startLine += 1;
|
|
3610
|
+
lastLineStart = i + 1;
|
|
3611
|
+
}
|
|
3612
|
+
let startChar = start - lastLineStart;
|
|
3613
|
+
let endLine = startLine;
|
|
3614
|
+
for (; i < end; i++) if (status.source[i] === "\n") {
|
|
3615
|
+
endLine += 1;
|
|
3616
|
+
lastLineStart = i + 1;
|
|
3617
|
+
}
|
|
3618
|
+
let endChar = end - lastLineStart;
|
|
3619
|
+
status.errors.push({
|
|
3620
|
+
message,
|
|
3621
|
+
startIndex: start,
|
|
3622
|
+
startLine,
|
|
3623
|
+
startChar,
|
|
3624
|
+
endIndex: end,
|
|
3625
|
+
endLine,
|
|
3626
|
+
endChar
|
|
3627
|
+
});
|
|
3628
|
+
}
|
|
2844
3629
|
//#endregion
|
|
2845
3630
|
//#region src/compile/parse/utils/isAlphaNumericChar.ts
|
|
2846
3631
|
/**
|
|
@@ -2855,7 +3640,6 @@ function isAlphaNumericChar(text, index) {
|
|
|
2855
3640
|
let code = text.charCodeAt(index);
|
|
2856
3641
|
return code > 47 && code < 58 || code > 64 && code < 91 || code > 96 && code < 123;
|
|
2857
3642
|
}
|
|
2858
|
-
|
|
2859
3643
|
//#endregion
|
|
2860
3644
|
//#region src/compile/parse/utils/consumeAlphaNumeric.ts
|
|
2861
3645
|
/**
|
|
@@ -2872,7 +3656,6 @@ function consumeAlphaNumeric(status) {
|
|
|
2872
3656
|
for (status.i; status.i < status.source.length; status.i++) if (!isAlphaNumericChar(status.source, status.i)) return status.source.substring(start, status.i);
|
|
2873
3657
|
return "";
|
|
2874
3658
|
}
|
|
2875
|
-
|
|
2876
3659
|
//#endregion
|
|
2877
3660
|
//#region src/compile/parse/utils/consumeUntil.ts
|
|
2878
3661
|
/**
|
|
@@ -2890,7 +3673,6 @@ function consumeUntil(chars, status) {
|
|
|
2890
3673
|
for (status.i; status.i < status.source.length; status.i++) if (chars.includes(status.source[status.i])) return status.source.substring(start, status.i);
|
|
2891
3674
|
return "";
|
|
2892
3675
|
}
|
|
2893
|
-
|
|
2894
3676
|
//#endregion
|
|
2895
3677
|
//#region src/compile/parse/parseTag.ts
|
|
2896
3678
|
function parseTag(status) {
|
|
@@ -2913,6 +3695,7 @@ function parseTag(status) {
|
|
|
2913
3695
|
type: [
|
|
2914
3696
|
"slot",
|
|
2915
3697
|
"fill",
|
|
3698
|
+
"filldef",
|
|
2916
3699
|
"@component",
|
|
2917
3700
|
"@element"
|
|
2918
3701
|
].includes(tagName) ? "special" : "element",
|
|
@@ -2966,10 +3749,12 @@ function parseAttribute(status) {
|
|
|
2966
3749
|
value = name.substring(1, name.length - 1);
|
|
2967
3750
|
valueStart++;
|
|
2968
3751
|
valueEnd = valueStart + name.length;
|
|
2969
|
-
|
|
3752
|
+
if (value.startsWith("...")) name = value;
|
|
3753
|
+
else name = value.split(".").at(-1);
|
|
2970
3754
|
if (name.endsWith("!")) name = name.substring(0, name.length - 1);
|
|
2971
3755
|
reactive = fullyReactive = true;
|
|
2972
3756
|
}
|
|
3757
|
+
if (value != null && reactive && value.startsWith("...")) addError(status, `Spread attributes are not supported`, valueStart, valueEnd);
|
|
2973
3758
|
return {
|
|
2974
3759
|
name,
|
|
2975
3760
|
value,
|
|
@@ -3000,7 +3785,6 @@ function parseAttributeValue(status) {
|
|
|
3000
3785
|
return value;
|
|
3001
3786
|
}
|
|
3002
3787
|
}
|
|
3003
|
-
|
|
3004
3788
|
//#endregion
|
|
3005
3789
|
//#region src/compile/parse/parseText.ts
|
|
3006
3790
|
function parseText(status, element) {
|
|
@@ -3014,18 +3798,16 @@ function parseText(status, element) {
|
|
|
3014
3798
|
let level = 0;
|
|
3015
3799
|
for (let k = j; k < status.source.length; k++) {
|
|
3016
3800
|
const char = status.source[k];
|
|
3017
|
-
|
|
3018
|
-
if (char === "/" && nextChar === "/") k = status.source.indexOf("\n", k);
|
|
3019
|
-
else if (char === "/" && nextChar === "*") k = status.source.indexOf("*/", k) + 1;
|
|
3020
|
-
else if (char === "\"" || char === "'") k = endOfString(char, status.source, k);
|
|
3021
|
-
else if (char === "`") k = endOfTemplateString(status.source, k);
|
|
3022
|
-
else if (char === "{") level += 1;
|
|
3801
|
+
if (char === "{") level += 1;
|
|
3023
3802
|
else if (char === "}") {
|
|
3024
3803
|
level -= 1;
|
|
3025
3804
|
if (level === 0) {
|
|
3026
3805
|
j = k;
|
|
3027
3806
|
break;
|
|
3028
3807
|
}
|
|
3808
|
+
} else {
|
|
3809
|
+
const skipped = skipStringOrComment(status.source, k);
|
|
3810
|
+
if (skipped !== -1) k = skipped - 1;
|
|
3029
3811
|
}
|
|
3030
3812
|
}
|
|
3031
3813
|
spans.push({
|
|
@@ -3061,7 +3843,38 @@ function parseText(status, element) {
|
|
|
3061
3843
|
status.i = end;
|
|
3062
3844
|
} else status.i += 1;
|
|
3063
3845
|
}
|
|
3064
|
-
|
|
3846
|
+
//#endregion
|
|
3847
|
+
//#region src/compile/parse/utils/usesSlot.ts
|
|
3848
|
+
const slotRegex = /\$slot\b/;
|
|
3849
|
+
/**
|
|
3850
|
+
* Checks whether the $slot variable is read in the nodes' own scope
|
|
3851
|
+
*
|
|
3852
|
+
* Note that the children of a component node are excluded, because they are
|
|
3853
|
+
* passed to the component as fills, which get their own $slot parameter.
|
|
3854
|
+
* The component's attributes do belong to this scope.
|
|
3855
|
+
*
|
|
3856
|
+
* @param nodes The nodes to check
|
|
3857
|
+
*
|
|
3858
|
+
* @returns True if a $slot variable is read in the nodes' own scope
|
|
3859
|
+
*/
|
|
3860
|
+
function usesSlot(nodes) {
|
|
3861
|
+
for (let node of nodes) if (node.type === "text") {
|
|
3862
|
+
const text = node;
|
|
3863
|
+
if (slotRegex.test(text.content)) return true;
|
|
3864
|
+
} else if (node.type === "control") {
|
|
3865
|
+
const control = node;
|
|
3866
|
+
if (slotRegex.test(control.statement)) return true;
|
|
3867
|
+
if (usesSlot(control.children)) return true;
|
|
3868
|
+
} else if (node.type === "element" || node.type === "special" || node.type === "component") {
|
|
3869
|
+
const element = node;
|
|
3870
|
+
for (let attribute of element.attributes) {
|
|
3871
|
+
if (slotRegex.test(attribute.name)) return true;
|
|
3872
|
+
if (attribute.value && slotRegex.test(attribute.value)) return true;
|
|
3873
|
+
}
|
|
3874
|
+
if (element.type !== "component" && usesSlot(element.children)) return true;
|
|
3875
|
+
}
|
|
3876
|
+
return false;
|
|
3877
|
+
}
|
|
3065
3878
|
//#endregion
|
|
3066
3879
|
//#region src/compile/parse/slottifyChildNodes.ts
|
|
3067
3880
|
/**
|
|
@@ -3069,65 +3882,33 @@ function parseText(status, element) {
|
|
|
3069
3882
|
* <fill> node
|
|
3070
3883
|
*/
|
|
3071
3884
|
function slottifyChildNodes(node) {
|
|
3072
|
-
|
|
3885
|
+
let nonFillNodes = [];
|
|
3886
|
+
for (let child of node.children) if (isFillNode(child)) if (nonFillNodes.find((n) => !isSpaceNode(n)) === void 0) nonFillNodes = [];
|
|
3887
|
+
else break;
|
|
3888
|
+
else nonFillNodes.push(child);
|
|
3889
|
+
if (nonFillNodes.find((n) => !isSpaceNode(n)) === void 0) nonFillNodes = [];
|
|
3073
3890
|
if (nonFillNodes.length) {
|
|
3074
3891
|
let defaultFillNode = node.children.find((n) => isFillNode(n) && !n.attributes.find((a) => a.name === "name"));
|
|
3075
3892
|
if (!defaultFillNode) {
|
|
3076
3893
|
defaultFillNode = {
|
|
3077
3894
|
type: "special",
|
|
3078
|
-
tagName: "
|
|
3895
|
+
tagName: "filldef",
|
|
3079
3896
|
attributes: [],
|
|
3080
3897
|
children: nonFillNodes,
|
|
3081
3898
|
span: {
|
|
3082
3899
|
start: 0,
|
|
3083
3900
|
end: 0
|
|
3084
|
-
}
|
|
3901
|
+
},
|
|
3902
|
+
hasSlotProps: usesSlot(nonFillNodes)
|
|
3085
3903
|
};
|
|
3086
3904
|
node.children.unshift(defaultFillNode);
|
|
3087
3905
|
}
|
|
3906
|
+
node.children = node.children.filter((c) => isFillNode(c) || !nonFillNodes.includes(c));
|
|
3088
3907
|
}
|
|
3089
|
-
node.children = node.children.filter((c) => isFillNode(c));
|
|
3090
3908
|
}
|
|
3091
3909
|
function isFillNode(n) {
|
|
3092
|
-
return isSpecialNode(n) && n.tagName === "fill";
|
|
3093
|
-
}
|
|
3094
|
-
|
|
3095
|
-
//#endregion
|
|
3096
|
-
//#region src/compile/parse/utils/addError.ts
|
|
3097
|
-
/**
|
|
3098
|
-
* Adds an error to the parse status
|
|
3099
|
-
*
|
|
3100
|
-
* @param status The parse status
|
|
3101
|
-
* @param message The error message to add
|
|
3102
|
-
* @param start (Optional) The start position of the error (defaults to the
|
|
3103
|
-
* current parse position)
|
|
3104
|
-
*/
|
|
3105
|
-
function addError(status, message, start = status.i, end = status.i) {
|
|
3106
|
-
let startLine = 0;
|
|
3107
|
-
let lastLineStart = 0;
|
|
3108
|
-
let i = 0;
|
|
3109
|
-
for (; i < start; i++) if (status.source[i] === "\n") {
|
|
3110
|
-
startLine += 1;
|
|
3111
|
-
lastLineStart = i + 1;
|
|
3112
|
-
}
|
|
3113
|
-
let startChar = start - lastLineStart;
|
|
3114
|
-
let endLine = startLine;
|
|
3115
|
-
for (; i < end; i++) if (status.source[i] === "\n") {
|
|
3116
|
-
endLine += 1;
|
|
3117
|
-
lastLineStart = i + 1;
|
|
3118
|
-
}
|
|
3119
|
-
let endChar = end - lastLineStart;
|
|
3120
|
-
status.errors.push({
|
|
3121
|
-
message,
|
|
3122
|
-
startIndex: start,
|
|
3123
|
-
startLine,
|
|
3124
|
-
startChar,
|
|
3125
|
-
endIndex: end,
|
|
3126
|
-
endLine,
|
|
3127
|
-
endChar
|
|
3128
|
-
});
|
|
3910
|
+
return isSpecialNode(n) && (n.tagName === "fill" || n.tagName === "filldef");
|
|
3129
3911
|
}
|
|
3130
|
-
|
|
3131
3912
|
//#endregion
|
|
3132
3913
|
//#region src/compile/parse/parseElement.ts
|
|
3133
3914
|
function parseElement(status) {
|
|
@@ -3139,16 +3920,40 @@ function parseElement(status) {
|
|
|
3139
3920
|
addError(status, `Non-matching close tag: ${element.tagName.substring(1)}`, start, start + element.tagName.length);
|
|
3140
3921
|
return element;
|
|
3141
3922
|
}
|
|
3142
|
-
if (
|
|
3143
|
-
if (!element.selfClosed && !
|
|
3923
|
+
if (voidTags.includes(element.tagName)) checkVoidElement(status, element);
|
|
3924
|
+
if (!element.selfClosed && !voidTags.includes(element.tagName)) {
|
|
3144
3925
|
status.stack.push(element);
|
|
3145
3926
|
while (status.i < status.source.length) {
|
|
3146
3927
|
addSpaceElement(element, status);
|
|
3147
3928
|
if (accept("}", status, false)) break;
|
|
3148
|
-
else if (accept("<!--", status))
|
|
3149
|
-
|
|
3150
|
-
|
|
3151
|
-
|
|
3929
|
+
else if (accept("<!--", status)) {
|
|
3930
|
+
const start = status.i;
|
|
3931
|
+
status.i = status.source.indexOf("-->", status.i) + 3;
|
|
3932
|
+
const text = {
|
|
3933
|
+
type: "comment",
|
|
3934
|
+
commentType: "html",
|
|
3935
|
+
content: status.source.substring(start, status.i - 3)
|
|
3936
|
+
};
|
|
3937
|
+
element.children.push(text);
|
|
3938
|
+
} else if (accept("@//", status)) {
|
|
3939
|
+
const start = status.i;
|
|
3940
|
+
status.i = status.source.indexOf("\n", status.i) + 1;
|
|
3941
|
+
const text = {
|
|
3942
|
+
type: "comment",
|
|
3943
|
+
commentType: "line",
|
|
3944
|
+
content: status.source.substring(start, status.i - 1)
|
|
3945
|
+
};
|
|
3946
|
+
element.children.push(text);
|
|
3947
|
+
} else if (accept("@/*", status)) {
|
|
3948
|
+
const start = status.i;
|
|
3949
|
+
status.i = status.source.indexOf("*/", status.i) + 2;
|
|
3950
|
+
const text = {
|
|
3951
|
+
type: "comment",
|
|
3952
|
+
commentType: "block",
|
|
3953
|
+
content: status.source.substring(start, status.i - 2)
|
|
3954
|
+
};
|
|
3955
|
+
element.children.push(text);
|
|
3956
|
+
} else if (accept("</", status, false)) {
|
|
3152
3957
|
if (checkCloseTag(status, element)) break;
|
|
3153
3958
|
} else if (accept("<", status, false)) {
|
|
3154
3959
|
const child = parseElement(status);
|
|
@@ -3167,10 +3972,8 @@ function parseElement(status) {
|
|
|
3167
3972
|
slottifyChildNodes(element);
|
|
3168
3973
|
current.slotProps ??= [];
|
|
3169
3974
|
current.slotProps.push(element.attributes.find((a) => a.name === "name")?.name ?? "default");
|
|
3170
|
-
} else if (element.tagName === "fill")
|
|
3171
|
-
|
|
3172
|
-
element.hasSlotProps = /\$sprops\b/.test(fillSource);
|
|
3173
|
-
} else if (element.tagName === "@component") {
|
|
3975
|
+
} else if (element.tagName === "fill" || element.tagName === "filldef") element.hasSlotProps = usesSlot(element.children);
|
|
3976
|
+
else if (element.tagName === "@component") {
|
|
3174
3977
|
const selfAttribute = element.attributes.find((a) => a.name === "self");
|
|
3175
3978
|
if (selfAttribute && selfAttribute.value && selfAttribute.fullyReactive) {
|
|
3176
3979
|
element.type = "component";
|
|
@@ -3198,7 +4001,7 @@ function parseElement(status) {
|
|
|
3198
4001
|
} else addError(status, "@component element must have a self attribute", start, start + element.tagName.length + 1);
|
|
3199
4002
|
}
|
|
3200
4003
|
}
|
|
3201
|
-
if (!element.closed && !
|
|
4004
|
+
if (!element.closed && !voidTags.includes(element.tagName)) addError(status, `Unclosed non-void element: ${element.tagName}`, start, start + element.tagName.length + 1);
|
|
3202
4005
|
return element;
|
|
3203
4006
|
}
|
|
3204
4007
|
function checkVoidElement(status, element) {
|
|
@@ -3233,7 +4036,6 @@ function checkCloseTag(status, element) {
|
|
|
3233
4036
|
if (!found) addError(status, `Non-matching close tag: ${closingTagName} (expected ${element.tagName})`, start, start + element.tagName.length + 1);
|
|
3234
4037
|
return found;
|
|
3235
4038
|
}
|
|
3236
|
-
|
|
3237
4039
|
//#endregion
|
|
3238
4040
|
//#region src/compile/parse/parseControl.ts
|
|
3239
4041
|
const controlOperations = [
|
|
@@ -3245,7 +4047,8 @@ const controlOperations = [
|
|
|
3245
4047
|
"@case",
|
|
3246
4048
|
"@default",
|
|
3247
4049
|
"@await",
|
|
3248
|
-
"@
|
|
4050
|
+
"@with",
|
|
4051
|
+
"@try",
|
|
3249
4052
|
"@catch",
|
|
3250
4053
|
"@replace",
|
|
3251
4054
|
"@html",
|
|
@@ -3268,7 +4071,7 @@ function parseControl(status, parentNode) {
|
|
|
3268
4071
|
const node = parseControlOpen(status);
|
|
3269
4072
|
if (!node) return;
|
|
3270
4073
|
if (standaloneOperations.includes(node.operation)) {
|
|
3271
|
-
wrangleControlNode(node, parentNode);
|
|
4074
|
+
wrangleControlNode(node, parentNode, status);
|
|
3272
4075
|
return;
|
|
3273
4076
|
}
|
|
3274
4077
|
while (status.i < status.source.length) {
|
|
@@ -3287,7 +4090,7 @@ function parseControl(status, parentNode) {
|
|
|
3287
4090
|
break;
|
|
3288
4091
|
}
|
|
3289
4092
|
}
|
|
3290
|
-
wrangleControlNode(node, parentNode);
|
|
4093
|
+
wrangleControlNode(node, parentNode, status);
|
|
3291
4094
|
parseControlBranches(status, parentNode);
|
|
3292
4095
|
}
|
|
3293
4096
|
function parseControlOpen(status) {
|
|
@@ -3309,6 +4112,7 @@ function parseControlOpen(status) {
|
|
|
3309
4112
|
accept("}", status);
|
|
3310
4113
|
}
|
|
3311
4114
|
if (operation === "@replace" || operation === "@html") statement = trimMatched(statement.substring(statement.indexOf("(")).trim(), "(", ")");
|
|
4115
|
+
if (operation === "@await" && /^await\s*\(/.test(statement)) addError(status, "old `@await (expr) { … } then (…) { … }` syntax is removed — use `@await { … } with { … }` with `$async` getters", start, status.i);
|
|
3312
4116
|
} else {
|
|
3313
4117
|
addError(status, `Unknown operation: ${operation}`, status.i, status.i + operation.length);
|
|
3314
4118
|
return null;
|
|
@@ -3326,25 +4130,27 @@ function parseControlOpen(status) {
|
|
|
3326
4130
|
}
|
|
3327
4131
|
function parseControlStatement(start, operation, status) {
|
|
3328
4132
|
let parenCount = 0;
|
|
3329
|
-
while (status.i < status.source.length)
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
|
|
3339
|
-
|
|
4133
|
+
while (status.i < status.source.length) {
|
|
4134
|
+
const skipped = skipStringOrComment(status.source, status.i);
|
|
4135
|
+
if (skipped !== -1) {
|
|
4136
|
+
status.i = skipped;
|
|
4137
|
+
continue;
|
|
4138
|
+
}
|
|
4139
|
+
if (accept("(", status)) parenCount += 1;
|
|
4140
|
+
else if (accept(")", status)) parenCount -= 1;
|
|
4141
|
+
else if (accept("{", status) || accept("\n", status) && standaloneOperations.includes(operation)) {
|
|
4142
|
+
if (parenCount === 0) return trimStart(status.source.substring(start, status.i - 1).trim(), "@");
|
|
4143
|
+
} else status.i += 1;
|
|
4144
|
+
}
|
|
3340
4145
|
return "";
|
|
3341
4146
|
}
|
|
3342
4147
|
/**
|
|
3343
4148
|
* Groups the control node into the correct parent e.g. if/else into an if group
|
|
3344
4149
|
* @param node The control node
|
|
3345
4150
|
* @param parentNode The parent node
|
|
4151
|
+
* @param status The parse status, for error reporting
|
|
3346
4152
|
*/
|
|
3347
|
-
function wrangleControlNode(node, parentNode) {
|
|
4153
|
+
function wrangleControlNode(node, parentNode, status) {
|
|
3348
4154
|
if (node.operation === "@if") {
|
|
3349
4155
|
const ifGroup = {
|
|
3350
4156
|
type: "control",
|
|
@@ -3393,14 +4199,41 @@ function wrangleControlNode(node, parentNode) {
|
|
|
3393
4199
|
}
|
|
3394
4200
|
};
|
|
3395
4201
|
parentNode.children.push(awaitGroup);
|
|
3396
|
-
} else if (node.operation === "@
|
|
4202
|
+
} else if (node.operation === "@with") for (let i = parentNode.children.length - 1; i >= 0; i--) {
|
|
3397
4203
|
const lastChild = parentNode.children[i];
|
|
3398
4204
|
if (isControlNode(lastChild) && lastChild.operation === "@await group") {
|
|
3399
4205
|
lastChild.children.push(node);
|
|
3400
4206
|
break;
|
|
3401
4207
|
}
|
|
3402
4208
|
}
|
|
3403
|
-
else if (node.operation === "@
|
|
4209
|
+
else if (node.operation === "@catch") {
|
|
4210
|
+
let attached = false;
|
|
4211
|
+
let duplicate = false;
|
|
4212
|
+
for (let i = parentNode.children.length - 1; i >= 0; i--) {
|
|
4213
|
+
const lastChild = parentNode.children[i];
|
|
4214
|
+
if (isControlNode(lastChild) && lastChild.operation === "@try group") {
|
|
4215
|
+
if (lastChild.children.some((child) => isControlNode(child) && child.operation === "@catch")) duplicate = true;
|
|
4216
|
+
else {
|
|
4217
|
+
lastChild.children.push(node);
|
|
4218
|
+
attached = true;
|
|
4219
|
+
}
|
|
4220
|
+
break;
|
|
4221
|
+
}
|
|
4222
|
+
}
|
|
4223
|
+
if (!attached) addError(status, duplicate ? "`@try` cannot have more than one `@catch` block" : "`@catch` must follow a `@try` block", node.span.start, node.span.end);
|
|
4224
|
+
} else if (node.operation === "@try") {
|
|
4225
|
+
const tryGroup = {
|
|
4226
|
+
type: "control",
|
|
4227
|
+
operation: "@try group",
|
|
4228
|
+
statement: "",
|
|
4229
|
+
children: [node],
|
|
4230
|
+
span: {
|
|
4231
|
+
start: 0,
|
|
4232
|
+
end: 0
|
|
4233
|
+
}
|
|
4234
|
+
};
|
|
4235
|
+
parentNode.children.push(tryGroup);
|
|
4236
|
+
} else if (node.operation === "@replace") {
|
|
3404
4237
|
const replaceGroup = {
|
|
3405
4238
|
type: "control",
|
|
3406
4239
|
operation: "@replace group",
|
|
@@ -3437,61 +4270,93 @@ function parseControlBranches(status, parentNode) {
|
|
|
3437
4270
|
let start = status.i;
|
|
3438
4271
|
status.i += 1;
|
|
3439
4272
|
consumeSpace(status);
|
|
3440
|
-
if (accept("else", status, false) || accept("case", status, false) || accept("default", status, false) || accept("
|
|
4273
|
+
if (accept("else", status, false) || accept("case", status, false) || accept("default", status, false) || accept("catch", status, false) || accept("with", status, false)) parseControl(status, parentNode);
|
|
3441
4274
|
else {
|
|
3442
4275
|
gotAnotherBranch = false;
|
|
3443
4276
|
status.i = start;
|
|
3444
4277
|
}
|
|
3445
4278
|
}
|
|
3446
4279
|
}
|
|
3447
|
-
|
|
3448
4280
|
//#endregion
|
|
3449
4281
|
//#region src/compile/parse/parseMarkup.ts
|
|
3450
4282
|
function parseMarkup(status, source) {
|
|
3451
4283
|
const current = status.components.at(-1);
|
|
3452
4284
|
if (!current) return;
|
|
4285
|
+
parseMarkupInto(status, source, (root) => current.markup ??= root);
|
|
4286
|
+
}
|
|
4287
|
+
/**
|
|
4288
|
+
* Parses markup into a root node, letting callers choose where the resulting
|
|
4289
|
+
* RootNode is stored (e.g. a component's `markup` or its `error` block).
|
|
4290
|
+
*
|
|
4291
|
+
* @param status The parse status
|
|
4292
|
+
* @param source The full source code
|
|
4293
|
+
* @param setRoot Called with a fresh root node the first time content is
|
|
4294
|
+
* parsed; subsequent content is appended to the same node.
|
|
4295
|
+
*/
|
|
4296
|
+
function parseMarkupInto(status, source, setRoot) {
|
|
4297
|
+
let root = null;
|
|
4298
|
+
const getRoot = () => root ??= {
|
|
4299
|
+
type: "root",
|
|
4300
|
+
children: []
|
|
4301
|
+
};
|
|
3453
4302
|
while (status.i < source.length) if (accept("}", status, false)) return;
|
|
3454
|
-
else if (accept("<!--", status))
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
4303
|
+
else if (accept("<!--", status)) {
|
|
4304
|
+
const start = status.i;
|
|
4305
|
+
status.i = status.source.indexOf("-->", status.i) + 3;
|
|
4306
|
+
const text = {
|
|
4307
|
+
type: "comment",
|
|
4308
|
+
commentType: "html",
|
|
4309
|
+
content: status.source.substring(start, status.i - 3)
|
|
4310
|
+
};
|
|
4311
|
+
setRoot(getRoot());
|
|
4312
|
+
getRoot().children.push(text);
|
|
4313
|
+
} else if (accept("@//", status)) {
|
|
4314
|
+
const start = status.i;
|
|
4315
|
+
status.i = status.source.indexOf("\n", status.i) + 1;
|
|
4316
|
+
const text = {
|
|
4317
|
+
type: "comment",
|
|
4318
|
+
commentType: "line",
|
|
4319
|
+
content: status.source.substring(start, status.i - 1)
|
|
3461
4320
|
};
|
|
4321
|
+
setRoot(getRoot());
|
|
4322
|
+
getRoot().children.push(text);
|
|
4323
|
+
} else if (accept("@/*", status)) {
|
|
4324
|
+
const start = status.i;
|
|
4325
|
+
status.i = status.source.indexOf("*/", status.i) + 2;
|
|
4326
|
+
const text = {
|
|
4327
|
+
type: "comment",
|
|
4328
|
+
commentType: "block",
|
|
4329
|
+
content: status.source.substring(start, status.i - 2)
|
|
4330
|
+
};
|
|
4331
|
+
setRoot(getRoot());
|
|
4332
|
+
getRoot().children.push(text);
|
|
4333
|
+
} else if (isSpaceChar(status.source, status.i)) {
|
|
4334
|
+
setRoot(getRoot());
|
|
3462
4335
|
const text = {
|
|
3463
4336
|
type: "text",
|
|
3464
4337
|
content: consumeSpace(status),
|
|
3465
4338
|
spans: []
|
|
3466
4339
|
};
|
|
3467
|
-
|
|
4340
|
+
getRoot().children.push(text);
|
|
3468
4341
|
} else if (accept("<", status, false)) {
|
|
3469
|
-
|
|
3470
|
-
type: "root",
|
|
3471
|
-
children: []
|
|
3472
|
-
};
|
|
4342
|
+
setRoot(getRoot());
|
|
3473
4343
|
const element = parseElement(status);
|
|
3474
|
-
|
|
4344
|
+
getRoot().children.push(element);
|
|
3475
4345
|
} else if (accept("@", status, false)) {
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
children: []
|
|
3479
|
-
};
|
|
3480
|
-
parseControl(status, current.markup);
|
|
4346
|
+
setRoot(getRoot());
|
|
4347
|
+
parseControl(status, getRoot());
|
|
3481
4348
|
} else status.i += 1;
|
|
3482
4349
|
}
|
|
3483
|
-
|
|
3484
4350
|
//#endregion
|
|
3485
4351
|
//#region src/compile/utils/hash.ts
|
|
3486
4352
|
const returnRegex = /\r/g;
|
|
3487
4353
|
function hash(input) {
|
|
3488
4354
|
input = input.replace(returnRegex, "");
|
|
3489
|
-
let hash
|
|
4355
|
+
let hash = 5381;
|
|
3490
4356
|
let i = input.length;
|
|
3491
|
-
while (i--) hash
|
|
3492
|
-
return (hash
|
|
4357
|
+
while (i--) hash = (hash << 5) - hash ^ input.charCodeAt(i);
|
|
4358
|
+
return (hash >>> 0).toString(36);
|
|
3493
4359
|
}
|
|
3494
|
-
|
|
3495
4360
|
//#endregion
|
|
3496
4361
|
//#region src/compile/parse/utils/expect.ts
|
|
3497
4362
|
/**
|
|
@@ -3513,7 +4378,6 @@ function expect(value, status, advance = true) {
|
|
|
3513
4378
|
else addError(status, "Expected token");
|
|
3514
4379
|
return false;
|
|
3515
4380
|
}
|
|
3516
|
-
|
|
3517
4381
|
//#endregion
|
|
3518
4382
|
//#region src/compile/parse/parseStyles.ts
|
|
3519
4383
|
function parseStyles(source, status) {
|
|
@@ -3521,7 +4385,7 @@ function parseStyles(source, status) {
|
|
|
3521
4385
|
if (!current) return;
|
|
3522
4386
|
current.style = {
|
|
3523
4387
|
global: false,
|
|
3524
|
-
|
|
4388
|
+
children: [],
|
|
3525
4389
|
hash: hash(source)
|
|
3526
4390
|
};
|
|
3527
4391
|
const styleStatus = {
|
|
@@ -3529,6 +4393,7 @@ function parseStyles(source, status) {
|
|
|
3529
4393
|
i: 0,
|
|
3530
4394
|
marker: 0,
|
|
3531
4395
|
level: 0,
|
|
4396
|
+
braces: [],
|
|
3532
4397
|
imports: [],
|
|
3533
4398
|
script: [],
|
|
3534
4399
|
components: [],
|
|
@@ -3537,69 +4402,86 @@ function parseStyles(source, status) {
|
|
|
3537
4402
|
};
|
|
3538
4403
|
while (styleStatus.i < styleStatus.source.length) if (isSpaceChar(styleStatus.source, styleStatus.i)) consumeSpace(styleStatus);
|
|
3539
4404
|
else {
|
|
3540
|
-
const
|
|
3541
|
-
if (
|
|
4405
|
+
const node = parseStyleNode(styleStatus);
|
|
4406
|
+
if (node) current.style.children.push(node);
|
|
3542
4407
|
else break;
|
|
3543
4408
|
}
|
|
3544
4409
|
status.errors = status.errors.concat(styleStatus.errors);
|
|
3545
4410
|
}
|
|
3546
|
-
function
|
|
4411
|
+
function parseStyleNode(status) {
|
|
4412
|
+
const gapBefore = /\n\s*\n\s*$/.test(status.source.slice(0, status.i));
|
|
4413
|
+
if (accept("//", status, false) || accept("/*", status, false)) return parseComment(status, gapBefore);
|
|
3547
4414
|
const selector = consumeUntil("{", status).trim();
|
|
3548
4415
|
if (expect("{", status)) {
|
|
3549
4416
|
const block = {
|
|
4417
|
+
type: "block",
|
|
3550
4418
|
selector,
|
|
3551
|
-
|
|
3552
|
-
|
|
4419
|
+
children: [],
|
|
4420
|
+
gapBefore
|
|
3553
4421
|
};
|
|
3554
4422
|
consumeSpace(status);
|
|
3555
4423
|
while (status.source[status.i] !== "}") {
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
if (
|
|
3566
|
-
|
|
3567
|
-
|
|
4424
|
+
if (accept("//", status, false) || accept("/*", status, false)) block.children.push(parseComment(status, false));
|
|
4425
|
+
else {
|
|
4426
|
+
let nextColon = status.source.indexOf(":", status.i);
|
|
4427
|
+
if (nextColon === -1) nextColon = status.source.length;
|
|
4428
|
+
let nextOpenBrace = status.source.indexOf("{", status.i);
|
|
4429
|
+
if (nextOpenBrace === -1) nextOpenBrace = status.source.length;
|
|
4430
|
+
if (nextColon < nextOpenBrace) {
|
|
4431
|
+
const attribute = parseStyleAttribute(status);
|
|
4432
|
+
block.children.push(attribute);
|
|
4433
|
+
} else if (nextOpenBrace < nextColon) {
|
|
4434
|
+
const child = parseStyleNode(status);
|
|
4435
|
+
if (child) block.children.push(child);
|
|
4436
|
+
else break;
|
|
4437
|
+
} else return block;
|
|
4438
|
+
}
|
|
3568
4439
|
consumeSpace(status);
|
|
3569
4440
|
}
|
|
3570
4441
|
accept("}", status);
|
|
3571
4442
|
return block;
|
|
3572
4443
|
}
|
|
3573
4444
|
}
|
|
4445
|
+
function parseComment(status, gapBefore) {
|
|
4446
|
+
const start = status.i;
|
|
4447
|
+
if (accept("//", status)) {
|
|
4448
|
+
const end = status.source.indexOf("\n", status.i);
|
|
4449
|
+
status.i = end === -1 ? status.source.length : end;
|
|
4450
|
+
} else {
|
|
4451
|
+
accept("/*", status);
|
|
4452
|
+
const end = status.source.indexOf("*/", status.i);
|
|
4453
|
+
status.i = end === -1 ? status.source.length : end + 2;
|
|
4454
|
+
}
|
|
4455
|
+
return {
|
|
4456
|
+
type: "comment",
|
|
4457
|
+
content: status.source.substring(start, status.i),
|
|
4458
|
+
gapBefore
|
|
4459
|
+
};
|
|
4460
|
+
}
|
|
3574
4461
|
function parseStyleAttribute(status) {
|
|
4462
|
+
const gapBefore = /\n\s*\n\s*$/.test(status.source.slice(0, status.i));
|
|
3575
4463
|
const name = consumeUntil(":", status).trim();
|
|
3576
4464
|
accept(":", status);
|
|
3577
4465
|
const value = consumeUntil(";", status).trim();
|
|
3578
4466
|
accept(";", status);
|
|
3579
4467
|
return {
|
|
4468
|
+
type: "attribute",
|
|
3580
4469
|
name,
|
|
3581
|
-
value
|
|
4470
|
+
value,
|
|
4471
|
+
gapBefore
|
|
3582
4472
|
};
|
|
3583
4473
|
}
|
|
3584
|
-
|
|
3585
4474
|
//#endregion
|
|
3586
4475
|
//#region src/compile/utils/isComponentNode.ts
|
|
3587
4476
|
function isComponentNode(node) {
|
|
3588
4477
|
return node.type === "component";
|
|
3589
4478
|
}
|
|
3590
|
-
|
|
3591
|
-
//#endregion
|
|
3592
|
-
//#region src/compile/utils/isParentNode.ts
|
|
3593
|
-
function isParentNode(node) {
|
|
3594
|
-
return "children" in node;
|
|
3595
|
-
}
|
|
3596
|
-
|
|
3597
4479
|
//#endregion
|
|
3598
4480
|
//#region src/compile/parse/scopeStyles.ts
|
|
3599
4481
|
function scopeStyles(status) {
|
|
3600
4482
|
let selectors = [];
|
|
3601
4483
|
for (let component of status.components) if (component.style && component.markup) {
|
|
3602
|
-
for (let block of component.style.
|
|
4484
|
+
for (let block of component.style.children) if (block.type === "block") collectStyleSelectors(block, selectors);
|
|
3603
4485
|
scopeStylesOnNode(component.markup, selectors);
|
|
3604
4486
|
}
|
|
3605
4487
|
}
|
|
@@ -3617,7 +4499,6 @@ function scopeStylesOnNode(node, selectors) {
|
|
|
3617
4499
|
function collectStyleSelectors(block, selectors) {
|
|
3618
4500
|
for (let s of block.selector.split(/[\s*,>+~[\]]/)) selectors.push(s);
|
|
3619
4501
|
}
|
|
3620
|
-
|
|
3621
4502
|
//#endregion
|
|
3622
4503
|
//#region src/compile/parse/parseCode.ts
|
|
3623
4504
|
function parseCode(source) {
|
|
@@ -3626,16 +4507,24 @@ function parseCode(source) {
|
|
|
3626
4507
|
i: 0,
|
|
3627
4508
|
marker: 0,
|
|
3628
4509
|
level: 0,
|
|
4510
|
+
braces: [],
|
|
3629
4511
|
imports: [],
|
|
3630
4512
|
script: [],
|
|
3631
4513
|
components: [],
|
|
3632
4514
|
stack: [],
|
|
3633
4515
|
errors: []
|
|
3634
4516
|
};
|
|
3635
|
-
for (status.i; status.i < source.length; status.i++) if (consumeScriptComments(status)) {} else if (status.source[status.i] === "{")
|
|
3636
|
-
|
|
4517
|
+
for (status.i; status.i < source.length; status.i++) if (consumeScriptComments(status)) {} else if (status.source[status.i] === "{") {
|
|
4518
|
+
status.level += 1;
|
|
4519
|
+
status.braces.push(false);
|
|
4520
|
+
} else if (status.source[status.i] === "}") {
|
|
3637
4521
|
status.level -= 1;
|
|
3638
|
-
|
|
4522
|
+
const fromComponent = status.braces.pop();
|
|
4523
|
+
if (status.level === 0 && fromComponent) parseComponentEnd(status);
|
|
4524
|
+
else if (status.level < 0) {
|
|
4525
|
+
addError(status, `Unexpected closing brace`, status.i, status.i + 1);
|
|
4526
|
+
status.level = 0;
|
|
4527
|
+
}
|
|
3639
4528
|
} else if (accept("function", status)) {
|
|
3640
4529
|
if (consumeSpace(status)) if (/[A-Z]/.test(source[status.i])) parseComponentStart(status);
|
|
3641
4530
|
else status.i--;
|
|
@@ -3657,8 +4546,13 @@ function parseCode(source) {
|
|
|
3657
4546
|
if (consumeSpace(status) && accept("default", status) && consumeSpace(status) && accept("(", status, false)) parseComponentStart(status);
|
|
3658
4547
|
else status.i = start;
|
|
3659
4548
|
} else if (accept("@render", status, false)) parseComponentRender(status);
|
|
4549
|
+
else if (accept("@error", status, false)) parseComponentError(status);
|
|
3660
4550
|
else if (accept("@head", status, false)) parseComponentHead(status);
|
|
3661
4551
|
else if (accept("@style", status, false)) parseComponentStyle(status);
|
|
4552
|
+
if (status.level > 0) {
|
|
4553
|
+
const count = status.level;
|
|
4554
|
+
addError(status, `Unbalanced braces: missing ${count} closing brace${count === 1 ? "" : "s"}`, source.length, source.length);
|
|
4555
|
+
}
|
|
3662
4556
|
status.script.push({
|
|
3663
4557
|
script: source.substring(status.marker, source.length),
|
|
3664
4558
|
span: {
|
|
@@ -3685,13 +4579,33 @@ function parseComponentStart(status) {
|
|
|
3685
4579
|
let def = /export\s+default\s+(function\s+|const\s+)*$/.test(source);
|
|
3686
4580
|
let isConst = /const\s+$/.test(source);
|
|
3687
4581
|
let isAnon = /export default\s+$/.test(source);
|
|
4582
|
+
let componentStart = status.i;
|
|
4583
|
+
let name = consumeAlphaNumeric(status);
|
|
4584
|
+
if (def && status.components.some((c) => c.default)) addError(status, `Multiple default exports`, componentStart, componentStart + name.length);
|
|
3688
4585
|
const current = {
|
|
3689
|
-
start:
|
|
3690
|
-
name
|
|
4586
|
+
start: componentStart,
|
|
4587
|
+
name,
|
|
3691
4588
|
exported,
|
|
3692
|
-
default: def
|
|
4589
|
+
default: def,
|
|
4590
|
+
scriptStart: status.script.length
|
|
3693
4591
|
};
|
|
3694
4592
|
status.components.push(current);
|
|
4593
|
+
if (status.source[status.i] === "<") {
|
|
4594
|
+
const typeParamsStart = status.i;
|
|
4595
|
+
let level = 0;
|
|
4596
|
+
for (status.i; status.i < status.source.length; status.i++) {
|
|
4597
|
+
const char = status.source[status.i];
|
|
4598
|
+
if (char === "<") level += 1;
|
|
4599
|
+
else if (char === ">") {
|
|
4600
|
+
level -= 1;
|
|
4601
|
+
if (level === 0) {
|
|
4602
|
+
current.typeParams = status.source.substring(typeParamsStart, status.i + 1);
|
|
4603
|
+
status.i += 1;
|
|
4604
|
+
break;
|
|
4605
|
+
}
|
|
4606
|
+
}
|
|
4607
|
+
}
|
|
4608
|
+
}
|
|
3695
4609
|
let presource = source.substring(0, status.i).trimEnd();
|
|
3696
4610
|
let lastNewline = presource.lastIndexOf("\n");
|
|
3697
4611
|
if (lastNewline !== -1) {
|
|
@@ -3738,8 +4652,8 @@ function parseComponentStart(status) {
|
|
|
3738
4652
|
const paramsMatch = current.params?.match(/\$props:\s*([$A-Za-z0-9]+)/);
|
|
3739
4653
|
if (paramsMatch) {
|
|
3740
4654
|
let propsTypeName = paramsMatch[1];
|
|
3741
|
-
let propsTypeStart = status.source.search(
|
|
3742
|
-
if (propsTypeStart === -1) propsTypeStart = status.source.search(
|
|
4655
|
+
let propsTypeStart = status.source.search(new RegExp(`interface\\s+${propsTypeName}`));
|
|
4656
|
+
if (propsTypeStart === -1) propsTypeStart = status.source.search(new RegExp(`type\\s+${propsTypeName}`));
|
|
3743
4657
|
if (propsTypeStart !== -1) {
|
|
3744
4658
|
let bracesStart = status.source.indexOf("{", propsTypeStart);
|
|
3745
4659
|
if (bracesStart !== -1) {
|
|
@@ -3748,6 +4662,7 @@ function parseComponentStart(status) {
|
|
|
3748
4662
|
i: bracesStart + 1,
|
|
3749
4663
|
marker: 0,
|
|
3750
4664
|
level: 0,
|
|
4665
|
+
braces: [],
|
|
3751
4666
|
imports: [],
|
|
3752
4667
|
script: [],
|
|
3753
4668
|
components: [],
|
|
@@ -3768,6 +4683,7 @@ function parseComponentStart(status) {
|
|
|
3768
4683
|
}
|
|
3769
4684
|
accept("{", status);
|
|
3770
4685
|
status.level += 1;
|
|
4686
|
+
status.braces.push(true);
|
|
3771
4687
|
const postFunctionChunk = {
|
|
3772
4688
|
script: status.source.substring(status.marker, status.i),
|
|
3773
4689
|
span: {
|
|
@@ -3822,6 +4738,42 @@ function parseComponentRender(status) {
|
|
|
3822
4738
|
}
|
|
3823
4739
|
status.marker = status.i;
|
|
3824
4740
|
}
|
|
4741
|
+
function parseComponentError(status) {
|
|
4742
|
+
const current = status.components.at(-1);
|
|
4743
|
+
if (!current) return;
|
|
4744
|
+
if (current.error) addError(status, `Multiple @error sections`, status.i, status.i + 6);
|
|
4745
|
+
if (!current.markup) addError(status, `@error must come after @render`, status.i, status.i + 6);
|
|
4746
|
+
status.script.push({
|
|
4747
|
+
script: status.source.substring(status.marker, status.i),
|
|
4748
|
+
span: {
|
|
4749
|
+
start: status.marker,
|
|
4750
|
+
end: status.i
|
|
4751
|
+
}
|
|
4752
|
+
});
|
|
4753
|
+
status.script.push({
|
|
4754
|
+
script: "/* @error */",
|
|
4755
|
+
span: {
|
|
4756
|
+
start: 0,
|
|
4757
|
+
end: 0
|
|
4758
|
+
}
|
|
4759
|
+
});
|
|
4760
|
+
accept("@error", status);
|
|
4761
|
+
consumeSpace(status);
|
|
4762
|
+
let errorVar = "";
|
|
4763
|
+
if (accept("(", status)) {
|
|
4764
|
+
let start = status.i;
|
|
4765
|
+
while (status.i < status.source.length && status.source[status.i] !== ")") status.i += 1;
|
|
4766
|
+
errorVar = status.source.substring(start, status.i).trim();
|
|
4767
|
+
accept(")", status);
|
|
4768
|
+
consumeSpace(status);
|
|
4769
|
+
}
|
|
4770
|
+
current.errorVar = errorVar || "err";
|
|
4771
|
+
if (accept("{", status)) {
|
|
4772
|
+
parseMarkupInto(status, status.source, (root) => current.error ??= root);
|
|
4773
|
+
accept("}", status);
|
|
4774
|
+
}
|
|
4775
|
+
status.marker = status.i;
|
|
4776
|
+
}
|
|
3825
4777
|
function parseComponentHead(status) {
|
|
3826
4778
|
const current = status.components.at(-1);
|
|
3827
4779
|
if (!current) return;
|
|
@@ -3875,16 +4827,22 @@ function parseComponentStyle(status) {
|
|
|
3875
4827
|
for (; status.i < status.source.length; status.i++) {
|
|
3876
4828
|
const char = status.source[status.i];
|
|
3877
4829
|
const nextChar = status.source[status.i + 1];
|
|
3878
|
-
if (char === "/" && nextChar === "/")
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
|
|
3883
|
-
|
|
4830
|
+
if (char === "/" && nextChar === "/") {
|
|
4831
|
+
let start = status.i;
|
|
4832
|
+
status.i = status.source.indexOf("\n", status.i);
|
|
4833
|
+
styleSource += status.source.substring(start, status.i + 1);
|
|
4834
|
+
} else if (char === "/" && nextChar === "*") {
|
|
4835
|
+
let start = status.i;
|
|
4836
|
+
status.i = status.source.indexOf("*/", status.i) + 1;
|
|
4837
|
+
styleSource += status.source.substring(start, status.i + 1);
|
|
4838
|
+
} else if (char === "\"" || char === "'") {
|
|
4839
|
+
let start = status.i;
|
|
4840
|
+
status.i = endOfString(char, status.source, start);
|
|
4841
|
+
styleSource += status.source.substring(start, status.i + 1);
|
|
3884
4842
|
} else if (char === "`") {
|
|
3885
|
-
let start
|
|
3886
|
-
status.i = endOfTemplateString(status.source, start
|
|
3887
|
-
styleSource += status.source.substring(start
|
|
4843
|
+
let start = status.i;
|
|
4844
|
+
status.i = endOfTemplateString(status.source, start);
|
|
4845
|
+
styleSource += status.source.substring(start, status.i + 1);
|
|
3888
4846
|
} else if (char === "{") {
|
|
3889
4847
|
level += 1;
|
|
3890
4848
|
if (start === -1) start = status.i + 1;
|
|
@@ -3919,49 +4877,80 @@ function parseComponentEnd(status) {
|
|
|
3919
4877
|
end: 0
|
|
3920
4878
|
}
|
|
3921
4879
|
});
|
|
3922
|
-
const
|
|
3923
|
-
|
|
3924
|
-
current.
|
|
4880
|
+
const scriptText = status.script.slice(current.scriptStart ?? status.script.length).map((chunk) => chunk.script).join("\n");
|
|
4881
|
+
const markupParts = [];
|
|
4882
|
+
if (current.markup) markupParts.push(collectMarkupExpressions(current.markup));
|
|
4883
|
+
if (current.error) markupParts.push(collectMarkupExpressions(current.error));
|
|
4884
|
+
if (current.head) markupParts.push(collectMarkupExpressions(current.head));
|
|
4885
|
+
const usageSource = scriptText + "\n" + markupParts.join("\n");
|
|
4886
|
+
const usageRanges = codeRanges(usageSource);
|
|
4887
|
+
current.props = getPropsUsage(usageSource, usageRanges);
|
|
4888
|
+
current.contextProps = getContextUsage(usageSource, usageRanges);
|
|
3925
4889
|
if (current.markup && isElementNode(current.markup.children[0]) && current.markup.children[0].tagName === "html" && isSpaceNode(current.markup.children[0].children[0])) current.markup.children.splice(1, 1);
|
|
3926
4890
|
status.marker = status.i;
|
|
3927
4891
|
}
|
|
3928
|
-
|
|
3929
|
-
|
|
4892
|
+
/**
|
|
4893
|
+
* Gets the `$props` names that the compiled output references. The input is
|
|
4894
|
+
* the component's code-only text (script plus markup expressions), with
|
|
4895
|
+
* `ranges` marking the positions that are code — matches that start inside a
|
|
4896
|
+
* string, comment or regex literal are ignored, so mentions inside
|
|
4897
|
+
* sample-code strings or prose don't count.
|
|
4898
|
+
*/
|
|
4899
|
+
function getPropsUsage(source, ranges) {
|
|
3930
4900
|
const props = [];
|
|
3931
|
-
for (let match of
|
|
4901
|
+
for (let match of source.matchAll(/\$props\??\s*(?:\.([\d\w]+)|\[([^\]]+)\])/g)) {
|
|
4902
|
+
if (!isInCode(ranges, match.index)) continue;
|
|
3932
4903
|
const name = trimQuotes(match[1] || match[2]);
|
|
3933
4904
|
if (!props.includes(name)) props.push(name);
|
|
3934
4905
|
}
|
|
4906
|
+
if (!props.length) for (let match of source.matchAll(/(?<![\w$])\$props(?![\w])(?!\s*:)(?!<)/g)) {
|
|
4907
|
+
if (!isInCode(ranges, match.index)) continue;
|
|
4908
|
+
props.push("$props");
|
|
4909
|
+
break;
|
|
4910
|
+
}
|
|
3935
4911
|
return props.length ? props : void 0;
|
|
3936
4912
|
}
|
|
3937
|
-
|
|
3938
|
-
|
|
4913
|
+
/**
|
|
4914
|
+
* Gets the `$context` names that the compiled output references. The input is
|
|
4915
|
+
* the component's code-only text (script plus markup expressions), with
|
|
4916
|
+
* `ranges` marking the positions that are code — matches that start inside a
|
|
4917
|
+
* string, comment or regex literal are ignored, so mentions inside
|
|
4918
|
+
* sample-code strings or prose don't count.
|
|
4919
|
+
*/
|
|
4920
|
+
function getContextUsage(source, ranges) {
|
|
4921
|
+
const contextsMatches = source.matchAll(/\$context\??\s*(?:\.([\d\w]+)|\[([^\]]+)\])/g);
|
|
3939
4922
|
const contexts = [];
|
|
3940
4923
|
for (let match of contextsMatches) {
|
|
4924
|
+
if (!isInCode(ranges, match.index)) continue;
|
|
3941
4925
|
const name = trimQuotes(match[1] || match[2]);
|
|
3942
4926
|
if (!contexts.includes(name)) contexts.push(name);
|
|
3943
4927
|
}
|
|
4928
|
+
if (!contexts.length) for (let match of source.matchAll(/(?<![\w$])\$context(?![\w])(?!\s*:)(?!<)/g)) {
|
|
4929
|
+
if (!isInCode(ranges, match.index)) continue;
|
|
4930
|
+
contexts.push("$context");
|
|
4931
|
+
break;
|
|
4932
|
+
}
|
|
3944
4933
|
return contexts.length ? contexts : void 0;
|
|
3945
4934
|
}
|
|
4935
|
+
/**
|
|
4936
|
+
* Returns true if `index` falls inside one of the (sorted, non-overlapping)
|
|
4937
|
+
* code ranges — i.e. not inside a string, comment or regex literal.
|
|
4938
|
+
*/
|
|
4939
|
+
function isInCode(ranges, index) {
|
|
4940
|
+
for (const [start, end] of ranges) {
|
|
4941
|
+
if (index >= start && index < end) return true;
|
|
4942
|
+
if (index < start) return false;
|
|
4943
|
+
}
|
|
4944
|
+
return false;
|
|
4945
|
+
}
|
|
3946
4946
|
function consumeScriptComments(status) {
|
|
3947
|
-
const
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
status.i = status.source.indexOf("\n", status.i);
|
|
3951
|
-
return true;
|
|
3952
|
-
} else if (char === "/" && nextChar === "*") {
|
|
3953
|
-
status.i = status.source.indexOf("*/", status.i) + 1;
|
|
3954
|
-
return true;
|
|
3955
|
-
} else if (char === "\"" || char === "'") {
|
|
3956
|
-
status.i = endOfString(char, status.source, status.i);
|
|
3957
|
-
return true;
|
|
3958
|
-
} else if (char === "`") {
|
|
3959
|
-
status.i = endOfTemplateString(status.source, status.i);
|
|
4947
|
+
const skipped = skipStringOrComment(status.source, status.i);
|
|
4948
|
+
if (skipped !== -1) {
|
|
4949
|
+
status.i = skipped;
|
|
3960
4950
|
return true;
|
|
3961
4951
|
}
|
|
3962
4952
|
return false;
|
|
3963
4953
|
}
|
|
3964
|
-
|
|
3965
4954
|
//#endregion
|
|
3966
4955
|
//#region src/compile/parse.ts
|
|
3967
4956
|
/**
|
|
@@ -3975,8 +4964,7 @@ function consumeScriptComments(status) {
|
|
|
3975
4964
|
function parse(source) {
|
|
3976
4965
|
return parseCode(source);
|
|
3977
4966
|
}
|
|
3978
|
-
|
|
3979
4967
|
//#endregion
|
|
3980
4968
|
exports.build = build;
|
|
3981
4969
|
exports.buildType = buildType;
|
|
3982
|
-
exports.parse = parse;
|
|
4970
|
+
exports.parse = parse;
|