olum-compiler 1.2.0 → 1.6.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/.claude/settings.local.json +3 -1
- package/bin/build.js +14 -8
- package/bin/bundle.js +108 -0
- package/bin/compiler.js +25 -7
- package/bin/copySrc.js +73 -25
- package/bin/dev.js +54 -34
- package/bin/index.js +6 -4
- package/bin/server.js +58 -34
- package/bin/watcher.js +15 -11
- package/bin/ws.js +16 -11
- package/lib/attr.js +1 -25
- package/lib/boolAttr.js +32 -15
- package/lib/changes.txt +0 -0
- package/lib/chokidar.js +52 -101
- package/lib/colors.js +1 -7
- package/lib/compPlaceholder.js +5 -3
- package/lib/compiler.js +11 -8
- package/lib/coreLibs.js +2 -7
- package/lib/cross-spawn.js +45 -59
- package/lib/cssParser.js +61 -37
- package/lib/domEvents.js +6 -2
- package/lib/events.js +75 -77
- package/lib/for.js +56 -28
- package/lib/getImports.js +35 -17
- package/lib/getTempPath.js +8 -4
- package/lib/helpers.js +289 -184
- package/lib/html.js +3 -10
- package/lib/htmlParser.js +45 -45
- package/lib/if.js +40 -20
- package/lib/importMap.js +49 -24
- package/lib/jsParser.js +8 -13
- package/lib/lineCounter.js +18 -5
- package/lib/liveProps.js +266 -0
- package/lib/log.js +30 -0
- package/lib/magic-string.js +45 -52
- package/lib/models.js +8 -7
- package/lib/parser.js +82 -43
- package/lib/placeholder.js +73 -58
- package/lib/regex.js +4 -3
- package/lib/show.js +16 -8
- package/lib/sourcemap.js +23 -24
- package/lib/styles.js +7 -13
- package/lib/textarea.js +3 -6
- package/lib/toJs.js +1 -1
- package/lib/tokens.js +10 -22
- package/lib/transition.js +86 -0
- package/lib/websocket.js +68 -62
- package/package.json +1 -1
- package/tests/compiler.test.js +450 -172
package/tests/compiler.test.js
CHANGED
|
@@ -1,21 +1,8 @@
|
|
|
1
|
-
// ============================================================================
|
|
2
|
-
// OlumJS compiler test suite — one (or more) fixture per rule in docs.md.
|
|
3
|
-
//
|
|
4
|
-
// The compiler is a pure function: parser(templateString, id, filePath) -> JS string.
|
|
5
|
-
// Each `check` compiles a template and asserts on the emitted JS. Sections below
|
|
6
|
-
// map 1:1 to docs.md so the docs and the compiler can't drift apart.
|
|
7
|
-
//
|
|
8
|
-
// Run: node tests/compiler.test.js (or: npm test)
|
|
9
|
-
// ============================================================================
|
|
10
|
-
|
|
11
1
|
const parser = require("../lib/parser");
|
|
12
2
|
|
|
13
3
|
let passed = 0;
|
|
14
4
|
let failed = 0;
|
|
15
5
|
|
|
16
|
-
// ── Output styling ──────────────────────────────────────────────────────────
|
|
17
|
-
// ANSI colors + emoji icons. Disabled automatically when the output isn't a TTY
|
|
18
|
-
// (e.g. piped to a file or CI log) or when NO_COLOR is set, so logs stay clean.
|
|
19
6
|
const COLOR = process.stdout.isTTY && !process.env.NO_COLOR;
|
|
20
7
|
const paint = (code, s) => (COLOR ? `\x1b[${code}m${s}\x1b[0m` : s);
|
|
21
8
|
const green = (s) => paint("32", s);
|
|
@@ -27,31 +14,26 @@ const cyan = (s) => paint("36", s);
|
|
|
27
14
|
const PASS_ICON = green("✔");
|
|
28
15
|
const FAIL_ICON = red("✖");
|
|
29
16
|
|
|
30
|
-
// Deterministic, monotonically-increasing ids so a run is fully reproducible
|
|
31
|
-
// (a flaky failure can be re-run and debugged with the exact same id in the output).
|
|
32
17
|
let _idSeq = 1000;
|
|
33
18
|
function compile(template) {
|
|
34
19
|
return parser(template, _idSeq++, "App.html");
|
|
35
20
|
}
|
|
36
21
|
|
|
37
|
-
// compiled output is a parseable JS module (after stripping import/export)
|
|
38
22
|
function parses(out) {
|
|
39
23
|
const body = out
|
|
40
24
|
.replace(/^\s*import .*$/gm, "")
|
|
41
25
|
.replace(/^\s*export\s+default\s+/m, "const __x=");
|
|
42
|
-
new Function(body);
|
|
26
|
+
new Function(body);
|
|
43
27
|
return true;
|
|
44
28
|
}
|
|
45
29
|
|
|
46
|
-
// the rendered template literal region (between `el.innerHTML = ` and the injectStyle call)
|
|
47
30
|
function tmpl(out) {
|
|
48
31
|
const m = out.match(/innerHTML = `([\s\S]*?)`;\s*olum\.injectStyle/);
|
|
49
32
|
return m ? m[1] : "";
|
|
50
33
|
}
|
|
51
34
|
|
|
52
|
-
// Current section, tracked so a failure can point at the category to look into.
|
|
53
35
|
let currentSection = "(no section)";
|
|
54
|
-
const failedSections = [];
|
|
36
|
+
const failedSections = [];
|
|
55
37
|
|
|
56
38
|
function recordFail(name) {
|
|
57
39
|
failed++;
|
|
@@ -64,7 +46,9 @@ function check(name, template, assertion) {
|
|
|
64
46
|
out = compile(template);
|
|
65
47
|
} catch (e) {
|
|
66
48
|
recordFail(name);
|
|
67
|
-
console.log(
|
|
49
|
+
console.log(
|
|
50
|
+
" " + FAIL_ICON + " " + name + dim(" (threw: " + e.message + ")"),
|
|
51
|
+
);
|
|
68
52
|
console.log(" " + dim("↳ in " + currentSection));
|
|
69
53
|
return;
|
|
70
54
|
}
|
|
@@ -80,8 +64,7 @@ function check(name, template, assertion) {
|
|
|
80
64
|
console.log(" " + PASS_ICON + " " + name);
|
|
81
65
|
} else {
|
|
82
66
|
recordFail(name);
|
|
83
|
-
|
|
84
|
-
// failure is diagnosable without re-instrumenting the test.
|
|
67
|
+
|
|
85
68
|
if (!detail) {
|
|
86
69
|
try {
|
|
87
70
|
const snippet = tmpl(out).trim().replace(/\s+/g, " ").slice(0, 140);
|
|
@@ -100,435 +83,577 @@ function section(title) {
|
|
|
100
83
|
console.log("\n" + bold(cyan(title)));
|
|
101
84
|
}
|
|
102
85
|
|
|
103
|
-
// Build a component from a template + optional extra script lines. The <script> MUST be
|
|
104
|
-
// multiline (opening/closing tags on their own lines) — a single-line <script>…</script>
|
|
105
|
-
// hits a jsParser regex quirk that strips the whole body (real components are multiline).
|
|
106
86
|
const STATE = `const state = { count: 0, name: "Ann", cls: "on", items: [{ id: 1, name: "a" }], settings: { a: 1 }, html: "<b>x</b>", color: "#fff" };`;
|
|
107
87
|
function comp(template, extraScript) {
|
|
108
88
|
return `<script>\n${STATE}\n${extraScript || ""}\n</script>\n${template}`;
|
|
109
89
|
}
|
|
110
90
|
|
|
111
|
-
console.log(
|
|
91
|
+
console.log(
|
|
92
|
+
"\n" + bold("🧪 OlumJS compiler fixtures") + "\n========================",
|
|
93
|
+
);
|
|
112
94
|
|
|
113
|
-
// ── §1 Component file structure ────────────────────────────────────────────
|
|
114
95
|
section("§1 Component file structure");
|
|
115
96
|
|
|
116
97
|
check(
|
|
117
98
|
"compiles to a parseable JS module",
|
|
118
99
|
comp(`<div class="box">{state.count}</div>`),
|
|
119
|
-
(out) => parses(out)
|
|
100
|
+
(out) => parses(out),
|
|
120
101
|
);
|
|
121
102
|
|
|
122
|
-
check(
|
|
123
|
-
|
|
124
|
-
comp(`<div></div>`),
|
|
125
|
-
(out) => /export default \(_instanceKey\) =>/.test(out)
|
|
103
|
+
check("exports a default factory", comp(`<div></div>`), (out) =>
|
|
104
|
+
/export default \(_instanceKey\) =>/.test(out),
|
|
126
105
|
);
|
|
127
106
|
|
|
128
|
-
// ── §3 State & reactivity ──────────────────────────────────────────────────
|
|
129
107
|
section("§3 State & reactivity");
|
|
130
108
|
|
|
131
109
|
check(
|
|
132
110
|
"state is wrapped in olum.proxyHandler (reactive)",
|
|
133
111
|
comp(`<div></div>`),
|
|
134
|
-
(out) => /state = olum\.proxyHandler\(state,/.test(out) && parses(out)
|
|
112
|
+
(out) => /state = olum\.proxyHandler\(state,/.test(out) && parses(out),
|
|
135
113
|
);
|
|
136
114
|
|
|
137
|
-
// ── §4 Text interpolation {expr} (auto-escaped) ────────────────────────────
|
|
138
115
|
section("§4 Text interpolation");
|
|
139
116
|
|
|
140
117
|
check(
|
|
141
118
|
"{expr} in text is wrapped in olum.esc",
|
|
142
119
|
comp(`<p>{state.name}</p>`),
|
|
143
|
-
(out) => /\$\{olum\.esc\(state\.name\)\}/.test(tmpl(out))
|
|
120
|
+
(out) => /\$\{olum\.esc\(state\.name\)\}/.test(tmpl(out)),
|
|
144
121
|
);
|
|
145
122
|
|
|
146
123
|
check(
|
|
147
124
|
"expression with operators interpolates as-is (escaped)",
|
|
148
125
|
comp(`<p>{state.count + 1}</p>`),
|
|
149
|
-
(out) =>
|
|
126
|
+
(out) =>
|
|
127
|
+
/\$\{olum\.esc\(state\.count \+ 1\)\}/.test(tmpl(out)) && parses(out),
|
|
150
128
|
);
|
|
151
129
|
|
|
152
|
-
// jsdom entity-encodes <,>,& in text nodes on serialize — the escape pass must decode them
|
|
153
|
-
// back inside the captured expr or the emitted JS is broken (`width > 380` → SyntaxError)
|
|
154
130
|
check(
|
|
155
131
|
"comparison operators in a text {expr} survive the jsdom round-trip",
|
|
156
132
|
comp(`<p>{state.width > 380 && state.width < 800 ? 'mid' : 'other'}</p>`),
|
|
157
|
-
(out) =>
|
|
133
|
+
(out) =>
|
|
134
|
+
/olum\.esc\(state\.width > 380 && state\.width < 800 \? 'mid' : 'other'\)/.test(
|
|
135
|
+
tmpl(out),
|
|
136
|
+
) && parses(out),
|
|
158
137
|
);
|
|
159
138
|
|
|
160
|
-
// ── §5 String attributes (literal + {expr}) ────────────────────────────────
|
|
161
139
|
section("§5 String attributes");
|
|
162
140
|
|
|
163
141
|
check(
|
|
164
142
|
"static attribute stays a literal string (no esc)",
|
|
165
143
|
comp(`<img alt="avatar" src="/logo.png" />`),
|
|
166
|
-
(out) =>
|
|
144
|
+
(out) =>
|
|
145
|
+
/alt="avatar"/.test(tmpl(out)) && !/olum\.esc\('avatar'\)/.test(tmpl(out)),
|
|
167
146
|
);
|
|
168
147
|
|
|
169
148
|
check(
|
|
170
149
|
"{expr} inside a string attribute interpolates",
|
|
171
150
|
comp(`<div class="card {state.cls}"></div>`),
|
|
172
|
-
(out) =>
|
|
151
|
+
(out) =>
|
|
152
|
+
/class="card \$\{olum\.esc\(state\.cls\)\}"/.test(tmpl(out)) && parses(out),
|
|
173
153
|
);
|
|
174
154
|
|
|
175
155
|
check(
|
|
176
156
|
"dynamic string style interpolates",
|
|
177
157
|
comp(`<div style="color:{state.color}; padding:8px;"></div>`),
|
|
178
|
-
(out) =>
|
|
158
|
+
(out) =>
|
|
159
|
+
/style="color:\$\{olum\.esc\(state\.color\)\}; padding:8px;"/.test(
|
|
160
|
+
tmpl(out),
|
|
161
|
+
) && parses(out),
|
|
179
162
|
);
|
|
180
163
|
|
|
181
|
-
// textarea has no `value` attribute in HTML (its value is its text content), so a
|
|
182
|
-
// serialized value="..." would be silently dropped by the browser's parser —
|
|
183
|
-
// the compiler must move it into the element's content
|
|
184
164
|
check(
|
|
185
165
|
"textarea value attribute becomes text content",
|
|
186
166
|
comp(`<textarea value="{state.cls}"></textarea>`),
|
|
187
|
-
(out) =>
|
|
167
|
+
(out) =>
|
|
168
|
+
/<textarea[^>]*>\$\{olum\.esc\(state\.cls\)\}<\/textarea>/.test(
|
|
169
|
+
tmpl(out),
|
|
170
|
+
) &&
|
|
171
|
+
!/value=/.test(tmpl(out)) &&
|
|
172
|
+
parses(out),
|
|
188
173
|
);
|
|
189
174
|
|
|
190
175
|
check(
|
|
191
176
|
"textarea value attribute wins over authored content",
|
|
192
177
|
comp(`<textarea value="{state.cls}">stale</textarea>`),
|
|
193
|
-
(out) =>
|
|
178
|
+
(out) =>
|
|
179
|
+
/<textarea[^>]*>\$\{olum\.esc\(state\.cls\)\}<\/textarea>/.test(
|
|
180
|
+
tmpl(out),
|
|
181
|
+
) &&
|
|
182
|
+
!/stale/.test(tmpl(out)) &&
|
|
183
|
+
parses(out),
|
|
194
184
|
);
|
|
195
185
|
|
|
196
|
-
// boolean attributes are truthy by PRESENCE (checked="false" is still checked), so
|
|
197
|
-
// attr="{expr}" must toggle the attribute's existence, never interpolate its value
|
|
198
186
|
check(
|
|
199
187
|
"boolean attribute compiles to a presence toggle (checked)",
|
|
200
188
|
comp(`<input type="checkbox" checked="{state.count}" />`),
|
|
201
|
-
(out) =>
|
|
189
|
+
(out) =>
|
|
190
|
+
/\$\{\(state\.count\) \? "checked" : ""\}/.test(tmpl(out)) &&
|
|
191
|
+
!/checked="\$\{olum\.esc/.test(tmpl(out)) &&
|
|
192
|
+
parses(out),
|
|
202
193
|
);
|
|
203
194
|
|
|
204
195
|
check(
|
|
205
196
|
"boolean attribute with an expression (disabled)",
|
|
206
197
|
comp(`<button disabled="{!state.count}">Go</button>`),
|
|
207
|
-
(out) =>
|
|
198
|
+
(out) =>
|
|
199
|
+
/\$\{\(!state\.count\) \? "disabled" : ""\}/.test(tmpl(out)) && parses(out),
|
|
208
200
|
);
|
|
209
201
|
|
|
210
202
|
check(
|
|
211
203
|
"static boolean attribute is left alone",
|
|
212
204
|
comp(`<input type="text" disabled />`),
|
|
213
|
-
(out) =>
|
|
205
|
+
(out) =>
|
|
206
|
+
/disabled/.test(tmpl(out)) &&
|
|
207
|
+
!/\? "disabled"/.test(tmpl(out)) &&
|
|
208
|
+
parses(out),
|
|
214
209
|
);
|
|
215
210
|
|
|
216
211
|
check(
|
|
217
212
|
"boolean attribute inside <for> stays a presence toggle",
|
|
218
|
-
comp(
|
|
219
|
-
|
|
213
|
+
comp(
|
|
214
|
+
`<for each="x of state.items"><option selected="{x.id === state.count}">{x.name}</option></for>`,
|
|
215
|
+
),
|
|
216
|
+
(out) =>
|
|
217
|
+
/\$\{\(x\.id === state\.count\) \? "selected" : ""\}/.test(tmpl(out)) &&
|
|
218
|
+
parses(out),
|
|
220
219
|
);
|
|
221
220
|
|
|
222
|
-
// ── §6 Events on* (code in "") ─────────────────────────────────────────────
|
|
223
221
|
section("§6 Events");
|
|
224
222
|
|
|
225
223
|
check(
|
|
226
224
|
"method-call handler compiles to data-o-event",
|
|
227
|
-
comp(
|
|
228
|
-
|
|
225
|
+
comp(
|
|
226
|
+
`<button onclick="inc()">+</button>`,
|
|
227
|
+
`const inc = () => state.count++;`,
|
|
228
|
+
),
|
|
229
|
+
(out) => /data-o-event=[^>]*onclick\|inc/.test(tmpl(out)) && parses(out),
|
|
229
230
|
);
|
|
230
231
|
|
|
231
232
|
check(
|
|
232
233
|
"multiple handlers in one attribute",
|
|
233
|
-
comp(
|
|
234
|
-
|
|
234
|
+
comp(
|
|
235
|
+
`<button onclick="a(), b()">x</button>`,
|
|
236
|
+
`const a = () => 0; const b = () => 0;`,
|
|
237
|
+
),
|
|
238
|
+
(out) => /onclick\|a=[^&]*&b=/.test(tmpl(out)) && parses(out),
|
|
235
239
|
);
|
|
236
240
|
|
|
237
241
|
check(
|
|
238
242
|
"$event is forwarded to the handler",
|
|
239
243
|
comp(`<input oninput="setVal($event)" />`, `const setVal = (e) => 0;`),
|
|
240
|
-
(out) =>
|
|
244
|
+
(out) =>
|
|
245
|
+
/oninput\|setVal=\$\{JSON\.stringify\(\['\$event'\]\)\}/.test(tmpl(out)) &&
|
|
246
|
+
parses(out),
|
|
241
247
|
);
|
|
242
248
|
check(
|
|
243
249
|
"inline arrow handler becomes a named __olumAnon_ method",
|
|
244
250
|
comp(`<input oninput="(e) => state.name = e.target.value" />`),
|
|
245
|
-
(out) =>
|
|
251
|
+
(out) =>
|
|
252
|
+
/__olumAnon_\w+/.test(out) && /data-o-event/.test(tmpl(out)) && parses(out),
|
|
246
253
|
);
|
|
247
254
|
|
|
248
|
-
// anon handlers are hoisted to component scope where <for> loop variables don't exist,
|
|
249
|
-
// so a referenced loop variable must become a handler parameter whose per-item value
|
|
250
|
-
// is serialized into the data-o-event args at render time
|
|
251
255
|
check(
|
|
252
256
|
"anon handler inside <for> receives the loop variable as an argument",
|
|
253
257
|
comp(
|
|
254
258
|
`<for each="flavour of state.items"><input type="checkbox" onchange="toggle(flavour)($event)" /></for>`,
|
|
255
|
-
`const toggle = (f) => (e) => 0
|
|
259
|
+
`const toggle = (f) => (e) => 0;`,
|
|
256
260
|
),
|
|
257
261
|
(out) =>
|
|
258
|
-
/const __olumAnon_\w+ = \(\$event, flavour\) => \{ toggle\(flavour\)\(\$event\) \}/.test(
|
|
262
|
+
/const __olumAnon_\w+ = \(\$event, flavour\) => \{ toggle\(flavour\)\(\$event\) \}/.test(
|
|
263
|
+
out,
|
|
264
|
+
) &&
|
|
259
265
|
/JSON\.stringify\(\['\$event', flavour\]\)/.test(tmpl(out)) &&
|
|
260
|
-
parses(out)
|
|
266
|
+
parses(out),
|
|
261
267
|
);
|
|
262
268
|
|
|
263
269
|
check(
|
|
264
270
|
"inline arrow handler inside <for> receives the loop variable as an argument",
|
|
265
|
-
comp(
|
|
266
|
-
|
|
271
|
+
comp(
|
|
272
|
+
`<for each="(x, i) of state.items"><button onclick="(e) => state.count = i">x</button></for>`,
|
|
273
|
+
),
|
|
274
|
+
(out) =>
|
|
275
|
+
/const __olumAnon_\w+ = \(\$event, i\) =>/.test(out) &&
|
|
276
|
+
/JSON\.stringify\(\['\$event', i\]\)/.test(tmpl(out)) &&
|
|
277
|
+
parses(out),
|
|
267
278
|
);
|
|
268
279
|
|
|
269
|
-
// ── §7 Conditionals <if>/<else-if>/<else> ──────────────────────────────────
|
|
270
280
|
section("§7 Conditionals");
|
|
271
281
|
|
|
272
282
|
check(
|
|
273
283
|
"if/else-if/else compiles to a raw (un-escaped) ternary",
|
|
274
|
-
comp(
|
|
275
|
-
|
|
284
|
+
comp(
|
|
285
|
+
`<if when="state.count === 1"><p>a</p></if><else-if when="state.count === 2"><p>b</p></else-if><else><p>c</p></else>`,
|
|
286
|
+
),
|
|
287
|
+
(out) =>
|
|
288
|
+
/\$\{state\.count === 1\?/.test(tmpl(out)) &&
|
|
289
|
+
out.indexOf("olum.esc(state.count === 1)") === -1 &&
|
|
290
|
+
parses(out),
|
|
276
291
|
);
|
|
277
292
|
|
|
278
293
|
check(
|
|
279
294
|
"comparison operators survive in a condition (no >)",
|
|
280
295
|
comp(`<if when="state.count > 0"><p>x</p></if>`),
|
|
281
|
-
(out) =>
|
|
296
|
+
(out) =>
|
|
297
|
+
/\$\{state\.count > 0\?/.test(tmpl(out)) &&
|
|
298
|
+
!/state\.count > 0/.test(tmpl(out)) &&
|
|
299
|
+
parses(out),
|
|
282
300
|
);
|
|
283
301
|
|
|
284
|
-
// ── §8 Show ────────────────────────────────────────────────────────────────
|
|
285
302
|
section("§8 Show");
|
|
286
303
|
|
|
287
304
|
check(
|
|
288
|
-
"<show>
|
|
305
|
+
"<show> emits ONE stable wrapper whose display toggles (contents/none)",
|
|
289
306
|
comp(`<show when="state.count"><span>v</span></show>`),
|
|
290
|
-
(out) =>
|
|
307
|
+
(out) =>
|
|
308
|
+
/data-o-show/.test(tmpl(out)) &&
|
|
309
|
+
/display:\$\{\(state\.count\)\?'contents':'none'\}/.test(tmpl(out)) &&
|
|
310
|
+
(tmpl(out).match(/<span>v<\/span>/g) || []).length === 1 &&
|
|
311
|
+
parses(out),
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
check(
|
|
315
|
+
"a double-quoted string inside the <show> condition survives the attribute context",
|
|
316
|
+
comp(`<show when='state.tab === "a"'><span>v</span></show>`),
|
|
317
|
+
|
|
318
|
+
(out) =>
|
|
319
|
+
/display:\$\{\(state\.tab === "a"\)\?'contents':'none'\}/.test(tmpl(out)) &&
|
|
320
|
+
parses(out),
|
|
291
321
|
);
|
|
292
322
|
|
|
293
|
-
// ── §9 Loops <for> ─────────────────────────────────────────────────────────
|
|
294
323
|
section("§9 Loops");
|
|
295
324
|
|
|
296
325
|
check(
|
|
297
326
|
'array loop "x of arr" compiles to .map()',
|
|
298
327
|
comp(`<for each="item of state.items"><li>{item.name}</li></for>`),
|
|
299
|
-
(out) => /state\.items\.map\(function\(item\)/.test(tmpl(out)) && parses(out)
|
|
328
|
+
(out) => /state\.items\.map\(function\(item\)/.test(tmpl(out)) && parses(out),
|
|
300
329
|
);
|
|
301
330
|
|
|
302
331
|
check(
|
|
303
332
|
'numeric loop "i of N" is 1-based new Array(N)',
|
|
304
333
|
comp(`<for each="i of 6"><span>{i}</span></for>`),
|
|
305
|
-
(out) => /new Array\(6\)\.fill\(\)\.map/.test(tmpl(out)) && parses(out)
|
|
334
|
+
(out) => /new Array\(6\)\.fill\(\)\.map/.test(tmpl(out)) && parses(out),
|
|
306
335
|
);
|
|
307
336
|
|
|
308
337
|
check(
|
|
309
338
|
'object loop "key in obj" compiles to Object.keys().map',
|
|
310
339
|
comp(`<for each="key in state.settings"><i>{key}</i></for>`),
|
|
311
|
-
(out) =>
|
|
340
|
+
(out) =>
|
|
341
|
+
/Object\.keys\(state\.settings\)\.map/.test(tmpl(out)) && parses(out),
|
|
312
342
|
);
|
|
313
343
|
|
|
314
344
|
check(
|
|
315
345
|
"keyed loop stamps data-o-key on a component placeholder",
|
|
316
|
-
comp(
|
|
317
|
-
|
|
346
|
+
comp(
|
|
347
|
+
`<for each="item of state.items" key="item.id"><Row item="{item}" /></for>`,
|
|
348
|
+
`import Row from "./Row";`,
|
|
349
|
+
),
|
|
350
|
+
(out) => /data-o-key/.test(tmpl(out)) && parses(out),
|
|
318
351
|
);
|
|
319
352
|
|
|
320
|
-
// the HTML parser drops unknown tags inside <select> and foster-parents them out of tables —
|
|
321
|
-
// the tag shield (tokens.js) must keep <for>/<if> alive in these containers
|
|
322
353
|
check(
|
|
323
354
|
"<for> inside <select> survives the parse and scopes the loop variable",
|
|
324
|
-
comp(
|
|
325
|
-
|
|
355
|
+
comp(
|
|
356
|
+
`<select><for each="q of state.questions"><option value="{q.id}">{q.text}</option></for></select>`,
|
|
357
|
+
),
|
|
358
|
+
(out) =>
|
|
359
|
+
/<select>\$\{state\.questions\.map\(function\(q\)/.test(tmpl(out)) &&
|
|
360
|
+
!/<olum-select/.test(tmpl(out)) &&
|
|
361
|
+
parses(out),
|
|
326
362
|
);
|
|
327
363
|
|
|
328
364
|
check(
|
|
329
365
|
"<for> inside <table>/<tbody> is not foster-parented out",
|
|
330
|
-
comp(
|
|
331
|
-
|
|
366
|
+
comp(
|
|
367
|
+
`<table><tbody><for each="row of state.rows"><tr><td>{row.a}</td></tr></for></tbody></table>`,
|
|
368
|
+
),
|
|
369
|
+
(out) =>
|
|
370
|
+
/<tbody>\$\{state\.rows\.map\(function\(row\)/.test(tmpl(out)) &&
|
|
371
|
+
/<tr><td>/.test(tmpl(out)) &&
|
|
372
|
+
parses(out),
|
|
332
373
|
);
|
|
333
374
|
|
|
334
|
-
// ── §10 Raw HTML (html="expr" + olum.html()) ───────────────────────────────
|
|
335
375
|
section("§10 Raw HTML");
|
|
336
376
|
|
|
337
377
|
check(
|
|
338
378
|
'html="expr" sets unescaped ${expr} content, other attrs kept',
|
|
339
379
|
comp(`<div class="rich" html="state.html">fallback</div>`),
|
|
340
|
-
(out) =>
|
|
380
|
+
(out) =>
|
|
381
|
+
/<div class="rich">\$\{state\.html\}<\/div>/.test(tmpl(out)) &&
|
|
382
|
+
!/olum\.esc\(state\.html\)/.test(tmpl(out)) &&
|
|
383
|
+
parses(out),
|
|
341
384
|
);
|
|
342
385
|
|
|
343
386
|
check(
|
|
344
387
|
"olum.html(...) inline opt-out is preserved (esc detects it at runtime)",
|
|
345
388
|
comp(`<p>{olum.html(state.html)}</p>`),
|
|
346
|
-
(out) =>
|
|
389
|
+
(out) =>
|
|
390
|
+
/olum\.esc\(olum\.html\(state\.html\)\)/.test(tmpl(out)) && parses(out),
|
|
347
391
|
);
|
|
348
392
|
|
|
349
|
-
// ── §11 Components & props ──────────────────────────────────────────────────
|
|
350
393
|
section("§11 Components & props");
|
|
351
394
|
|
|
352
395
|
check(
|
|
353
396
|
"PascalCase component compiles to an <olum> placeholder",
|
|
354
397
|
comp(`<Badge />`, `import Badge from "./Badge";`),
|
|
355
|
-
(out) => /<olum name="Badge"/.test(tmpl(out)) && parses(out)
|
|
398
|
+
(out) => /<olum name="Badge"/.test(tmpl(out)) && parses(out),
|
|
356
399
|
);
|
|
357
400
|
|
|
358
401
|
check(
|
|
359
402
|
"literal prop is a string",
|
|
360
403
|
comp(`<C title="Hello" />`, `import C from "./C";`),
|
|
361
|
-
(out) => /title: 'Hello'/.test(out) && parses(out)
|
|
404
|
+
(out) => /title: 'Hello'/.test(out) && parses(out),
|
|
362
405
|
);
|
|
363
406
|
|
|
364
407
|
check(
|
|
365
408
|
'whole-value prop "{expr}" keeps the expression type',
|
|
366
|
-
comp(
|
|
367
|
-
|
|
409
|
+
comp(
|
|
410
|
+
`<C count="{state.count + 1}" data="{state.settings}" />`,
|
|
411
|
+
`import C from "./C";`,
|
|
412
|
+
),
|
|
413
|
+
(out) =>
|
|
414
|
+
/count: state\.count \+ 1/.test(out) &&
|
|
415
|
+
/data: state\.settings/.test(out) &&
|
|
416
|
+
parses(out),
|
|
368
417
|
);
|
|
369
418
|
|
|
370
419
|
check(
|
|
371
420
|
"mixed-text prop becomes a template literal (string)",
|
|
372
421
|
comp(`<C greet="Hi {state.name}" />`, `import C from "./C";`),
|
|
373
|
-
(out) => /greet: `Hi \$\{state\.name\}`/.test(out) && parses(out)
|
|
422
|
+
(out) => /greet: `Hi \$\{state\.name\}`/.test(out) && parses(out),
|
|
374
423
|
);
|
|
375
424
|
|
|
376
425
|
check(
|
|
377
426
|
"passing state.X records a write-back source",
|
|
378
427
|
comp(`<C v="{state.count}" />`, `import C from "./C";`),
|
|
379
|
-
(out) => /data-o-props-src="v:state:count"/.test(out) && parses(out)
|
|
428
|
+
(out) => /data-o-props-src="v:state:count"/.test(out) && parses(out),
|
|
429
|
+
);
|
|
430
|
+
|
|
431
|
+
check(
|
|
432
|
+
"a slash in an attribute value still yields an <olum> placeholder",
|
|
433
|
+
comp(`<Item returnTo="#/top/1" />`, `import Item from "./Item";`),
|
|
434
|
+
(out) =>
|
|
435
|
+
/<olum name="Item"/.test(out) && !/<item\b/.test(tmpl(out)) && parses(out),
|
|
436
|
+
);
|
|
437
|
+
|
|
438
|
+
check(
|
|
439
|
+
"an interpolated URL attribute compiles to a template literal prop",
|
|
440
|
+
comp(`<Item returnTo="#/top/{state.page}" />`, `import Item from "./Item";`),
|
|
441
|
+
(out) =>
|
|
442
|
+
/returnTo: `#\/top\/\$\{state\.page\}`/.test(out) &&
|
|
443
|
+
/<olum name="Item"/.test(out) &&
|
|
444
|
+
parses(out),
|
|
445
|
+
);
|
|
446
|
+
|
|
447
|
+
check(
|
|
448
|
+
"object prop encoding escapes single quotes to survive the single-quoted attribute",
|
|
449
|
+
comp(`<C data="{state.settings}" />`, `import C from "./C";`),
|
|
450
|
+
(out) =>
|
|
451
|
+
/encodeURIComponent\(JSON\.stringify\([\s\S]*\)\)\.replace\(\/'\/g,'%27'\)/.test(
|
|
452
|
+
out,
|
|
453
|
+
) && parses(out),
|
|
380
454
|
);
|
|
381
455
|
|
|
382
|
-
// ── §11b Function props ─────────────────────────────────────────────────────
|
|
383
456
|
section("§11b Function props");
|
|
384
457
|
|
|
385
458
|
check(
|
|
386
459
|
"a prop naming a top-level function is a method source, not a JSON entry",
|
|
387
|
-
comp(
|
|
388
|
-
|
|
460
|
+
comp(
|
|
461
|
+
`<C todo="{state.items}" toggle="{toggle}" />`,
|
|
462
|
+
`import C from "./C";\nconst toggle = (id) => id;`,
|
|
463
|
+
),
|
|
464
|
+
(out) =>
|
|
465
|
+
/toggle:method:toggle/.test(out) &&
|
|
466
|
+
/todo: state\.items/.test(out) &&
|
|
467
|
+
!/toggle: toggle/.test(out) &&
|
|
468
|
+
parses(out),
|
|
389
469
|
);
|
|
390
470
|
|
|
391
471
|
check(
|
|
392
472
|
"function prop can be renamed on the child",
|
|
393
|
-
comp(
|
|
394
|
-
|
|
473
|
+
comp(
|
|
474
|
+
`<C onToggle="{toggle}" />`,
|
|
475
|
+
`import C from "./C";\nfunction toggle(id) { return id; }`,
|
|
476
|
+
),
|
|
477
|
+
(out) => /data-o-props-src="onToggle:method:toggle"/.test(out) && parses(out),
|
|
395
478
|
);
|
|
396
479
|
|
|
397
480
|
check(
|
|
398
481
|
"function-only props emit data-o-props-src without data-o-props",
|
|
399
|
-
comp(
|
|
400
|
-
|
|
482
|
+
comp(
|
|
483
|
+
`<C toggle="{toggle}" remove="{remove}" />`,
|
|
484
|
+
`import C from "./C";\nconst toggle = (id) => id;\nconst remove = (id) => id;`,
|
|
485
|
+
),
|
|
486
|
+
(out) =>
|
|
487
|
+
/data-o-props-src="toggle:method:toggle\|remove:method:remove"/.test(out) &&
|
|
488
|
+
!/data-o-props=/.test(tmpl(out)) &&
|
|
489
|
+
parses(out),
|
|
401
490
|
);
|
|
402
491
|
|
|
403
492
|
check(
|
|
404
493
|
"a bare identifier NOT naming a function (e.g. loop var) stays a JSON prop",
|
|
405
|
-
comp(
|
|
406
|
-
|
|
494
|
+
comp(
|
|
495
|
+
`<for each="item of state.items" key="item.id"><C item="{item}" /></for>`,
|
|
496
|
+
`import C from "./C";`,
|
|
497
|
+
),
|
|
498
|
+
(out) => /item: item/.test(out) && !/item:method/.test(out) && parses(out),
|
|
407
499
|
);
|
|
408
500
|
|
|
409
501
|
check(
|
|
410
502
|
"forwarding a destructured prop emits a kind-props source (function props survive nesting)",
|
|
411
|
-
comp(
|
|
412
|
-
|
|
503
|
+
comp(
|
|
504
|
+
`<C onMessage="{onMessage}" />`,
|
|
505
|
+
`import C from "./C";\nconst { onMessage } = props();`,
|
|
506
|
+
),
|
|
507
|
+
(out) =>
|
|
508
|
+
/data-o-props-src="onMessage:props:onMessage"/.test(out) &&
|
|
509
|
+
/onMessage: __props\.onMessage/.test(out) &&
|
|
510
|
+
!/onMessage:method/.test(out) &&
|
|
511
|
+
parses(out),
|
|
413
512
|
);
|
|
414
513
|
|
|
415
514
|
check(
|
|
416
515
|
"forwarding a RENAMED destructured prop keys the source by the original prop name",
|
|
417
|
-
comp(
|
|
418
|
-
|
|
516
|
+
comp(
|
|
517
|
+
`<C onMessage="{om}" />`,
|
|
518
|
+
`import C from "./C";\nconst { onMessage: om } = props();`,
|
|
519
|
+
),
|
|
520
|
+
(out) =>
|
|
521
|
+
/data-o-props-src="onMessage:props:onMessage"/.test(out) && parses(out),
|
|
419
522
|
);
|
|
420
523
|
|
|
421
524
|
check(
|
|
422
|
-
// a destructured prop isn't in methodsRef, so the data-o-event chain can't dispatch it by
|
|
423
|
-
// name — the compiler must wrap the call in an anon handler whose closure holds the prop
|
|
424
525
|
"calling a destructured prop in an event handler wraps it in an anon method",
|
|
425
|
-
comp(
|
|
426
|
-
|
|
526
|
+
comp(
|
|
527
|
+
`<button onclick="onclick()">x</button>`,
|
|
528
|
+
`const { onclick } = props();`,
|
|
529
|
+
),
|
|
530
|
+
|
|
531
|
+
(out) =>
|
|
532
|
+
/__olumAnon_\w+ = \(\$event\) => \{ __props\.onclick\(\) \}/.test(out) &&
|
|
533
|
+
/onclick\|__olumAnon_/.test(out) &&
|
|
534
|
+
parses(out),
|
|
427
535
|
);
|
|
428
536
|
|
|
429
537
|
check(
|
|
430
538
|
"forwarding a function prop records a props source (resolved live at runtime)",
|
|
431
|
-
comp(
|
|
432
|
-
|
|
539
|
+
comp(
|
|
540
|
+
`<C fn="{props().onSave}" />`,
|
|
541
|
+
`import C from "./C";\nimport { props } from "../core/olum.js";`,
|
|
542
|
+
),
|
|
543
|
+
(out) => /fn:props:onSave/.test(out) && parses(out),
|
|
433
544
|
);
|
|
434
545
|
|
|
435
546
|
check(
|
|
436
547
|
"calling a function prop in an event compiles to an anon handler",
|
|
437
|
-
comp(
|
|
438
|
-
|
|
548
|
+
comp(
|
|
549
|
+
`<button onclick="props().toggle(props().todo.id)">x</button>`,
|
|
550
|
+
`import { props } from "../core/olum.js";`,
|
|
551
|
+
),
|
|
552
|
+
(out) =>
|
|
553
|
+
/const __olumAnon_\w+ = \(\$event\) => \{ props\(_storeKey\)\.toggle\(props\(_storeKey\)\.todo\.id\) \};/.test(
|
|
554
|
+
out,
|
|
555
|
+
) && parses(out),
|
|
439
556
|
);
|
|
440
557
|
|
|
441
558
|
check(
|
|
442
559
|
"plain method-call events still use the direct call-list path (no anon wrapper)",
|
|
443
|
-
comp(
|
|
444
|
-
|
|
560
|
+
comp(
|
|
561
|
+
`<button onclick="inc(), dec($event)">x</button>`,
|
|
562
|
+
`const inc = () => {};\nconst dec = (e) => {};`,
|
|
563
|
+
),
|
|
564
|
+
(out) =>
|
|
565
|
+
!/__olumAnon_/.test(out) &&
|
|
566
|
+
/onclick\|inc=/.test(out) &&
|
|
567
|
+
/dec=/.test(out) &&
|
|
568
|
+
parses(out),
|
|
445
569
|
);
|
|
446
570
|
|
|
447
|
-
// ── §12 Slots {children} ───────────────────────────────────────────────────
|
|
448
571
|
section("§12 Slots");
|
|
449
572
|
|
|
450
573
|
check(
|
|
451
574
|
"{children} compiles to the slot interpolation",
|
|
452
575
|
comp(`<div class="slot">{children}</div>`),
|
|
453
|
-
(out) =>
|
|
576
|
+
(out) =>
|
|
577
|
+
/<div class="slot">\$\{children\}<\/div>/.test(tmpl(out)) && parses(out),
|
|
454
578
|
);
|
|
455
579
|
|
|
456
|
-
// ── §14 Watchers ────────────────────────────────────────────────────────────
|
|
457
580
|
section("§14 Watchers");
|
|
458
581
|
|
|
459
582
|
check(
|
|
460
583
|
"a watcher is wired into proxyHandler",
|
|
461
584
|
comp(`<div></div>`, `const watcher = { count(o, n) {} };`),
|
|
462
|
-
(out) => /olum\.proxyHandler\(state, ?watcher,/.test(out) && parses(out)
|
|
585
|
+
(out) => /olum\.proxyHandler\(state, ?watcher,/.test(out) && parses(out),
|
|
463
586
|
);
|
|
464
587
|
|
|
465
588
|
check(
|
|
466
589
|
"no watcher -> proxyHandler gets null",
|
|
467
590
|
comp(`<div></div>`),
|
|
468
|
-
(out) => /olum\.proxyHandler\(state, ?null,/.test(out) && parses(out)
|
|
591
|
+
(out) => /olum\.proxyHandler\(state, ?null,/.test(out) && parses(out),
|
|
469
592
|
);
|
|
470
593
|
|
|
471
|
-
// ── §15 Lifecycle hooks ─────────────────────────────────────────────────────
|
|
472
594
|
section("§15 Lifecycle hooks");
|
|
473
595
|
|
|
474
596
|
check(
|
|
475
597
|
"mounted hook is resolved at runtime via a typeof guard",
|
|
476
598
|
comp(`<div></div>`, `const mounted = () => 0; const unMounted = () => 0;`),
|
|
477
|
-
(out) =>
|
|
599
|
+
(out) =>
|
|
600
|
+
/mounted: ?typeof mounted !== "undefined" \? mounted : null/.test(out) &&
|
|
601
|
+
parses(out),
|
|
478
602
|
);
|
|
479
603
|
|
|
480
604
|
check(
|
|
481
605
|
"unMounted is emitted as a null placeholder (assigned at runtime)",
|
|
482
606
|
comp(`<div></div>`),
|
|
483
|
-
(out) => /unMounted: ?null/.test(out) && parses(out)
|
|
607
|
+
(out) => /unMounted: ?null/.test(out) && parses(out),
|
|
484
608
|
);
|
|
485
609
|
|
|
486
|
-
// ── §16 Scoped CSS ──────────────────────────────────────────────────────────
|
|
487
610
|
section("§16 Scoped CSS");
|
|
488
611
|
|
|
489
612
|
check(
|
|
490
|
-
// the scope attr attaches to the selector's LAST COMPOUND (.box[data-o-x], p[data-o-x]:hover),
|
|
491
|
-
// not as a `[data-o-x] .box` descendant prefix — a descendant rule would also match nested
|
|
492
|
-
// components' elements, which sit inside this component's DOM but carry their own scope attr
|
|
493
613
|
"styles are scoped onto the selector's last compound and injected",
|
|
494
|
-
comp(`<div class="box"></div>`).replace(
|
|
495
|
-
|
|
614
|
+
comp(`<div class="box"></div>`).replace(
|
|
615
|
+
"</script>",
|
|
616
|
+
"</script>\n<style>.box{color:red}\np:hover{color:blue}</style>",
|
|
617
|
+
),
|
|
618
|
+
(out) =>
|
|
619
|
+
/\.box\[data-o-\w+\]/.test(out) &&
|
|
620
|
+
/p\[data-o-\w+\]:hover/.test(out) &&
|
|
621
|
+
/olum\.injectStyle\(/.test(out) &&
|
|
622
|
+
parses(out),
|
|
496
623
|
);
|
|
497
624
|
|
|
498
|
-
// ── §17 Imports ─────────────────────────────────────────────────────────────
|
|
499
625
|
section("§17 Imports");
|
|
500
626
|
|
|
501
627
|
check(
|
|
502
628
|
"component import is preserved (resolved to .js) and registered",
|
|
503
629
|
comp(`<Badge />`, `import Badge from "./Badge";`),
|
|
504
|
-
(out) =>
|
|
630
|
+
(out) =>
|
|
631
|
+
/import Badge from "\.\/Badge\.js"/.test(out) &&
|
|
632
|
+
/components:\s*\{[^}]*Badge\s*:\s*Badge/.test(out) &&
|
|
633
|
+
parses(out),
|
|
505
634
|
);
|
|
506
635
|
|
|
507
|
-
// ── §19 Unsupported prop syntax (negative tests) ───────────────────────────
|
|
508
|
-
// NOTE: tests for commented-out/disabled compiler passes (:style, model, mode) were
|
|
509
|
-
// removed — a disabled feature has no stable contract to assert against. The checks
|
|
510
|
-
// below cover the parser's *active* behavior: brace-prop shorthand is silently ignored.
|
|
511
636
|
section("§19 Unsupported prop syntax");
|
|
512
637
|
|
|
513
638
|
check(
|
|
514
639
|
"naked-brace prop shorthand <Comp {a}/> is ignored (no props)",
|
|
515
640
|
comp(`<C {state} />`, `import C from "./C";`),
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
641
|
+
|
|
642
|
+
(out) =>
|
|
643
|
+
/<olum name="C"/.test(tmpl(out)) &&
|
|
644
|
+
!/data-o-props=/.test(tmpl(out)) &&
|
|
645
|
+
parses(out),
|
|
519
646
|
);
|
|
520
647
|
|
|
521
648
|
check(
|
|
522
649
|
"unquoted brace prop <Comp a={a}/> is ignored (no props)",
|
|
523
650
|
comp(`<C a={state} />`, `import C from "./C";`),
|
|
524
|
-
(out) =>
|
|
651
|
+
(out) =>
|
|
652
|
+
/<olum name="C"/.test(tmpl(out)) &&
|
|
653
|
+
!/data-o-props=/.test(tmpl(out)) &&
|
|
654
|
+
parses(out),
|
|
525
655
|
);
|
|
526
656
|
|
|
527
|
-
// ── §20 File-based routing (compileRoutes) ──────────────────────────────────
|
|
528
|
-
// compileRoutes(entryPoint) scans a src tree and emits the router entry module:
|
|
529
|
-
// imports + `export const routes = [...]` + Router/Olum bootstrap. Unlike the
|
|
530
|
-
// parser checks above it reads the filesystem, so each check builds a throwaway
|
|
531
|
-
// fixture tree in the OS temp dir and removes it afterwards.
|
|
532
657
|
section("§20 File-based routing (compileRoutes)");
|
|
533
658
|
|
|
534
659
|
const { compileRoutes } = require("../lib/helpers");
|
|
@@ -536,15 +661,15 @@ const fs = require("fs");
|
|
|
536
661
|
const os = require("os");
|
|
537
662
|
const nodePath = require("path");
|
|
538
663
|
|
|
539
|
-
// like check() but for units that aren't parser(template): producer() returns
|
|
540
|
-
// the value under test (and owns its fixture setup/teardown)
|
|
541
664
|
function checkFn(name, producer, assertion) {
|
|
542
665
|
let out;
|
|
543
666
|
try {
|
|
544
667
|
out = producer();
|
|
545
668
|
} catch (e) {
|
|
546
669
|
recordFail(name);
|
|
547
|
-
console.log(
|
|
670
|
+
console.log(
|
|
671
|
+
" " + FAIL_ICON + " " + name + dim(" (threw: " + e.message + ")"),
|
|
672
|
+
);
|
|
548
673
|
console.log(" " + dim("↳ in " + currentSection));
|
|
549
674
|
return;
|
|
550
675
|
}
|
|
@@ -560,7 +685,8 @@ function checkFn(name, producer, assertion) {
|
|
|
560
685
|
console.log(" " + PASS_ICON + " " + name);
|
|
561
686
|
} else {
|
|
562
687
|
recordFail(name);
|
|
563
|
-
if (!detail)
|
|
688
|
+
if (!detail)
|
|
689
|
+
detail = "\n " + dim(String(out).replace(/\s+/g, " ").slice(0, 200));
|
|
564
690
|
console.log(" " + FAIL_ICON + " " + red(name) + detail);
|
|
565
691
|
console.log(" " + dim("↳ in " + currentSection));
|
|
566
692
|
}
|
|
@@ -590,10 +716,10 @@ checkFn(
|
|
|
590
716
|
"about/page.html",
|
|
591
717
|
"(main)/club/page.html",
|
|
592
718
|
"01-blog/[slug]/page.html",
|
|
593
|
-
"components/Widget.html",
|
|
594
|
-
"_drafts/page.html",
|
|
719
|
+
"components/Widget.html",
|
|
720
|
+
"_drafts/page.html",
|
|
595
721
|
],
|
|
596
|
-
(root) => compileRoutes(root)
|
|
722
|
+
(root) => compileRoutes(root),
|
|
597
723
|
),
|
|
598
724
|
(out) => {
|
|
599
725
|
const paths = [...out.matchAll(/path: "([^"]+)"/g)].map((m) => m[1]);
|
|
@@ -601,7 +727,6 @@ checkFn(
|
|
|
601
727
|
/import App from "\.\/page\.js";/.test(out) &&
|
|
602
728
|
/import NotFound from "\.\/not-found\.js";/.test(out) &&
|
|
603
729
|
/import About from "\.\/about\/page\.js";/.test(out) &&
|
|
604
|
-
// imports keep the REAL file location; only route path/comp are cleaned
|
|
605
730
|
/import Club from "\.\/\(main\)\/club\/page\.js";/.test(out) &&
|
|
606
731
|
/import BlogSlug from "\.\/01-blog\/\[slug\]\/page\.js";/.test(out) &&
|
|
607
732
|
/\{ path: "\/club", comp: Club \}/.test(out) &&
|
|
@@ -609,26 +734,177 @@ checkFn(
|
|
|
609
734
|
/\{ path: "\/about", comp: About \}/.test(out) &&
|
|
610
735
|
paths[0] === "/" &&
|
|
611
736
|
paths[paths.length - 1] === "/404" &&
|
|
612
|
-
/err: "\/404",/.test(out) &&
|
|
613
|
-
!/Widget|_drafts|Drafts/.test(out) &&
|
|
737
|
+
/err: "\/404",/.test(out) &&
|
|
738
|
+
!/Widget|_drafts|Drafts/.test(out) &&
|
|
614
739
|
/new Router\(config\)/.test(out) &&
|
|
615
740
|
/new Olum\(\)\.\$\("#app"\)\.use\(router\)/.test(out)
|
|
616
741
|
);
|
|
617
|
-
}
|
|
742
|
+
},
|
|
618
743
|
);
|
|
619
744
|
|
|
620
745
|
checkFn(
|
|
621
746
|
"no not-found.html -> no /404 route and no err in the router config",
|
|
622
|
-
() =>
|
|
623
|
-
|
|
747
|
+
() =>
|
|
748
|
+
withRouteTree(["page.html", "about/page.html"], (root) =>
|
|
749
|
+
compileRoutes(root),
|
|
750
|
+
),
|
|
751
|
+
(out) =>
|
|
752
|
+
!/\/404/.test(out) &&
|
|
753
|
+
!/err:/.test(out) &&
|
|
754
|
+
/\{ path: "\/", comp: App \}/.test(out) &&
|
|
755
|
+
/\{ path: "\/about", comp: About \}/.test(out),
|
|
756
|
+
);
|
|
757
|
+
|
|
758
|
+
section("§21 Live destructured props");
|
|
759
|
+
|
|
760
|
+
const LIVE_FIXTURE = `<script>
|
|
761
|
+
import { props } from "olum";
|
|
762
|
+
const { color, size = "md", theme: t } = props();
|
|
763
|
+
const shout = () => color + "!";
|
|
764
|
+
const shadowed = (color) => color.trim();
|
|
765
|
+
const payload = () => ({ color });
|
|
766
|
+
const keyed = { color: 1 };
|
|
767
|
+
const member = (o) => o.color;
|
|
768
|
+
</script>
|
|
769
|
+
<p>{color} {size} {t}</p>
|
|
770
|
+
<for each="color of [1,2]"><i>{color}</i></for>`;
|
|
771
|
+
|
|
772
|
+
check("template reference becomes a live read", LIVE_FIXTURE, (out) =>
|
|
773
|
+
/esc\(__props\.color\)/.test(tmpl(out)),
|
|
774
|
+
);
|
|
775
|
+
|
|
776
|
+
check("method body reference is rewritten too", LIVE_FIXTURE, (out) =>
|
|
777
|
+
/__props\.color \+ "!"/.test(out),
|
|
778
|
+
);
|
|
779
|
+
|
|
780
|
+
check("a default becomes an undefined-guarded ternary", LIVE_FIXTURE, (out) =>
|
|
781
|
+
/__props\.size === undefined \? \("md"\) : __props\.size/.test(out),
|
|
782
|
+
);
|
|
783
|
+
|
|
784
|
+
check("an alias reads its original prop key", LIVE_FIXTURE, (out) =>
|
|
785
|
+
/esc\(__props\.theme\)/.test(tmpl(out)),
|
|
786
|
+
);
|
|
787
|
+
|
|
788
|
+
check("a shadowing param is left alone", LIVE_FIXTURE, (out) =>
|
|
789
|
+
/\(color\) => color\.trim\(\)/.test(out),
|
|
790
|
+
);
|
|
791
|
+
|
|
792
|
+
check(
|
|
793
|
+
"a loop variable shadowing the prop is left alone",
|
|
794
|
+
LIVE_FIXTURE,
|
|
795
|
+
(out) =>
|
|
796
|
+
/map\(function\(color\)/.test(tmpl(out)) &&
|
|
797
|
+
/<i>\$\{olum\.esc\(color\)\}<\/i>/.test(tmpl(out)),
|
|
798
|
+
);
|
|
799
|
+
|
|
800
|
+
check(
|
|
801
|
+
"object-expression shorthand expands with its key; keys and members untouched",
|
|
802
|
+
LIVE_FIXTURE,
|
|
803
|
+
(out) =>
|
|
804
|
+
/\(\{ color: __props\.color \}\)/.test(out) &&
|
|
805
|
+
/\{ color: 1 \}/.test(out) &&
|
|
806
|
+
/o\.color/.test(out),
|
|
807
|
+
);
|
|
808
|
+
|
|
809
|
+
check(
|
|
810
|
+
"the declaration is kept and the output still parses",
|
|
811
|
+
LIVE_FIXTURE,
|
|
812
|
+
(out) =>
|
|
813
|
+
/\{ color, size = "md", theme: t \} = props\(_storeKey\)/.test(out) &&
|
|
814
|
+
parses(out),
|
|
815
|
+
);
|
|
816
|
+
|
|
817
|
+
section("§22 <log> debug tag");
|
|
818
|
+
|
|
819
|
+
check(
|
|
820
|
+
"<log>{expr}</log> compiles to console.log(expr) rendering nothing",
|
|
821
|
+
"<log>{state.user}</log>",
|
|
822
|
+
(out) =>
|
|
823
|
+
/\$\{\(console\.log\(state\.user\), ''\)\}/.test(tmpl(out)) &&
|
|
824
|
+
!/<log>/.test(out),
|
|
825
|
+
);
|
|
826
|
+
|
|
827
|
+
check(
|
|
828
|
+
"the logged state key becomes a template dep (so it re-fires on change)",
|
|
829
|
+
"<script>\n const state = { n: 0, user: {} };\n</script>\n<div>{state.n}</div>\n<log>{state.user}</log>",
|
|
830
|
+
(out) => /deps:\s*\[[^\]]*"user"[^\]]*\]/.test(out),
|
|
831
|
+
);
|
|
832
|
+
|
|
833
|
+
check(
|
|
834
|
+
"multiple comma-separated args pass straight through to console.log",
|
|
835
|
+
"<log>{state.a, state.b}</log>",
|
|
836
|
+
(out) => /console\.log\(state\.a, state\.b\)/.test(tmpl(out)),
|
|
837
|
+
);
|
|
838
|
+
|
|
839
|
+
check(
|
|
840
|
+
"an object-literal argument keeps its braces",
|
|
841
|
+
"<log>{ {name: state.user} }</log>",
|
|
842
|
+
(out) =>
|
|
843
|
+
/console\.log\(\{name: state\.user\}\)/.test(tmpl(out)) && parses(out),
|
|
844
|
+
);
|
|
845
|
+
|
|
846
|
+
check(
|
|
847
|
+
"a comment containing the text <script> doesn't wipe the template",
|
|
848
|
+
`<!-- put this in <script> -->\n<script>\n const state = { n: 0 };\n</script>\n<p>{state.n}</p>`,
|
|
849
|
+
(out) => /olum\.esc\(state\.n\)/.test(tmpl(out)) && parses(out),
|
|
850
|
+
);
|
|
851
|
+
|
|
852
|
+
section("§23 <transition> tag");
|
|
853
|
+
|
|
854
|
+
check(
|
|
855
|
+
"<transition> dissolves and stamps data-o-trans on its child",
|
|
856
|
+
comp(`<transition in="fly({ y: 200 })" out="fade"><p>hi</p></transition>`),
|
|
857
|
+
(out) =>
|
|
858
|
+
/<p[^>]*data-o-trans=/.test(tmpl(out)) &&
|
|
859
|
+
!/<transition/.test(tmpl(out)) &&
|
|
860
|
+
parses(out),
|
|
861
|
+
);
|
|
862
|
+
|
|
863
|
+
check(
|
|
864
|
+
"in and out defs are encoded with render-time params",
|
|
865
|
+
comp(`<transition in="fly({ y: 200 })" out="fade"><p>hi</p></transition>`),
|
|
866
|
+
(out) =>
|
|
867
|
+
/in:fly=\$\{JSON\.stringify\(\[\{ y: 200 \}\]\)\}/.test(out) &&
|
|
868
|
+
/out:fade=\$\{JSON\.stringify\(\[\]\)\}/.test(out),
|
|
869
|
+
);
|
|
870
|
+
|
|
871
|
+
check(
|
|
872
|
+
"transition shorthand applies the same fn to in and out",
|
|
873
|
+
comp(`<transition transition="fade"><p>hi</p></transition>`),
|
|
874
|
+
(out) => /in:fade=/.test(out) && /out:fade=/.test(out) && parses(out),
|
|
875
|
+
);
|
|
876
|
+
|
|
877
|
+
check(
|
|
878
|
+
"flip attribute encodes a flip= segment with render-time params",
|
|
879
|
+
comp(`<transition flip="{ duration: 300 }"><li>x</li></transition>`),
|
|
880
|
+
(out) =>
|
|
881
|
+
/flip=\$\{JSON\.stringify\(\[\{ duration: 300 \}\]\)\}/.test(out) &&
|
|
882
|
+
/<li[^>]*data-o-trans=/.test(tmpl(out)) &&
|
|
883
|
+
parses(out),
|
|
884
|
+
);
|
|
885
|
+
|
|
886
|
+
check(
|
|
887
|
+
"bare flip (no params) encodes flip= with an empty arg list",
|
|
888
|
+
comp(`<transition flip><li>x</li></transition>`),
|
|
889
|
+
(out) => /flip=\$\{JSON\.stringify\(\[\]\)\}/.test(out) && parses(out),
|
|
890
|
+
);
|
|
891
|
+
|
|
892
|
+
check(
|
|
893
|
+
"flip composes with in/out on the same wrapper (crossfade + flip)",
|
|
894
|
+
comp(
|
|
895
|
+
`<transition in="receive({ key: id })" out="send({ key: id })" flip><li>x</li></transition>`,
|
|
896
|
+
),
|
|
897
|
+
(out) =>
|
|
898
|
+
/in:receive=/.test(out) &&
|
|
899
|
+
/out:send=/.test(out) &&
|
|
900
|
+
/flip=/.test(out) &&
|
|
901
|
+
parses(out),
|
|
624
902
|
);
|
|
625
903
|
|
|
626
904
|
console.log("\n========================");
|
|
627
905
|
const summary = `${passed} passed, ${failed} failed`;
|
|
628
906
|
console.log((failed ? red(bold(summary)) : green(bold(summary))) + "\n");
|
|
629
907
|
|
|
630
|
-
// Recap which section(s) to look into, grouped so a run with several failures is
|
|
631
|
-
// still scannable ("go check §11b").
|
|
632
908
|
if (failed) {
|
|
633
909
|
const bySection = {};
|
|
634
910
|
failedSections.forEach(({ section, name }) => {
|
|
@@ -642,12 +918,14 @@ if (failed) {
|
|
|
642
918
|
console.log("");
|
|
643
919
|
}
|
|
644
920
|
|
|
645
|
-
|
|
646
|
-
// throws before registering, etc.). Bump EXPECTED_CHECKS when you add/remove tests.
|
|
647
|
-
const EXPECTED_CHECKS = 58;
|
|
921
|
+
const EXPECTED_CHECKS = 81;
|
|
648
922
|
const total = passed + failed;
|
|
649
923
|
if (total !== EXPECTED_CHECKS) {
|
|
650
|
-
console.log(
|
|
924
|
+
console.log(
|
|
925
|
+
yellow(
|
|
926
|
+
`⚠ ran ${total} checks but expected ${EXPECTED_CHECKS} — did a test get dropped?`,
|
|
927
|
+
) + "\n",
|
|
928
|
+
);
|
|
651
929
|
process.exit(1);
|
|
652
930
|
}
|
|
653
931
|
|