olum-compiler 0.9.7 → 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/.claude/settings.local.json +9 -0
- package/lib/events.js +23 -0
- package/lib/htmlParser.js +3 -3
- package/lib/parser.js +9 -1
- package/lib/placeholder.js +22 -9
- package/package.json +4 -1
- package/tests/compiler.test.js +449 -0
package/lib/events.js
CHANGED
|
@@ -11,6 +11,22 @@ function looksLikeFunction(str) {
|
|
|
11
11
|
return /^(?:\([^)]*\)|[\w$]+)\s*=>/.test(str) || /^function\b/.test(str);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
// True when the value is a plain call list the legacy parser can split — one or more
|
|
15
|
+
// `name(args)` calls where each callee is a bare identifier: onclick="inc()" / "inc(), dec()".
|
|
16
|
+
// Anything else (props().toggle(id), state.count++, obj.method()) must run as a real JS
|
|
17
|
+
// expression, so the caller wraps it in an anonymous handler instead.
|
|
18
|
+
function isSimpleCallList(str) {
|
|
19
|
+
try {
|
|
20
|
+
const acorn = require("acorn");
|
|
21
|
+
const node = acorn.parseExpressionAt(str, 0, { ecmaVersion: "latest" });
|
|
22
|
+
if (node.end !== str.length) return false;
|
|
23
|
+
const calls = node.type === "SequenceExpression" ? node.expressions : [node];
|
|
24
|
+
return calls.every((c) => c.type === "CallExpression" && c.callee.type === "Identifier");
|
|
25
|
+
} catch (e) {
|
|
26
|
+
return true; // unparseable as a single expression -> let the legacy splitter have it
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
14
30
|
function handleEvents(body, counter, filePath, anonHandlers) {
|
|
15
31
|
let name = path.basename(filePath).replace(/\.html/i, "");
|
|
16
32
|
name = name.slice(0, 1).toLowerCase() + name.slice(1); // make compName camel case
|
|
@@ -26,6 +42,13 @@ function handleEvents(body, counter, filePath, anonHandlers) {
|
|
|
26
42
|
const handlerName = "__olumAnon_" + Math.random().toString(36).slice(2, 9);
|
|
27
43
|
if (anonHandlers) anonHandlers.push(`const ${handlerName} = ${attrValue};`);
|
|
28
44
|
attrValue = handlerName + "($event)";
|
|
45
|
+
} else if (!isSimpleCallList(attrValue)) {
|
|
46
|
+
// not a plain `name(args)` call list — e.g. a function prop call props().toggle(id)
|
|
47
|
+
// or a statement like state.count++ — wrap the whole expression in an anon handler
|
|
48
|
+
// so it executes as-is in component scope ($event = the DOM event)
|
|
49
|
+
const handlerName = "__olumAnon_" + Math.random().toString(36).slice(2, 9);
|
|
50
|
+
if (anonHandlers) anonHandlers.push(`const ${handlerName} = ($event) => { ${attrValue} };`);
|
|
51
|
+
attrValue = handlerName + "($event)";
|
|
29
52
|
}
|
|
30
53
|
|
|
31
54
|
let methods = attrValue.split("),");
|
package/lib/htmlParser.js
CHANGED
|
@@ -13,11 +13,11 @@ const handleCompPlaceholder = require("./compPlaceholder");
|
|
|
13
13
|
const handleHtml = require("./html");
|
|
14
14
|
const { RAW_OPEN, RAW_CLOSE, LT, GT } = require("./tokens");
|
|
15
15
|
|
|
16
|
-
function htmlparser(str, counter, filePath) {
|
|
16
|
+
function htmlparser(str, counter, filePath, parentMethods) {
|
|
17
17
|
let content = str.replace(script, "").replace(style, "");
|
|
18
18
|
content = clean(content); // clean html comments to avoid runtime errors
|
|
19
|
-
content = placeholder(content, selfClosingTag, "self");
|
|
20
|
-
content = placeholder(content, normalTag, "normal");
|
|
19
|
+
content = placeholder(content, selfClosingTag, "self", parentMethods);
|
|
20
|
+
content = placeholder(content, normalTag, "normal", parentMethods);
|
|
21
21
|
|
|
22
22
|
// Inline arrow/function event handlers (oninput="(e)=> expr") are extracted into named
|
|
23
23
|
// __olumAnon_ methods inside handleEvents (collected here, injected into the component JS later).
|
package/lib/parser.js
CHANGED
|
@@ -16,7 +16,14 @@ const parser = (str, counter, filePath) => {
|
|
|
16
16
|
// end
|
|
17
17
|
|
|
18
18
|
let { js, jsAttrs } = jsParser(str, counter, filePath); // js content and attributes in script opening tag
|
|
19
|
-
|
|
19
|
+
|
|
20
|
+
// top-level function names of THIS component, computed before htmlParser so placeholder.js can
|
|
21
|
+
// flag bare-identifier props referencing them (toggle="{toggle}") as function props — functions
|
|
22
|
+
// can't cross the JSON data-o-props channel, so they travel via data-o-props-src kind "method"
|
|
23
|
+
let parentMethods = [];
|
|
24
|
+
try { parentMethods = getGlobalMethods(js); } catch (e) {}
|
|
25
|
+
|
|
26
|
+
let {html, comps, anonHandlers} = htmlParser(str, counter, filePath, parentMethods); // html content and all components tags <olum></olum>
|
|
20
27
|
html = html.replace(/\"\-\[\[\-/g, "'").replace(/\-\]\]\-\"/g, "'"); // fix json in attribute issue
|
|
21
28
|
|
|
22
29
|
// Inject anonymous handler declarations so getGlobalMethods picks them up as regular methods
|
|
@@ -140,6 +147,7 @@ const parser = (str, counter, filePath) => {
|
|
|
140
147
|
__OLUM__: olum_${currentID},
|
|
141
148
|
el: el_${currentID},
|
|
142
149
|
incomingPropsProxy: olum.props(_storeKey),
|
|
150
|
+
methodsRef: methods_${currentID},
|
|
143
151
|
${isDev ? `stateProps: ${hasState ? "state" : "null"},` : ""}
|
|
144
152
|
hooks: {
|
|
145
153
|
mounted: typeof mounted !== "undefined" ? mounted : null,
|
package/lib/placeholder.js
CHANGED
|
@@ -16,7 +16,7 @@ function literalExpr(raw) {
|
|
|
16
16
|
return "'" + raw.replace(/'/g, "\\'") + "'";
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
function parseProps(attrsStr) {
|
|
19
|
+
function parseProps(attrsStr, parentMethods) {
|
|
20
20
|
const entries = [];
|
|
21
21
|
const srcEntries = []; // prop-name:state-key pairs for setter write-back
|
|
22
22
|
// six alternatives in priority order (groups kept intact for easy toggling):
|
|
@@ -48,6 +48,15 @@ function parseProps(attrsStr) {
|
|
|
48
48
|
}
|
|
49
49
|
if (!propName) continue;
|
|
50
50
|
if (isExpr) {
|
|
51
|
+
// FUNCTION PROP: a bare identifier naming a top-level function of THIS (parent) component,
|
|
52
|
+
// e.g. toggle="{toggle}". Functions can't cross the JSON data-o-props channel, so skip the
|
|
53
|
+
// JSON entry and record it in data-o-props-src as kind "method" — buildTree resolves it
|
|
54
|
+
// live from the parent instance's methodsRef on every render pass.
|
|
55
|
+
const bare = expr.trim();
|
|
56
|
+
if (parentMethods && parentMethods.includes(bare) && /^[A-Za-z_$][\w$]*$/.test(bare)) {
|
|
57
|
+
srcEntries.push(propName + ':method:' + bare);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
51
60
|
// object literal: restore {} wrappers, escaping { so htmlParser won't turn it into ${
|
|
52
61
|
const entryExpr = /^\s*\w+\s*:/.test(expr) ? ESC + expr + '}' : expr;
|
|
53
62
|
entries.push(propName + ': ' + entryExpr);
|
|
@@ -66,25 +75,29 @@ function parseProps(attrsStr) {
|
|
|
66
75
|
return { entries, srcEntries };
|
|
67
76
|
}
|
|
68
77
|
|
|
69
|
-
function buildPropsAttr(attrsStr) {
|
|
70
|
-
const { entries, srcEntries } = parseProps(attrsStr);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
let attr =
|
|
78
|
+
function buildPropsAttr(attrsStr, parentMethods) {
|
|
79
|
+
const { entries, srcEntries } = parseProps(attrsStr, parentMethods);
|
|
80
|
+
|
|
81
|
+
// emit the two attributes independently: a component may receive only function props
|
|
82
|
+
// (src entries, no JSON payload) or only data props (JSON payload, no src entries)
|
|
83
|
+
let attr = "";
|
|
84
|
+
if (entries.length) {
|
|
85
|
+
const expr = "$" + ESC + "encodeURIComponent(JSON.stringify(" + ESC + entries.join(", ") + "}))}";
|
|
86
|
+
attr += ' data-o-props="-[[-' + expr + '-]]-"';
|
|
87
|
+
}
|
|
75
88
|
if (srcEntries.length) attr += ' data-o-props-src="' + srcEntries.join("|") + '"';
|
|
76
89
|
return attr;
|
|
77
90
|
}
|
|
78
91
|
|
|
79
92
|
// handle components placeholders with olum tag <olum></olum>
|
|
80
|
-
function placeholder(content, regex, type) {
|
|
93
|
+
function placeholder(content, regex, type, parentMethods) {
|
|
81
94
|
return content.replace(regex, (str, $1, $2) => {
|
|
82
95
|
let data = $1.split(" ");
|
|
83
96
|
if (isFullArr(data)) {
|
|
84
97
|
let name = data[0].trim();
|
|
85
98
|
if (isUpper(name[0])) {
|
|
86
99
|
name = "name='" + name + "'";
|
|
87
|
-
const propsAttr = data.length > 1 ? buildPropsAttr(data.slice(1).join(" ")) : "";
|
|
100
|
+
const propsAttr = data.length > 1 ? buildPropsAttr(data.slice(1).join(" "), parentMethods) : "";
|
|
88
101
|
const slot = type === "normal" ? $2 : "";
|
|
89
102
|
return "<olum " + name + propsAttr + ">" + slot + "</olum>";
|
|
90
103
|
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "olum-compiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "A module for compiling/bundling olumjs projects",
|
|
5
5
|
"main": "bin/index.js",
|
|
6
6
|
"bin": {
|
|
7
7
|
"olum-compiler": "bin/index.js"
|
|
8
8
|
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "node ./tests/compiler.test.js"
|
|
11
|
+
},
|
|
9
12
|
"author": "Eissa Saber",
|
|
10
13
|
"license": "MIT",
|
|
11
14
|
"repository": {
|
|
@@ -0,0 +1,449 @@
|
|
|
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
|
+
const parser = require("../lib/parser");
|
|
12
|
+
|
|
13
|
+
let passed = 0;
|
|
14
|
+
let failed = 0;
|
|
15
|
+
|
|
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
|
+
const COLOR = process.stdout.isTTY && !process.env.NO_COLOR;
|
|
20
|
+
const paint = (code, s) => (COLOR ? `\x1b[${code}m${s}\x1b[0m` : s);
|
|
21
|
+
const green = (s) => paint("32", s);
|
|
22
|
+
const red = (s) => paint("31", s);
|
|
23
|
+
const dim = (s) => paint("2", s);
|
|
24
|
+
const bold = (s) => paint("1", s);
|
|
25
|
+
const yellow = (s) => paint("33", s);
|
|
26
|
+
const cyan = (s) => paint("36", s);
|
|
27
|
+
const PASS_ICON = green("✔");
|
|
28
|
+
const FAIL_ICON = red("✖");
|
|
29
|
+
|
|
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
|
+
let _idSeq = 1000;
|
|
33
|
+
function compile(template) {
|
|
34
|
+
return parser(template, _idSeq++, "App.html");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// compiled output is a parseable JS module (after stripping import/export)
|
|
38
|
+
function parses(out) {
|
|
39
|
+
const body = out
|
|
40
|
+
.replace(/^\s*import .*$/gm, "")
|
|
41
|
+
.replace(/^\s*export\s+default\s+/m, "const __x=");
|
|
42
|
+
new Function(body); // throws on syntax error
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// the rendered template literal region (between `el.innerHTML = ` and the injectStyle call)
|
|
47
|
+
function tmpl(out) {
|
|
48
|
+
const m = out.match(/innerHTML = `([\s\S]*?)`;\s*olum\.injectStyle/);
|
|
49
|
+
return m ? m[1] : "";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Current section, tracked so a failure can point at the category to look into.
|
|
53
|
+
let currentSection = "(no section)";
|
|
54
|
+
const failedSections = []; // { section, name } per failure, for the end-of-run recap
|
|
55
|
+
|
|
56
|
+
function recordFail(name) {
|
|
57
|
+
failed++;
|
|
58
|
+
failedSections.push({ section: currentSection, name });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function check(name, template, assertion) {
|
|
62
|
+
let out;
|
|
63
|
+
try {
|
|
64
|
+
out = compile(template);
|
|
65
|
+
} catch (e) {
|
|
66
|
+
recordFail(name);
|
|
67
|
+
console.log(" " + FAIL_ICON + " " + name + dim(" (threw: " + e.message + ")"));
|
|
68
|
+
console.log(" " + dim("↳ in " + currentSection));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
let ok = false;
|
|
72
|
+
let detail = "";
|
|
73
|
+
try {
|
|
74
|
+
ok = assertion(out);
|
|
75
|
+
} catch (e) {
|
|
76
|
+
detail = " (" + e.message + ")";
|
|
77
|
+
}
|
|
78
|
+
if (ok) {
|
|
79
|
+
passed++;
|
|
80
|
+
console.log(" " + PASS_ICON + " " + name);
|
|
81
|
+
} else {
|
|
82
|
+
recordFail(name);
|
|
83
|
+
// On a plain assertion miss (no thrown error) show the rendered template so a
|
|
84
|
+
// failure is diagnosable without re-instrumenting the test.
|
|
85
|
+
if (!detail) {
|
|
86
|
+
try {
|
|
87
|
+
const snippet = tmpl(out).trim().replace(/\s+/g, " ").slice(0, 140);
|
|
88
|
+
if (snippet) detail = "\n " + dim("template: " + snippet);
|
|
89
|
+
} catch (_) {}
|
|
90
|
+
} else {
|
|
91
|
+
detail = dim(detail);
|
|
92
|
+
}
|
|
93
|
+
console.log(" " + FAIL_ICON + " " + red(name) + detail);
|
|
94
|
+
console.log(" " + dim("↳ in " + currentSection));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function section(title) {
|
|
99
|
+
currentSection = title;
|
|
100
|
+
console.log("\n" + bold(cyan(title)));
|
|
101
|
+
}
|
|
102
|
+
|
|
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
|
+
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
|
+
function comp(template, extraScript) {
|
|
108
|
+
return `<script>\n${STATE}\n${extraScript || ""}\n</script>\n${template}`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
console.log("\n" + bold("🧪 OlumJS compiler fixtures") + "\n========================");
|
|
112
|
+
|
|
113
|
+
// ── §1 Component file structure ────────────────────────────────────────────
|
|
114
|
+
section("§1 Component file structure");
|
|
115
|
+
|
|
116
|
+
check(
|
|
117
|
+
"compiles to a parseable JS module",
|
|
118
|
+
comp(`<div class="box">{state.count}</div>`),
|
|
119
|
+
(out) => parses(out)
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
check(
|
|
123
|
+
"exports a default factory",
|
|
124
|
+
comp(`<div></div>`),
|
|
125
|
+
(out) => /export default \(_instanceKey\) =>/.test(out)
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
// ── §3 State & reactivity ──────────────────────────────────────────────────
|
|
129
|
+
section("§3 State & reactivity");
|
|
130
|
+
|
|
131
|
+
check(
|
|
132
|
+
"state is wrapped in olum.proxyHandler (reactive)",
|
|
133
|
+
comp(`<div></div>`),
|
|
134
|
+
(out) => /state = olum\.proxyHandler\(state,/.test(out) && parses(out)
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
// ── §4 Text interpolation {expr} (auto-escaped) ────────────────────────────
|
|
138
|
+
section("§4 Text interpolation");
|
|
139
|
+
|
|
140
|
+
check(
|
|
141
|
+
"{expr} in text is wrapped in olum.esc",
|
|
142
|
+
comp(`<p>{state.name}</p>`),
|
|
143
|
+
(out) => /\$\{olum\.esc\(state\.name\)\}/.test(tmpl(out))
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
check(
|
|
147
|
+
"expression with operators interpolates as-is (escaped)",
|
|
148
|
+
comp(`<p>{state.count + 1}</p>`),
|
|
149
|
+
(out) => /\$\{olum\.esc\(state\.count \+ 1\)\}/.test(tmpl(out)) && parses(out)
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
// ── §5 String attributes (literal + {expr}) ────────────────────────────────
|
|
153
|
+
section("§5 String attributes");
|
|
154
|
+
|
|
155
|
+
check(
|
|
156
|
+
"static attribute stays a literal string (no esc)",
|
|
157
|
+
comp(`<img alt="avatar" src="/logo.png" />`),
|
|
158
|
+
(out) => /alt="avatar"/.test(tmpl(out)) && !/olum\.esc\('avatar'\)/.test(tmpl(out))
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
check(
|
|
162
|
+
"{expr} inside a string attribute interpolates",
|
|
163
|
+
comp(`<div class="card {state.cls}"></div>`),
|
|
164
|
+
(out) => /class="card \$\{olum\.esc\(state\.cls\)\}"/.test(tmpl(out)) && parses(out)
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
check(
|
|
168
|
+
"dynamic string style interpolates",
|
|
169
|
+
comp(`<div style="color:{state.color}; padding:8px;"></div>`),
|
|
170
|
+
(out) => /style="color:\$\{olum\.esc\(state\.color\)\}; padding:8px;"/.test(tmpl(out)) && parses(out)
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
// ── §6 Events on* (code in "") ─────────────────────────────────────────────
|
|
174
|
+
section("§6 Events");
|
|
175
|
+
|
|
176
|
+
check(
|
|
177
|
+
"method-call handler compiles to data-o-event",
|
|
178
|
+
comp(`<button onclick="inc()">+</button>`, `const inc = () => state.count++;`),
|
|
179
|
+
(out) => /data-o-event=[^>]*onclick\|inc/.test(tmpl(out)) && parses(out)
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
check(
|
|
183
|
+
"multiple handlers in one attribute",
|
|
184
|
+
comp(`<button onclick="a(), b()">x</button>`, `const a = () => 0; const b = () => 0;`),
|
|
185
|
+
(out) => /onclick\|a=[^&]*&b=/.test(tmpl(out)) && parses(out)
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
check(
|
|
189
|
+
"$event is forwarded to the handler",
|
|
190
|
+
comp(`<input oninput="setVal($event)" />`, `const setVal = (e) => 0;`),
|
|
191
|
+
(out) => /oninput\|setVal=\$\{JSON\.stringify\(\['\$event'\]\)\}/.test(tmpl(out)) && parses(out)
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
check(
|
|
195
|
+
"inline arrow handler becomes a named __olumAnon_ method",
|
|
196
|
+
comp(`<input oninput="(e) => state.name = e.target.value" />`),
|
|
197
|
+
(out) => /__olumAnon_\w+/.test(out) && /data-o-event/.test(tmpl(out)) && parses(out)
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
// ── §7 Conditionals <if>/<else-if>/<else> ──────────────────────────────────
|
|
201
|
+
section("§7 Conditionals");
|
|
202
|
+
|
|
203
|
+
check(
|
|
204
|
+
"if/else-if/else compiles to a raw (un-escaped) ternary",
|
|
205
|
+
comp(`<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>`),
|
|
206
|
+
(out) => /\$\{state\.count === 1\?/.test(tmpl(out)) && out.indexOf("olum.esc(state.count === 1)") === -1 && parses(out)
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
check(
|
|
210
|
+
"comparison operators survive in a condition (no >)",
|
|
211
|
+
comp(`<if when="state.count > 0"><p>x</p></if>`),
|
|
212
|
+
(out) => /\$\{state\.count > 0\?/.test(tmpl(out)) && !/state\.count > 0/.test(tmpl(out)) && parses(out)
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
// ── §8 Show ────────────────────────────────────────────────────────────────
|
|
216
|
+
section("§8 Show");
|
|
217
|
+
|
|
218
|
+
check(
|
|
219
|
+
"<show> keeps the element and toggles display:none",
|
|
220
|
+
comp(`<show when="state.count"><span>v</span></show>`),
|
|
221
|
+
(out) => /\$\{state\.count\?/.test(tmpl(out)) && /display:none/.test(tmpl(out)) && parses(out)
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
// ── §9 Loops <for> ─────────────────────────────────────────────────────────
|
|
225
|
+
section("§9 Loops");
|
|
226
|
+
|
|
227
|
+
check(
|
|
228
|
+
'array loop "x of arr" compiles to .map()',
|
|
229
|
+
comp(`<for each="item of state.items"><li>{item.name}</li></for>`),
|
|
230
|
+
(out) => /state\.items\.map\(function\(item\)/.test(tmpl(out)) && parses(out)
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
check(
|
|
234
|
+
'numeric loop "i of N" is 1-based new Array(N)',
|
|
235
|
+
comp(`<for each="i of 6"><span>{i}</span></for>`),
|
|
236
|
+
(out) => /new Array\(6\)\.fill\(\)\.map/.test(tmpl(out)) && parses(out)
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
check(
|
|
240
|
+
'object loop "key in obj" compiles to Object.keys().map',
|
|
241
|
+
comp(`<for each="key in state.settings"><i>{key}</i></for>`),
|
|
242
|
+
(out) => /Object\.keys\(state\.settings\)\.map/.test(tmpl(out)) && parses(out)
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
check(
|
|
246
|
+
"keyed loop stamps data-o-key on a component placeholder",
|
|
247
|
+
comp(`<for each="item of state.items" key="item.id"><Row item="{item}" /></for>`, `import Row from "./Row";`),
|
|
248
|
+
(out) => /data-o-key/.test(tmpl(out)) && parses(out)
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
// ── §10 Raw HTML (html="expr" + olum.html()) ───────────────────────────────
|
|
252
|
+
section("§10 Raw HTML");
|
|
253
|
+
|
|
254
|
+
check(
|
|
255
|
+
'html="expr" sets unescaped ${expr} content, other attrs kept',
|
|
256
|
+
comp(`<div class="rich" html="state.html">fallback</div>`),
|
|
257
|
+
(out) => /<div class="rich">\$\{state\.html\}<\/div>/.test(tmpl(out)) && !/olum\.esc\(state\.html\)/.test(tmpl(out)) && parses(out)
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
check(
|
|
261
|
+
"olum.html(...) inline opt-out is preserved (esc detects it at runtime)",
|
|
262
|
+
comp(`<p>{olum.html(state.html)}</p>`),
|
|
263
|
+
(out) => /olum\.esc\(olum\.html\(state\.html\)\)/.test(tmpl(out)) && parses(out)
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
// ── §11 Components & props ──────────────────────────────────────────────────
|
|
267
|
+
section("§11 Components & props");
|
|
268
|
+
|
|
269
|
+
check(
|
|
270
|
+
"PascalCase component compiles to an <olum> placeholder",
|
|
271
|
+
comp(`<Badge />`, `import Badge from "./Badge";`),
|
|
272
|
+
(out) => /<olum name="Badge"/.test(tmpl(out)) && parses(out)
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
check(
|
|
276
|
+
"literal prop is a string",
|
|
277
|
+
comp(`<C title="Hello" />`, `import C from "./C";`),
|
|
278
|
+
(out) => /title: 'Hello'/.test(out) && parses(out)
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
check(
|
|
282
|
+
'whole-value prop "{expr}" keeps the expression type',
|
|
283
|
+
comp(`<C count="{state.count + 1}" data="{state.settings}" />`, `import C from "./C";`),
|
|
284
|
+
(out) => /count: state\.count \+ 1/.test(out) && /data: state\.settings/.test(out) && parses(out)
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
check(
|
|
288
|
+
"mixed-text prop becomes a template literal (string)",
|
|
289
|
+
comp(`<C greet="Hi {state.name}" />`, `import C from "./C";`),
|
|
290
|
+
(out) => /greet: `Hi \$\{state\.name\}`/.test(out) && parses(out)
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
check(
|
|
294
|
+
"passing state.X records a write-back source",
|
|
295
|
+
comp(`<C v="{state.count}" />`, `import C from "./C";`),
|
|
296
|
+
(out) => /data-o-props-src="v:state:count"/.test(out) && parses(out)
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
// ── §11b Function props ─────────────────────────────────────────────────────
|
|
300
|
+
section("§11b Function props");
|
|
301
|
+
|
|
302
|
+
check(
|
|
303
|
+
"a prop naming a top-level function is a method source, not a JSON entry",
|
|
304
|
+
comp(`<C todo="{state.items}" toggle="{toggle}" />`, `import C from "./C";\nconst toggle = (id) => id;`),
|
|
305
|
+
(out) => /toggle:method:toggle/.test(out) && /todo: state\.items/.test(out) && !/toggle: toggle/.test(out) && parses(out)
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
check(
|
|
309
|
+
"function prop can be renamed on the child",
|
|
310
|
+
comp(`<C onToggle="{toggle}" />`, `import C from "./C";\nfunction toggle(id) { return id; }`),
|
|
311
|
+
(out) => /data-o-props-src="onToggle:method:toggle"/.test(out) && parses(out)
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
check(
|
|
315
|
+
"function-only props emit data-o-props-src without data-o-props",
|
|
316
|
+
comp(`<C toggle="{toggle}" remove="{remove}" />`, `import C from "./C";\nconst toggle = (id) => id;\nconst remove = (id) => id;`),
|
|
317
|
+
(out) => /data-o-props-src="toggle:method:toggle\|remove:method:remove"/.test(out) && !/data-o-props=/.test(tmpl(out)) && parses(out)
|
|
318
|
+
);
|
|
319
|
+
|
|
320
|
+
check(
|
|
321
|
+
"a bare identifier NOT naming a function (e.g. loop var) stays a JSON prop",
|
|
322
|
+
comp(`<for each="item of state.items" key="item.id"><C item="{item}" /></for>`, `import C from "./C";`),
|
|
323
|
+
(out) => /item: item/.test(out) && !/item:method/.test(out) && parses(out)
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
check(
|
|
327
|
+
"forwarding a function prop records a props source (resolved live at runtime)",
|
|
328
|
+
comp(`<C fn="{props().onSave}" />`, `import C from "./C";\nimport { props } from "../core/olum.js";`),
|
|
329
|
+
(out) => /fn:props:onSave/.test(out) && parses(out)
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
check(
|
|
333
|
+
"calling a function prop in an event compiles to an anon handler",
|
|
334
|
+
comp(`<button onclick="props().toggle(props().todo.id)">x</button>`, `import { props } from "../core/olum.js";`),
|
|
335
|
+
(out) => /const __olumAnon_\w+ = \(\$event\) => \{ props\(_storeKey\)\.toggle\(props\(_storeKey\)\.todo\.id\) \};/.test(out) && parses(out)
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
check(
|
|
339
|
+
"plain method-call events still use the direct call-list path (no anon wrapper)",
|
|
340
|
+
comp(`<button onclick="inc(), dec($event)">x</button>`, `const inc = () => {};\nconst dec = (e) => {};`),
|
|
341
|
+
(out) => !/__olumAnon_/.test(out) && /onclick\|inc=/.test(out) && /dec=/.test(out) && parses(out)
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
// ── §12 Slots {children} ───────────────────────────────────────────────────
|
|
345
|
+
section("§12 Slots");
|
|
346
|
+
|
|
347
|
+
check(
|
|
348
|
+
"{children} compiles to the slot interpolation",
|
|
349
|
+
comp(`<div class="slot">{children}</div>`),
|
|
350
|
+
(out) => /<div class="slot">\$\{children\}<\/div>/.test(tmpl(out)) && parses(out)
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
// ── §14 Watchers ────────────────────────────────────────────────────────────
|
|
354
|
+
section("§14 Watchers");
|
|
355
|
+
|
|
356
|
+
check(
|
|
357
|
+
"a watcher is wired into proxyHandler",
|
|
358
|
+
comp(`<div></div>`, `const watcher = { count(o, n) {} };`),
|
|
359
|
+
(out) => /olum\.proxyHandler\(state, ?watcher,/.test(out) && parses(out)
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
check(
|
|
363
|
+
"no watcher -> proxyHandler gets null",
|
|
364
|
+
comp(`<div></div>`),
|
|
365
|
+
(out) => /olum\.proxyHandler\(state, ?null,/.test(out) && parses(out)
|
|
366
|
+
);
|
|
367
|
+
|
|
368
|
+
// ── §15 Lifecycle hooks ─────────────────────────────────────────────────────
|
|
369
|
+
section("§15 Lifecycle hooks");
|
|
370
|
+
|
|
371
|
+
check(
|
|
372
|
+
"mounted hook is resolved at runtime via a typeof guard",
|
|
373
|
+
comp(`<div></div>`, `const mounted = () => 0; const unMounted = () => 0;`),
|
|
374
|
+
(out) => /mounted: ?typeof mounted !== "undefined" \? mounted : null/.test(out) && parses(out)
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
check(
|
|
378
|
+
"unMounted is emitted as a null placeholder (assigned at runtime)",
|
|
379
|
+
comp(`<div></div>`),
|
|
380
|
+
(out) => /unMounted: ?null/.test(out) && parses(out)
|
|
381
|
+
);
|
|
382
|
+
|
|
383
|
+
// ── §16 Scoped CSS ──────────────────────────────────────────────────────────
|
|
384
|
+
section("§16 Scoped CSS");
|
|
385
|
+
|
|
386
|
+
check(
|
|
387
|
+
"styles are scoped with a [data-o-*] prefix and injected",
|
|
388
|
+
comp(`<div class="box"></div>`).replace("</script>", "</script>\n<style>.box{color:red}</style>"),
|
|
389
|
+
(out) => /\[data-o-\w+\] \.box/.test(out) && /olum\.injectStyle\(/.test(out) && parses(out)
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
// ── §17 Imports ─────────────────────────────────────────────────────────────
|
|
393
|
+
section("§17 Imports");
|
|
394
|
+
|
|
395
|
+
check(
|
|
396
|
+
"component import is preserved (resolved to .js) and registered",
|
|
397
|
+
comp(`<Badge />`, `import Badge from "./Badge";`),
|
|
398
|
+
(out) => /import Badge from "\.\/Badge\.js"/.test(out) && /components:\s*\{[^}]*Badge\s*:\s*Badge/.test(out) && parses(out)
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
// ── §19 Unsupported prop syntax (negative tests) ───────────────────────────
|
|
402
|
+
// NOTE: tests for commented-out/disabled compiler passes (:style, model, mode) were
|
|
403
|
+
// removed — a disabled feature has no stable contract to assert against. The checks
|
|
404
|
+
// below cover the parser's *active* behavior: brace-prop shorthand is silently ignored.
|
|
405
|
+
section("§19 Unsupported prop syntax");
|
|
406
|
+
|
|
407
|
+
check(
|
|
408
|
+
"naked-brace prop shorthand <Comp {a}/> is ignored (no props)",
|
|
409
|
+
comp(`<C {state} />`, `import C from "./C";`),
|
|
410
|
+
// positive companion (<olum name="C">) proves the component WAS processed and the
|
|
411
|
+
// brace syntax was merely dropped — without it the negative could pass vacuously.
|
|
412
|
+
(out) => /<olum name="C"/.test(tmpl(out)) && !/data-o-props=/.test(tmpl(out)) && parses(out)
|
|
413
|
+
);
|
|
414
|
+
|
|
415
|
+
check(
|
|
416
|
+
"unquoted brace prop <Comp a={a}/> is ignored (no props)",
|
|
417
|
+
comp(`<C a={state} />`, `import C from "./C";`),
|
|
418
|
+
(out) => /<olum name="C"/.test(tmpl(out)) && !/data-o-props=/.test(tmpl(out)) && parses(out)
|
|
419
|
+
);
|
|
420
|
+
|
|
421
|
+
console.log("\n========================");
|
|
422
|
+
const summary = `${passed} passed, ${failed} failed`;
|
|
423
|
+
console.log((failed ? red(bold(summary)) : green(bold(summary))) + "\n");
|
|
424
|
+
|
|
425
|
+
// Recap which section(s) to look into, grouped so a run with several failures is
|
|
426
|
+
// still scannable ("go check §11b").
|
|
427
|
+
if (failed) {
|
|
428
|
+
const bySection = {};
|
|
429
|
+
failedSections.forEach(({ section, name }) => {
|
|
430
|
+
(bySection[section] = bySection[section] || []).push(name);
|
|
431
|
+
});
|
|
432
|
+
console.log(bold("Failed in:"));
|
|
433
|
+
Object.keys(bySection).forEach((sec) => {
|
|
434
|
+
console.log(" " + red(sec));
|
|
435
|
+
bySection[sec].forEach((name) => console.log(" " + dim("• " + name)));
|
|
436
|
+
});
|
|
437
|
+
console.log("");
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Guard against a whole section silently disappearing (a bad merge, a `check` that
|
|
441
|
+
// throws before registering, etc.). Bump EXPECTED_CHECKS when you add/remove tests.
|
|
442
|
+
const EXPECTED_CHECKS = 42;
|
|
443
|
+
const total = passed + failed;
|
|
444
|
+
if (total !== EXPECTED_CHECKS) {
|
|
445
|
+
console.log(yellow(`⚠ ran ${total} checks but expected ${EXPECTED_CHECKS} — did a test get dropped?`) + "\n");
|
|
446
|
+
process.exit(1);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
process.exit(failed ? 1 : 0);
|