@tylix/compiler 0.5.2 → 0.7.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/LICENSE +21 -0
- package/package.json +30 -2
- package/src/ast/nodes.js +219 -16
- package/src/ast/substituteCapabilityRefs.js +185 -61
- package/src/codegen/emitStatementLines.js +98 -54
- package/src/codegen/generateExpression.js +339 -36
- package/src/codegen/generateMethod.js +5 -3
- package/src/codegen/generatePage.js +74 -44
- package/src/codegen/generateStatement.js +169 -20
- package/src/codegen/generateTemplate.js +183 -101
- package/src/codegen/generateTemplateExpression.js +102 -59
- package/src/codegen/jsGlobals.js +211 -0
- package/src/compileComponent.js +39 -8
- package/src/imports/extractImports.js +112 -26
- package/src/imports/resolveImports.js +186 -0
- package/src/index.js +3 -2
- package/src/lexer/Lexer.js +430 -235
- package/src/lexer/tokenTypes.js +32 -7
- package/src/parser/Parser.js +2176 -325
- package/src/parser/languageOptions.js +16 -0
- package/src/parser/parseExpressionString.js +23 -7
- package/src/parser/parsePageFile.js +139 -58
- package/src/parser/parseTemplate.js +350 -62
- package/src/renderPageDocument.js +338 -56
- package/src/runtime/capability.js +39 -17
- package/src/runtime/loadingIndicator.js +91 -29
- package/src/runtime/reactive.js +362 -64
- package/src/runtime/useApi.js +74 -13
- package/src/runtime/useChannel.js +27 -6
- package/src/typecheck/buildVirtualTs.js +23 -12
- package/src/typecheck/tylix-globals.d.ts +23 -4
- package/src/typecheck/typedTemplateExpr.js +195 -53
- package/src/typecheckPage.js +28 -4
- package/src/codegen/generateExpression.array.test.js +0 -13
- package/src/codegen/generateExpression.callexpr.test.js +0 -19
- package/src/codegen/generateExpression.test.js +0 -59
- package/src/codegen/generateMethod.asyncawait.test.js +0 -37
- package/src/codegen/generateMethod.ifstatement.test.js +0 -20
- package/src/codegen/generateMethod.test.js +0 -81
- package/src/codegen/generatePage.onmount.test.js +0 -46
- package/src/codegen/generatePage.test.js +0 -138
- package/src/codegen/generateStatement.test.js +0 -31
- package/src/codegen/generateTemplate.ifeach.test.js +0 -138
- package/src/codegen/generateTemplate.test.js +0 -149
- package/src/codegen/generateTemplateExpression.test.js +0 -34
- package/src/compileComponent.test.js +0 -121
- package/src/imports/resolvePackageComponents.js +0 -41
- package/src/lexer/Lexer.test.js +0 -151
- package/src/parser/Parser.array.test.js +0 -37
- package/src/parser/Parser.asyncawait.test.js +0 -33
- package/src/parser/Parser.ifstatement.test.js +0 -20
- package/src/parser/Parser.onmount.test.js +0 -18
- package/src/parser/Parser.test.js +0 -315
- package/src/parser/parseComponent.test.js +0 -53
- package/src/parser/parsePageFile.test.js +0 -101
- package/src/parser/parseTemplate.ifeach.test.js +0 -33
- package/src/parser/parseTemplate.pre.test.js +0 -39
- package/src/parser/parseTemplate.test.js +0 -112
- package/src/renderPageDocument.test.js +0 -125
- package/src/runtime/reactive.test.js +0 -106
- package/test/dashboard-syntax.test.js +0 -116
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { generatePage } from "./generatePage.js";
|
|
4
|
-
import { reactive } from "../runtime/reactive.js";
|
|
5
|
-
import { Parser } from "../parser/Parser.js";
|
|
6
|
-
import { Lexer } from "../lexer/Lexer.js";
|
|
7
|
-
|
|
8
|
-
function buildPageClass(source) {
|
|
9
|
-
const page = new Parser(new Lexer(source).tokenize()).parse();
|
|
10
|
-
const classSource = generatePage(page, "Page");
|
|
11
|
-
return new Function("reactive", `return ${classSource}`)(reactive);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// onMount is not invoked from the constructor: the host awaits
|
|
15
|
-
// __ready() (so capabilities are loaded) and then calls __onMount().
|
|
16
|
-
test("onMount does not run on construction", () => {
|
|
17
|
-
const Page = buildPageClass(`state\ncount: 0\nonMount\nthis.count = 99`);
|
|
18
|
-
|
|
19
|
-
const instance = new Page();
|
|
20
|
-
assert.equal(instance.count, 0);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test("onMount runs when the host calls __onMount()", () => {
|
|
24
|
-
const Page = buildPageClass(`state\ncount: 0\nonMount\nthis.count = 99`);
|
|
25
|
-
|
|
26
|
-
const instance = new Page();
|
|
27
|
-
instance.__onMount();
|
|
28
|
-
assert.equal(instance.count, 99);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test("async onMount can await before setting state", async () => {
|
|
32
|
-
const source = `state\ndata: 0\nonMount\nasync {\n const value = await this.fetchIt()\n this.data = value\n}`;
|
|
33
|
-
const page = new Parser(new Lexer(source).tokenize()).parse();
|
|
34
|
-
const classSource = generatePage(page, "Page");
|
|
35
|
-
|
|
36
|
-
const PageClass = new Function(
|
|
37
|
-
"reactive",
|
|
38
|
-
`const Page = ${classSource};
|
|
39
|
-
Page.prototype.fetchIt = async function () { return 123; };
|
|
40
|
-
return Page;`
|
|
41
|
-
)(reactive);
|
|
42
|
-
|
|
43
|
-
const instance = new PageClass();
|
|
44
|
-
await instance.__onMount();
|
|
45
|
-
assert.equal(instance.data, 123);
|
|
46
|
-
});
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { test } from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { generatePage } from './generatePage.js'
|
|
4
|
-
import { reactive, effect } from '../runtime/reactive.js'
|
|
5
|
-
import { Parser } from '../parser/Parser.js'
|
|
6
|
-
import { Lexer } from '../lexer/Lexer.js'
|
|
7
|
-
|
|
8
|
-
function compileAndLoad(source, className = 'Page') {
|
|
9
|
-
const tokens = new Lexer(source).tokenize()
|
|
10
|
-
const page = new Parser(tokens).parse()
|
|
11
|
-
const classSource = generatePage(page, className)
|
|
12
|
-
// reactive must be in scope for the generated class body to run
|
|
13
|
-
return new Function('reactive', `return ${classSource}`)(reactive)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
test('generates working state initializers for array, object, and null values, not just numbers/strings', () => {
|
|
17
|
-
const source = `
|
|
18
|
-
state
|
|
19
|
-
items: []
|
|
20
|
-
config: { enabled: true }
|
|
21
|
-
selectedId: null
|
|
22
|
-
`
|
|
23
|
-
const Page = compileAndLoad(source, 'TestPage')
|
|
24
|
-
const instance = new Page()
|
|
25
|
-
|
|
26
|
-
assert.deepEqual(instance.items, [])
|
|
27
|
-
assert.deepEqual(instance.config, { enabled: true })
|
|
28
|
-
assert.equal(instance.selectedId, null)
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
test('generates a class with reactive state accessible via this.<name>', () => {
|
|
32
|
-
const Page = compileAndLoad(`state { count: 0 }`)
|
|
33
|
-
const instance = new Page()
|
|
34
|
-
assert.equal(instance.count, 0)
|
|
35
|
-
instance.count = 5
|
|
36
|
-
assert.equal(instance.count, 5)
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
test('state writes trigger reactive effects (proves no manual re-render wiring needed)', () => {
|
|
40
|
-
const Page = compileAndLoad(`state { count: 0 }`)
|
|
41
|
-
const instance = new Page()
|
|
42
|
-
let seen = null
|
|
43
|
-
|
|
44
|
-
effect(() => {
|
|
45
|
-
seen = instance.count
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
assert.equal(seen, 0)
|
|
49
|
-
instance.count = 42
|
|
50
|
-
assert.equal(seen, 42)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
test('generates working action methods that mutate state', () => {
|
|
54
|
-
const source = `
|
|
55
|
-
state { count: 0 }
|
|
56
|
-
action {
|
|
57
|
-
increment() {
|
|
58
|
-
this.count = this.count + 1;
|
|
59
|
-
}
|
|
60
|
-
}`
|
|
61
|
-
const Page = compileAndLoad(source)
|
|
62
|
-
const instance = new Page()
|
|
63
|
-
|
|
64
|
-
instance.increment()
|
|
65
|
-
assert.equal(instance.count, 1)
|
|
66
|
-
instance.increment()
|
|
67
|
-
assert.equal(instance.count, 2)
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
test('generates working computed getters derived from state', () => {
|
|
71
|
-
const source = `
|
|
72
|
-
state { count: 3 }
|
|
73
|
-
computed {
|
|
74
|
-
doubled() {
|
|
75
|
-
return this.count * 2;
|
|
76
|
-
}
|
|
77
|
-
}`
|
|
78
|
-
const Page = compileAndLoad(source)
|
|
79
|
-
const instance = new Page()
|
|
80
|
-
|
|
81
|
-
assert.equal(instance.doubled, 6)
|
|
82
|
-
instance.count = 10
|
|
83
|
-
assert.equal(instance.doubled, 20)
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
test('computed getters are reactive when read inside an effect', () => {
|
|
87
|
-
const source = `
|
|
88
|
-
state { count: 3 }
|
|
89
|
-
computed {
|
|
90
|
-
doubled() {
|
|
91
|
-
return this.count * 2;
|
|
92
|
-
}
|
|
93
|
-
}`
|
|
94
|
-
const Page = compileAndLoad(source)
|
|
95
|
-
const instance = new Page()
|
|
96
|
-
let seenDoubled = null
|
|
97
|
-
|
|
98
|
-
effect(() => {
|
|
99
|
-
seenDoubled = instance.doubled
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
assert.equal(seenDoubled, 6)
|
|
103
|
-
instance.count = 5
|
|
104
|
-
assert.equal(seenDoubled, 10)
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
test('generates a full realistic counter page end to end', () => {
|
|
108
|
-
const source = `
|
|
109
|
-
state { count: 0 }
|
|
110
|
-
computed {
|
|
111
|
-
doubled() {
|
|
112
|
-
return this.count * 2;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
action {
|
|
116
|
-
increment() {
|
|
117
|
-
this.count = this.count + 1;
|
|
118
|
-
}
|
|
119
|
-
reset() {
|
|
120
|
-
this.count = 0;
|
|
121
|
-
}
|
|
122
|
-
}`
|
|
123
|
-
const Page = compileAndLoad(source, 'Counter')
|
|
124
|
-
const instance = new Page()
|
|
125
|
-
|
|
126
|
-
assert.equal(instance.count, 0)
|
|
127
|
-
assert.equal(instance.doubled, 0)
|
|
128
|
-
|
|
129
|
-
instance.increment()
|
|
130
|
-
instance.increment()
|
|
131
|
-
instance.increment()
|
|
132
|
-
|
|
133
|
-
assert.equal(instance.count, 3)
|
|
134
|
-
assert.equal(instance.doubled, 6)
|
|
135
|
-
|
|
136
|
-
instance.reset()
|
|
137
|
-
assert.equal(instance.count, 0)
|
|
138
|
-
})
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { generateStatement } from "./generateStatement.js";
|
|
4
|
-
import {
|
|
5
|
-
ReturnStatement,
|
|
6
|
-
ExpressionStatement,
|
|
7
|
-
AssignmentExpr,
|
|
8
|
-
BinaryExpr,
|
|
9
|
-
MemberExpr,
|
|
10
|
-
Identifier,
|
|
11
|
-
Literal,
|
|
12
|
-
} from "../ast/nodes.js";
|
|
13
|
-
|
|
14
|
-
test("generates a return statement", () => {
|
|
15
|
-
const node = ReturnStatement(BinaryExpr("*", MemberExpr(Identifier("this"), "count"), Literal(2)));
|
|
16
|
-
assert.equal(generateStatement(node), "return (this.count * 2);");
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test("generates an expression statement wrapping an assignment", () => {
|
|
20
|
-
const node = ExpressionStatement(
|
|
21
|
-
AssignmentExpr(
|
|
22
|
-
MemberExpr(Identifier("this"), "count"),
|
|
23
|
-
BinaryExpr("+", MemberExpr(Identifier("this"), "count"), Literal(1))
|
|
24
|
-
)
|
|
25
|
-
);
|
|
26
|
-
assert.equal(generateStatement(node), "this.count = (this.count + 1);");
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test("throws on an unknown statement type", () => {
|
|
30
|
-
assert.throws(() => generateStatement({ type: "Bogus" }), /unknown node type "Bogus"/);
|
|
31
|
-
});
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { JSDOM } from "jsdom";
|
|
4
|
-
import { generateTemplate } from "./generateTemplate.js";
|
|
5
|
-
import { parseTemplate } from "../parser/parseTemplate.js";
|
|
6
|
-
import { reactive, effect } from "../runtime/reactive.js";
|
|
7
|
-
|
|
8
|
-
function renderToDom(templateSource, instance) {
|
|
9
|
-
const nodes = parseTemplate(templateSource);
|
|
10
|
-
const { code, rootVar } = generateTemplate(nodes);
|
|
11
|
-
const dom = new JSDOM();
|
|
12
|
-
const { document } = dom.window;
|
|
13
|
-
const fn = new Function("document", "effect", "instance", "components", `${code}\nreturn ${rootVar};`);
|
|
14
|
-
const rootNode = fn(document, effect, instance, {});
|
|
15
|
-
return { rootNode, document };
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
test("renders the if branch when condition is true, hides it when false", () => {
|
|
19
|
-
const instance = reactive({ visible: true });
|
|
20
|
-
const { rootNode } = renderToDom(`<div>{{#if visible}}<p>Shown</p>{{/if}}</div>`, instance);
|
|
21
|
-
|
|
22
|
-
assert.equal(rootNode.querySelector("p")?.textContent, "Shown");
|
|
23
|
-
instance.visible = false;
|
|
24
|
-
assert.equal(rootNode.querySelector("p"), null);
|
|
25
|
-
instance.visible = true;
|
|
26
|
-
assert.equal(rootNode.querySelector("p")?.textContent, "Shown");
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test("renders a list with {{#each}}, using the loop variable bare (not instance.<var>)", () => {
|
|
30
|
-
const instance = reactive({ posts: [{ title: "First" }, { title: "Second" }] });
|
|
31
|
-
const { rootNode } = renderToDom(
|
|
32
|
-
`<ul>{{#each post in posts}}<li>{{ post.title }}</li>{{/each}}</ul>`,
|
|
33
|
-
instance
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
const items = rootNode.querySelectorAll("li");
|
|
37
|
-
assert.equal(items.length, 2);
|
|
38
|
-
assert.equal(items[0].textContent, "First");
|
|
39
|
-
assert.equal(items[1].textContent, "Second");
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
test("each list rebuilds correctly when the underlying array changes", () => {
|
|
43
|
-
const instance = reactive({ posts: [{ title: "A" }] });
|
|
44
|
-
const { rootNode } = renderToDom(
|
|
45
|
-
`<ul>{{#each post in posts}}<li>{{ post.title }}</li>{{/each}}</ul>`,
|
|
46
|
-
instance
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
assert.equal(rootNode.querySelectorAll("li").length, 1);
|
|
50
|
-
instance.posts = [{ title: "X" }, { title: "Y" }, { title: "Z" }];
|
|
51
|
-
const items = rootNode.querySelectorAll("li");
|
|
52
|
-
assert.equal(items.length, 3);
|
|
53
|
-
assert.equal(items[1].textContent, "Y");
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test("nested if inside each correctly filters items reactively", () => {
|
|
57
|
-
const instance = reactive({
|
|
58
|
-
posts: [
|
|
59
|
-
{ title: "Visible", published: true },
|
|
60
|
-
{ title: "Hidden", published: false },
|
|
61
|
-
],
|
|
62
|
-
});
|
|
63
|
-
const { rootNode } = renderToDom(
|
|
64
|
-
`<ul>{{#each post in posts}}{{#if post.published}}<li>{{ post.title }}</li>{{/if}}{{/each}}</ul>`,
|
|
65
|
-
instance
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
const items = rootNode.querySelectorAll("li");
|
|
69
|
-
assert.equal(items.length, 1);
|
|
70
|
-
assert.equal(items[0].textContent, "Visible");
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
test("event binding with a CallExpression passes explicit arguments, not the DOM event", () => {
|
|
74
|
-
const instance = reactive({
|
|
75
|
-
posts: [{ id: 1, title: "A" }, { id: 2, title: "B" }],
|
|
76
|
-
});
|
|
77
|
-
const removed = [];
|
|
78
|
-
instance.remove = function (id) {
|
|
79
|
-
removed.push(id);
|
|
80
|
-
this.posts = this.posts.filter((p) => p.id !== id);
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const { rootNode, document } = renderToDom(
|
|
84
|
-
`<ul>{{#each post in posts}}<li>{{ post.title }}<button onclick="{{ remove(post.id) }}">x</button></li>{{/each}}</ul>`,
|
|
85
|
-
instance
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
const firstButton = rootNode.querySelectorAll("button")[0];
|
|
89
|
-
firstButton.dispatchEvent(new document.defaultView.Event("click"));
|
|
90
|
-
|
|
91
|
-
assert.deepEqual(removed, [1]);
|
|
92
|
-
assert.equal(rootNode.querySelectorAll("li").length, 1);
|
|
93
|
-
assert.equal(rootNode.querySelector("li").textContent.includes("B"), true);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
test("event is available as a bare scoped identifier for oninput handlers with arguments", () => {
|
|
97
|
-
const instance = reactive({ name: "" });
|
|
98
|
-
instance.setField = function (field, value) {
|
|
99
|
-
this[field] = value;
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const nodes = parseTemplate(`<input oninput="{{ setField('name', event.target.value) }}" />`);
|
|
103
|
-
const { code, rootVar } = generateTemplate(nodes);
|
|
104
|
-
|
|
105
|
-
const dom = new JSDOM();
|
|
106
|
-
const { document } = dom.window;
|
|
107
|
-
const fn = new Function("document", "effect", "instance", "components", `${code}\nreturn ${rootVar};`);
|
|
108
|
-
const inputEl = fn(document, effect, instance, {});
|
|
109
|
-
|
|
110
|
-
document.body.appendChild(inputEl);
|
|
111
|
-
inputEl.value = "Ada";
|
|
112
|
-
inputEl.dispatchEvent(new document.defaultView.Event("input"));
|
|
113
|
-
|
|
114
|
-
assert.equal(instance.name, "Ada");
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
test("event can be passed as an explicit argument and calls preventDefault on the real DOM event", () => {
|
|
118
|
-
let prevented = false;
|
|
119
|
-
const instance = reactive({});
|
|
120
|
-
instance.create = function (event) {
|
|
121
|
-
event.preventDefault();
|
|
122
|
-
prevented = true;
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
const nodes = parseTemplate(`<form onsubmit="{{ create(event) }}"></form>`);
|
|
126
|
-
const { code, rootVar } = generateTemplate(nodes);
|
|
127
|
-
|
|
128
|
-
const dom = new JSDOM();
|
|
129
|
-
const { document } = dom.window;
|
|
130
|
-
const fn = new Function("document", "effect", "instance", "components", `${code}
|
|
131
|
-
return ${rootVar};`);
|
|
132
|
-
const formEl = fn(document, effect, instance, {});
|
|
133
|
-
|
|
134
|
-
document.body.appendChild(formEl);
|
|
135
|
-
formEl.dispatchEvent(new document.defaultView.Event("submit", { cancelable: true }));
|
|
136
|
-
|
|
137
|
-
assert.equal(prevented, true);
|
|
138
|
-
});
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { test } from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { JSDOM } from 'jsdom'
|
|
4
|
-
import { generateTemplate } from './generateTemplate.js'
|
|
5
|
-
import { parseTemplate } from '../parser/parseTemplate.js'
|
|
6
|
-
import { compileComponent } from '../compileComponent.js'
|
|
7
|
-
import { reactive, effect } from '../runtime/reactive.js'
|
|
8
|
-
|
|
9
|
-
function renderToDom(templateSource, instance) {
|
|
10
|
-
const nodes = parseTemplate(templateSource)
|
|
11
|
-
const { code, rootVar } = generateTemplate(nodes)
|
|
12
|
-
|
|
13
|
-
const dom = new JSDOM()
|
|
14
|
-
const { document } = dom.window
|
|
15
|
-
|
|
16
|
-
const fn = new Function(
|
|
17
|
-
'document',
|
|
18
|
-
'effect',
|
|
19
|
-
'instance',
|
|
20
|
-
`${code}\nreturn ${rootVar};`,
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
const rootNode = fn(document, effect, instance)
|
|
24
|
-
return { rootNode, document }
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
test('compiles static text into a real text node', () => {
|
|
28
|
-
const { rootNode } = renderToDom(`<p>Hello Tylix</p>`, {})
|
|
29
|
-
assert.equal(rootNode.tagName, 'P')
|
|
30
|
-
assert.equal(rootNode.textContent, 'Hello Tylix')
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
test('compiles an interpolation into a live-updating text node', () => {
|
|
34
|
-
const instance = reactive({ count: 0 })
|
|
35
|
-
const { rootNode } = renderToDom(`<span>{{ count }}</span>`, instance)
|
|
36
|
-
|
|
37
|
-
assert.equal(rootNode.textContent, '0')
|
|
38
|
-
instance.count = 5
|
|
39
|
-
assert.equal(rootNode.textContent, '5')
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
test('compiles nested elements with mixed text and interpolation', () => {
|
|
43
|
-
const instance = reactive({ name: 'Ada' })
|
|
44
|
-
const { rootNode } = renderToDom(
|
|
45
|
-
`<div><h1>Hello {{ name }}</h1></div>`,
|
|
46
|
-
instance,
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
const h1 = rootNode.querySelector('h1')
|
|
50
|
-
assert.equal(h1.textContent, 'Hello Ada')
|
|
51
|
-
instance.name = 'Grace'
|
|
52
|
-
assert.equal(h1.textContent, 'Hello Grace')
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
test('compiles a static attribute', () => {
|
|
56
|
-
const { rootNode } = renderToDom(`<div class="box"></div>`, {})
|
|
57
|
-
assert.equal(rootNode.getAttribute('class'), 'box')
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test('compiles a dynamic attribute that updates reactively', () => {
|
|
61
|
-
const instance = reactive({ theme: 'dark' })
|
|
62
|
-
const { rootNode } = renderToDom(`<div class="{{ theme }}"></div>`, instance)
|
|
63
|
-
|
|
64
|
-
assert.equal(rootNode.getAttribute('class'), 'dark')
|
|
65
|
-
instance.theme = 'light'
|
|
66
|
-
assert.equal(rootNode.getAttribute('class'), 'light')
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test('compiles an event binding that calls the bound instance method', () => {
|
|
70
|
-
let clicked = false
|
|
71
|
-
const instance = {
|
|
72
|
-
handleClick: () => {
|
|
73
|
-
clicked = true
|
|
74
|
-
},
|
|
75
|
-
}
|
|
76
|
-
const { rootNode, document } = renderToDom(
|
|
77
|
-
`<button onclick="{{ handleClick }}">Go</button>`,
|
|
78
|
-
instance,
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
document.body.appendChild(rootNode)
|
|
82
|
-
rootNode.dispatchEvent(new document.defaultView.Event('click'))
|
|
83
|
-
|
|
84
|
-
assert.equal(clicked, true)
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
test('compiles the full realistic counter template with a live click handler', () => {
|
|
88
|
-
const instance = reactive({ count: 0 })
|
|
89
|
-
instance.increment = function () {
|
|
90
|
-
this.count = this.count + 1
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const { rootNode, document } = renderToDom(
|
|
94
|
-
`<div><span>{{ count }}</span><button onclick="{{ increment }}">+1</button></div>`,
|
|
95
|
-
instance,
|
|
96
|
-
)
|
|
97
|
-
|
|
98
|
-
test("parses a bare boolean attribute with no '=' as AttributeNode(name, true, false)", () => {
|
|
99
|
-
const nodes = parseTemplate(`<input disabled />`)
|
|
100
|
-
const attr = nodes[0].attributes.find((a) => a.name === 'disabled')
|
|
101
|
-
assert.equal(attr.value, true)
|
|
102
|
-
assert.equal(attr.dynamic, false)
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
document.body.appendChild(rootNode)
|
|
106
|
-
const span = rootNode.querySelector('span')
|
|
107
|
-
const button = rootNode.querySelector('button')
|
|
108
|
-
|
|
109
|
-
assert.equal(span.textContent, '0')
|
|
110
|
-
button.dispatchEvent(new document.defaultView.Event('click'))
|
|
111
|
-
assert.equal(span.textContent, '1')
|
|
112
|
-
button.dispatchEvent(new document.defaultView.Event('click'))
|
|
113
|
-
assert.equal(span.textContent, '2')
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
test('throws a clear error for a non-{{}} event attribute', () => {
|
|
117
|
-
assert.throws(
|
|
118
|
-
() =>
|
|
119
|
-
generateTemplate(parseTemplate(`<button onclick="doStuff">Go</button>`)),
|
|
120
|
-
/must use \{\{ \}\} binding/,
|
|
121
|
-
)
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
test('compiles a capitalized tag as a component reference, not an HTML element', () => {
|
|
125
|
-
const badgeSource = `
|
|
126
|
-
<template>
|
|
127
|
-
<span class="badge">Verified</span>
|
|
128
|
-
</template>
|
|
129
|
-
`
|
|
130
|
-
const Badge = compileComponent(badgeSource, 'Badge')
|
|
131
|
-
|
|
132
|
-
const nodes = parseTemplate(`<div><Badge /></div>`)
|
|
133
|
-
const { code, rootVar } = generateTemplate(nodes)
|
|
134
|
-
|
|
135
|
-
const dom = new JSDOM()
|
|
136
|
-
const { document } = dom.window
|
|
137
|
-
|
|
138
|
-
const fn = new Function(
|
|
139
|
-
'document',
|
|
140
|
-
'effect',
|
|
141
|
-
'instance',
|
|
142
|
-
'components',
|
|
143
|
-
`${code}\nreturn ${rootVar};`,
|
|
144
|
-
)
|
|
145
|
-
|
|
146
|
-
const rootNode = fn(document, () => {}, {}, { Badge })
|
|
147
|
-
const badgeSpan = rootNode.querySelector('span.badge')
|
|
148
|
-
assert.equal(badgeSpan.textContent, 'Verified')
|
|
149
|
-
})
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { generateTemplateExpression } from "./generateTemplateExpression.js";
|
|
4
|
-
import { Identifier, BinaryExpr, MemberExpr, Literal } from "../ast/nodes.js";
|
|
5
|
-
|
|
6
|
-
test("prefixes a bare identifier with instance.", () => {
|
|
7
|
-
assert.equal(generateTemplateExpression(Identifier("count")), "instance.count");
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
test("prefixes the root of a member expression", () => {
|
|
11
|
-
const node = MemberExpr(Identifier("user"), "name");
|
|
12
|
-
assert.equal(generateTemplateExpression(node), "instance.user.name");
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test("prefixes both sides of a binary expression", () => {
|
|
16
|
-
const node = BinaryExpr("*", Identifier("count"), Literal(2));
|
|
17
|
-
assert.equal(generateTemplateExpression(node), "(instance.count * 2)");
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test("passes through a bare literal unchanged", () => {
|
|
21
|
-
assert.equal(generateTemplateExpression(Literal(42)), "42");
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test("leaves a scoped identifier bare instead of prefixing with instance.", () => {
|
|
25
|
-
const scope = new Set(["post"]);
|
|
26
|
-
const node = MemberExpr(Identifier("post"), "title");
|
|
27
|
-
assert.equal(generateTemplateExpression(node, scope), "post.title");
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("still prefixes identifiers not in scope", () => {
|
|
31
|
-
const scope = new Set(["post"]);
|
|
32
|
-
const node = Identifier("count");
|
|
33
|
-
assert.equal(generateTemplateExpression(node, scope), "instance.count");
|
|
34
|
-
});
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { JSDOM } from "jsdom";
|
|
4
|
-
import { compileComponent } from "./compileComponent.js";
|
|
5
|
-
|
|
6
|
-
test("compiles and mounts a full counter component from raw .tyx source", () => {
|
|
7
|
-
const source = `
|
|
8
|
-
<script>
|
|
9
|
-
state {
|
|
10
|
-
count: 0
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
action {
|
|
14
|
-
increment() {
|
|
15
|
-
this.count = this.count + 1;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
</script>
|
|
19
|
-
|
|
20
|
-
<template>
|
|
21
|
-
<div>
|
|
22
|
-
<span>{{ count }}</span>
|
|
23
|
-
<button onclick="{{ increment }}">+1</button>
|
|
24
|
-
</div>
|
|
25
|
-
</template>
|
|
26
|
-
`;
|
|
27
|
-
|
|
28
|
-
const Component = compileComponent(source, "Counter");
|
|
29
|
-
const dom = new JSDOM();
|
|
30
|
-
const { node, instance } = Component.mount(dom.window.document);
|
|
31
|
-
|
|
32
|
-
dom.window.document.body.appendChild(node);
|
|
33
|
-
const span = node.querySelector("span");
|
|
34
|
-
const button = node.querySelector("button");
|
|
35
|
-
|
|
36
|
-
assert.equal(span.textContent, "0");
|
|
37
|
-
assert.equal(instance.count, 0);
|
|
38
|
-
|
|
39
|
-
button.dispatchEvent(new dom.window.Event("click"));
|
|
40
|
-
assert.equal(span.textContent, "1");
|
|
41
|
-
assert.equal(instance.count, 1);
|
|
42
|
-
|
|
43
|
-
button.dispatchEvent(new dom.window.Event("click"));
|
|
44
|
-
assert.equal(span.textContent, "2");
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test("compiles a template-only component with no script section", () => {
|
|
48
|
-
const source = `
|
|
49
|
-
<template>
|
|
50
|
-
<p>Static content, no reactivity needed</p>
|
|
51
|
-
</template>
|
|
52
|
-
`;
|
|
53
|
-
|
|
54
|
-
const Component = compileComponent(source);
|
|
55
|
-
const dom = new JSDOM();
|
|
56
|
-
const { node } = Component.mount(dom.window.document);
|
|
57
|
-
|
|
58
|
-
assert.equal(node.textContent, "Static content, no reactivity needed");
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
test("computed values work correctly when mounted", () => {
|
|
62
|
-
const source = `
|
|
63
|
-
<script>
|
|
64
|
-
state { count: 3 }
|
|
65
|
-
computed {
|
|
66
|
-
doubled() {
|
|
67
|
-
return this.count * 2;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
action {
|
|
71
|
-
increment() {
|
|
72
|
-
this.count = this.count + 1;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
</script>
|
|
76
|
-
|
|
77
|
-
<template>
|
|
78
|
-
<div>
|
|
79
|
-
<span>{{ doubled }}</span>
|
|
80
|
-
<button onclick="{{ increment }}">+1</button>
|
|
81
|
-
</div>
|
|
82
|
-
</template>
|
|
83
|
-
`;
|
|
84
|
-
|
|
85
|
-
const Component = compileComponent(source, "WithComputed");
|
|
86
|
-
const dom = new JSDOM();
|
|
87
|
-
const { node } = Component.mount(dom.window.document);
|
|
88
|
-
|
|
89
|
-
const span = node.querySelector("span");
|
|
90
|
-
const button = node.querySelector("button");
|
|
91
|
-
|
|
92
|
-
assert.equal(span.textContent, "6");
|
|
93
|
-
button.dispatchEvent(new dom.window.Event("click"));
|
|
94
|
-
assert.equal(span.textContent, "8");
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
test("mounts a component that nests another compiled component", () => {
|
|
98
|
-
const badgeSource = `
|
|
99
|
-
<template>
|
|
100
|
-
<span class="badge">Verified</span>
|
|
101
|
-
</template>
|
|
102
|
-
`;
|
|
103
|
-
const Badge = compileComponent(badgeSource, "Badge");
|
|
104
|
-
|
|
105
|
-
const profileSource = `
|
|
106
|
-
<template>
|
|
107
|
-
<div>
|
|
108
|
-
<h1>Ada Lovelace</h1>
|
|
109
|
-
<Badge />
|
|
110
|
-
</div>
|
|
111
|
-
</template>
|
|
112
|
-
`;
|
|
113
|
-
const Profile = compileComponent(profileSource, "Profile");
|
|
114
|
-
|
|
115
|
-
const dom = new JSDOM();
|
|
116
|
-
const { node } = Profile.mount(dom.window.document, { Badge });
|
|
117
|
-
|
|
118
|
-
const badgeSpan = node.querySelector(".badge");
|
|
119
|
-
assert.equal(badgeSpan.textContent, "Verified");
|
|
120
|
-
assert.equal(node.querySelector("h1").textContent, "Ada Lovelace");
|
|
121
|
-
});
|