@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,101 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { parsePageFile } from "./parsePageFile.js";
|
|
4
|
-
|
|
5
|
-
test("parses the page name from the page declaration", () => {
|
|
6
|
-
const result = parsePageFile(`page Home\n\ntemplate\n <div>Hi</div>`);
|
|
7
|
-
assert.equal(result.pageName, "Home");
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
test("extracts state/computed/action into the script portion", () => {
|
|
11
|
-
const source = `
|
|
12
|
-
page Counter
|
|
13
|
-
|
|
14
|
-
state
|
|
15
|
-
count: 0
|
|
16
|
-
|
|
17
|
-
computed
|
|
18
|
-
doubled() {
|
|
19
|
-
return this.count * 2
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
action
|
|
23
|
-
increment() {
|
|
24
|
-
this.count = this.count + 1
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
template
|
|
28
|
-
<div>{{ count }}</div>
|
|
29
|
-
`;
|
|
30
|
-
const result = parsePageFile(source);
|
|
31
|
-
assert.match(result.script, /state/);
|
|
32
|
-
assert.match(result.script, /computed/);
|
|
33
|
-
assert.match(result.script, /action/);
|
|
34
|
-
assert.match(result.script, /increment/);
|
|
35
|
-
assert.doesNotMatch(result.script, /<div>/);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test("extracts the template section", () => {
|
|
39
|
-
const source = `page Home\n\ntemplate\n <h1>{{ title }}</h1>`;
|
|
40
|
-
const result = parsePageFile(source);
|
|
41
|
-
assert.match(result.template, /<h1>\{\{ title \}\}<\/h1>/);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test("extracts an optional style section after the template", () => {
|
|
45
|
-
const source = `
|
|
46
|
-
page Home
|
|
47
|
-
|
|
48
|
-
template
|
|
49
|
-
<p>Hi</p>
|
|
50
|
-
|
|
51
|
-
style
|
|
52
|
-
p { color: red; }
|
|
53
|
-
`;
|
|
54
|
-
const result = parsePageFile(source);
|
|
55
|
-
assert.match(result.template, /<p>Hi<\/p>/);
|
|
56
|
-
assert.match(result.style, /color: red/);
|
|
57
|
-
assert.doesNotMatch(result.template, /color: red/);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test("style is optional and defaults to empty string", () => {
|
|
61
|
-
const result = parsePageFile(`page Home\n\ntemplate\n <p>Hi</p>`);
|
|
62
|
-
assert.equal(result.style, "");
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
test("throws when the page declaration is missing", () => {
|
|
66
|
-
assert.throws(() => parsePageFile(`template\n <p>Hi</p>`), /must start with "page/);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test("throws when the template section is missing", () => {
|
|
70
|
-
assert.throws(() => parsePageFile(`page Home\n\nstate\n count: 0`), /missing a required "template"/);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
test("parses a realistic full counter page with no wrapper tags", () => {
|
|
74
|
-
const source = `
|
|
75
|
-
page Counter
|
|
76
|
-
|
|
77
|
-
state
|
|
78
|
-
count: 0
|
|
79
|
-
|
|
80
|
-
computed
|
|
81
|
-
doubled() {
|
|
82
|
-
return this.count * 2
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
action
|
|
86
|
-
increment() {
|
|
87
|
-
this.count = this.count + 1
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
template
|
|
91
|
-
<div>
|
|
92
|
-
<h1>Counter</h1>
|
|
93
|
-
<p>Count: {{ count }}, doubled: {{ doubled }}</p>
|
|
94
|
-
<button onclick="{{ increment }}">+1</button>
|
|
95
|
-
</div>
|
|
96
|
-
`;
|
|
97
|
-
const result = parsePageFile(source);
|
|
98
|
-
assert.equal(result.pageName, "Counter");
|
|
99
|
-
assert.match(result.script, /count: 0/);
|
|
100
|
-
assert.match(result.template, /<button onclick="\{\{ increment \}\}">\+1<\/button>/);
|
|
101
|
-
});
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { parseTemplate } from "./parseTemplate.js";
|
|
4
|
-
|
|
5
|
-
test("parses an {{#if}} block", () => {
|
|
6
|
-
const nodes = parseTemplate(`{{#if isVisible}}<p>Shown</p>{{/if}}`);
|
|
7
|
-
assert.equal(nodes[0].type, "If");
|
|
8
|
-
assert.equal(nodes[0].children[0].tag, "p");
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
test("parses an {{#each}} block with item in items syntax", () => {
|
|
12
|
-
const nodes = parseTemplate(`{{#each post in posts}}<li>{{ post.title }}</li>{{/each}}`);
|
|
13
|
-
assert.equal(nodes[0].type, "Each");
|
|
14
|
-
assert.equal(nodes[0].itemName, "post");
|
|
15
|
-
assert.equal(nodes[0].iterable.name, "posts");
|
|
16
|
-
assert.equal(nodes[0].children[0].tag, "li");
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test("parses nested if inside each", () => {
|
|
20
|
-
const nodes = parseTemplate(
|
|
21
|
-
`{{#each post in posts}}{{#if post.published}}<span>{{ post.title }}</span>{{/if}}{{/each}}`
|
|
22
|
-
);
|
|
23
|
-
assert.equal(nodes[0].type, "Each");
|
|
24
|
-
assert.equal(nodes[0].children[0].type, "If");
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test("throws on an unclosed {{#if}} block", () => {
|
|
28
|
-
assert.throws(() => parseTemplate(`{{#if x}}<p>Hi</p>`), /Unclosed block/);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test("throws on a malformed {{#each}} header", () => {
|
|
32
|
-
assert.throws(() => parseTemplate(`{{#each posts}}<li></li>{{/each}}`), /expected "item in items"/);
|
|
33
|
-
});
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { parseTemplate } from "./parseTemplate.js";
|
|
4
|
-
|
|
5
|
-
test("preserves exact whitespace and newlines inside <pre>", () => {
|
|
6
|
-
const source = `<pre>line one
|
|
7
|
-
line two (indented)
|
|
8
|
-
line three</pre>`;
|
|
9
|
-
const nodes = parseTemplate(source);
|
|
10
|
-
const preNode = nodes[0];
|
|
11
|
-
assert.equal(preNode.tag, "pre");
|
|
12
|
-
assert.equal(preNode.children[0].value, "line one\n line two (indented)\nline three");
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test("does not interpret {{ }} syntax inside <pre> as real interpolation/control-flow", () => {
|
|
16
|
-
const source = `<pre>{{#each post in posts}}
|
|
17
|
-
{{ post.title }}
|
|
18
|
-
{{/each}}</pre>`;
|
|
19
|
-
const nodes = parseTemplate(source);
|
|
20
|
-
const preNode = nodes[0];
|
|
21
|
-
assert.equal(preNode.children.length, 1);
|
|
22
|
-
assert.equal(preNode.children[0].type, "Text");
|
|
23
|
-
assert.match(preNode.children[0].value, /\{\{#each post in posts\}\}/);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test("throws a clear error on an unclosed <pre> tag", () => {
|
|
27
|
-
assert.throws(() => parseTemplate(`<pre>never closed`), /Unclosed <pre> tag/);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("<pre> sits correctly alongside normal interpreted siblings", () => {
|
|
31
|
-
const source = `<div><h1>{{ title }}</h1><pre>{{ raw }}</pre></div>`;
|
|
32
|
-
const nodes = parseTemplate(source);
|
|
33
|
-
const div = nodes[0];
|
|
34
|
-
assert.equal(div.children[0].tag, "h1");
|
|
35
|
-
assert.equal(div.children[0].children[0].type, "Interpolation");
|
|
36
|
-
assert.equal(div.children[1].tag, "pre");
|
|
37
|
-
assert.equal(div.children[1].children[0].type, "Text");
|
|
38
|
-
assert.equal(div.children[1].children[0].value, "{{ raw }}");
|
|
39
|
-
});
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { parseTemplate } from "./parseTemplate.js";
|
|
4
|
-
|
|
5
|
-
// Element and Attribute nodes carry a `line` for error reporting.
|
|
6
|
-
// Tests that compare whole nodes drop it first, so they assert on
|
|
7
|
-
// structure and don't break whenever position tracking changes.
|
|
8
|
-
function withoutLines(node) {
|
|
9
|
-
return JSON.parse(
|
|
10
|
-
JSON.stringify(node, (key, value) => (key === "line" ? undefined : value)),
|
|
11
|
-
);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
test("parses a single element with text content", () => {
|
|
15
|
-
const nodes = parseTemplate(`<h1>Hello</h1>`);
|
|
16
|
-
assert.deepEqual(withoutLines(nodes), [
|
|
17
|
-
{ type: "Element", tag: "h1", attributes: [], children: [{ type: "Text", value: "Hello" }] },
|
|
18
|
-
]);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test("records the source line on elements", () => {
|
|
22
|
-
const nodes = parseTemplate(`<h1>Hello</h1>`);
|
|
23
|
-
assert.equal(nodes[0].line, 1);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test("parses an interpolation node", () => {
|
|
27
|
-
const nodes = parseTemplate(`{{ count }}`);
|
|
28
|
-
assert.equal(nodes.length, 1);
|
|
29
|
-
assert.equal(nodes[0].type, "Interpolation");
|
|
30
|
-
assert.deepEqual(nodes[0].expression, { type: "Identifier", name: "count" });
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("parses text and interpolation mixed in one element", () => {
|
|
34
|
-
const nodes = parseTemplate(`<p>Count: {{ count }}</p>`);
|
|
35
|
-
const p = nodes[0];
|
|
36
|
-
assert.equal(p.children.length, 2);
|
|
37
|
-
assert.deepEqual(p.children[0], { type: "Text", value: "Count: " });
|
|
38
|
-
assert.equal(p.children[1].type, "Interpolation");
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("parses nested elements", () => {
|
|
42
|
-
const nodes = parseTemplate(`<div><h1>Title</h1><p>Body</p></div>`);
|
|
43
|
-
const div = nodes[0];
|
|
44
|
-
assert.equal(div.children.length, 2);
|
|
45
|
-
assert.equal(div.children[0].tag, "h1");
|
|
46
|
-
assert.equal(div.children[1].tag, "p");
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test("parses a static attribute", () => {
|
|
50
|
-
const nodes = parseTemplate(`<div class="wrapper"></div>`);
|
|
51
|
-
assert.deepEqual(withoutLines(nodes[0].attributes), [
|
|
52
|
-
{ type: "Attribute", name: "class", value: "wrapper", dynamic: false },
|
|
53
|
-
]);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test("parses a dynamic event-binding attribute using the expression parser", () => {
|
|
57
|
-
const nodes = parseTemplate(`<button onclick="{{ increment }}">+1</button>`);
|
|
58
|
-
const attr = nodes[0].attributes[0];
|
|
59
|
-
assert.equal(attr.name, "onclick");
|
|
60
|
-
assert.equal(attr.dynamic, true);
|
|
61
|
-
assert.deepEqual(attr.value, { type: "Identifier", name: "increment" });
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test("parses a self-closing element", () => {
|
|
65
|
-
const nodes = parseTemplate(`<input />`);
|
|
66
|
-
assert.deepEqual(withoutLines(nodes), [
|
|
67
|
-
{ type: "Element", tag: "input", attributes: [], children: [] },
|
|
68
|
-
]);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test("parses the realistic counter template from the design doc", () => {
|
|
72
|
-
const nodes = parseTemplate(`
|
|
73
|
-
<div>
|
|
74
|
-
<h1>{{ title }}</h1>
|
|
75
|
-
<p>Count: {{ count }}, doubled: {{ doubled }}</p>
|
|
76
|
-
<button onclick="{{ increment }}">+1</button>
|
|
77
|
-
</div>
|
|
78
|
-
`);
|
|
79
|
-
const div = nodes[0];
|
|
80
|
-
assert.equal(div.tag, "div");
|
|
81
|
-
assert.equal(div.children.length, 3);
|
|
82
|
-
assert.equal(div.children[0].tag, "h1");
|
|
83
|
-
assert.equal(div.children[1].tag, "p");
|
|
84
|
-
assert.equal(div.children[2].tag, "button");
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test("throws a clear error on a mismatched closing tag", () => {
|
|
88
|
-
assert.throws(() => parseTemplate(`<div><span></div></span>`), /Mismatched closing tag/);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test("throws a clear error on an unclosed tag", () => {
|
|
92
|
-
assert.throws(() => parseTemplate(`<div><span>text</div>`), /Unclosed tag|Mismatched closing tag/);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test("throws a clear error on an unterminated interpolation", () => {
|
|
96
|
-
assert.throws(() => parseTemplate(`<div>{{ count </div>`), /Unterminated interpolation/);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test("preserves meaningful whitespace between text and interpolation", () => {
|
|
100
|
-
const nodes = parseTemplate(`<h1>Hello {{ name }}</h1>`);
|
|
101
|
-
assert.equal(nodes[0].type, "Element");
|
|
102
|
-
const [textNode, interpNode] = nodes[0].children;
|
|
103
|
-
assert.equal(textNode.type, "Text");
|
|
104
|
-
assert.equal(textNode.value, "Hello ");
|
|
105
|
-
assert.equal(interpNode.type, "Interpolation");
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
test("collapses indentation-only whitespace between tags to nothing", () => {
|
|
109
|
-
const nodes = parseTemplate(`<div>\n <h1>Hi</h1>\n</div>`);
|
|
110
|
-
assert.equal(nodes[0].children.length, 1, "pure-whitespace indentation should not become a Text node");
|
|
111
|
-
assert.equal(nodes[0].children[0].tag, "h1");
|
|
112
|
-
});
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { test } from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { JSDOM } from 'jsdom'
|
|
4
|
-
import { renderPageDocument } from './renderPageDocument.js'
|
|
5
|
-
|
|
6
|
-
function loadDocument(html) {
|
|
7
|
-
return new Promise((resolve) => {
|
|
8
|
-
const dom = new JSDOM(html, {
|
|
9
|
-
runScripts: 'dangerously',
|
|
10
|
-
resources: 'usable',
|
|
11
|
-
url: 'http://localhost',
|
|
12
|
-
})
|
|
13
|
-
dom.window.document.addEventListener('DOMContentLoaded', () => {
|
|
14
|
-
setTimeout(() => resolve(dom), 0)
|
|
15
|
-
})
|
|
16
|
-
})
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
test('renders a full HTML document from native Tylix syntax, mounted and interactive', async () => {
|
|
20
|
-
const source = `
|
|
21
|
-
page CounterPage
|
|
22
|
-
|
|
23
|
-
state
|
|
24
|
-
count: 0
|
|
25
|
-
|
|
26
|
-
action
|
|
27
|
-
increment() {
|
|
28
|
-
this.count = this.count + 1
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
template
|
|
32
|
-
<div>
|
|
33
|
-
<span>{{ count }}</span>
|
|
34
|
-
<button onclick="{{ increment }}">+1</button>
|
|
35
|
-
</div>
|
|
36
|
-
`
|
|
37
|
-
|
|
38
|
-
const html = await renderPageDocument(source)
|
|
39
|
-
assert.match(html, /<title>CounterPage<\/title>/)
|
|
40
|
-
|
|
41
|
-
const dom = await loadDocument(html)
|
|
42
|
-
const { document } = dom.window
|
|
43
|
-
|
|
44
|
-
const span = document.querySelector('#app span')
|
|
45
|
-
const button = document.querySelector('#app button')
|
|
46
|
-
|
|
47
|
-
assert.equal(span.textContent, '0')
|
|
48
|
-
button.dispatchEvent(new dom.window.Event('click'))
|
|
49
|
-
assert.equal(span.textContent, '1')
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
test('passes route params through as props on the page instance', async () => {
|
|
53
|
-
const source = `
|
|
54
|
-
page PostDetail
|
|
55
|
-
|
|
56
|
-
props
|
|
57
|
-
id: string
|
|
58
|
-
|
|
59
|
-
template
|
|
60
|
-
<div>
|
|
61
|
-
<span>{{ id }}</span>
|
|
62
|
-
</div>
|
|
63
|
-
`
|
|
64
|
-
|
|
65
|
-
const html = await renderPageDocument(source, {}, { props: { id: '42' } })
|
|
66
|
-
const dom = await loadDocument(html)
|
|
67
|
-
const span = dom.window.document.querySelector('#app span')
|
|
68
|
-
|
|
69
|
-
assert.equal(span.textContent, '42')
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
test('inlines the style section into the document head', async () => {
|
|
73
|
-
const source = `
|
|
74
|
-
page Home
|
|
75
|
-
|
|
76
|
-
template
|
|
77
|
-
<p>Hello</p>
|
|
78
|
-
|
|
79
|
-
style
|
|
80
|
-
p { color: red; }
|
|
81
|
-
`
|
|
82
|
-
const html = await renderPageDocument(source)
|
|
83
|
-
assert.match(html, /<style>\s*p \{ color: red; \}\s*<\/style>/)
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
test('omits the style tag entirely when no style section is present', async () => {
|
|
87
|
-
const source = `page Home\n\ntemplate\n <p>Hello</p>`
|
|
88
|
-
const html = await renderPageDocument(source)
|
|
89
|
-
assert.doesNotMatch(html, /<style>/)
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
test('supports a page with no script section at all', async () => {
|
|
93
|
-
const source = `page Static\n\ntemplate\n <p>Just static markup</p>`
|
|
94
|
-
const html = await renderPageDocument(source)
|
|
95
|
-
const dom = await loadDocument(html)
|
|
96
|
-
assert.equal(
|
|
97
|
-
dom.window.document.querySelector('#app p').textContent,
|
|
98
|
-
'Just static markup',
|
|
99
|
-
)
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
test('renders a page that nests a child component via <TagName />', async () => {
|
|
103
|
-
const badgeSource = `
|
|
104
|
-
<template>
|
|
105
|
-
<span class="badge">Verified</span>
|
|
106
|
-
</template>
|
|
107
|
-
`
|
|
108
|
-
|
|
109
|
-
const pageSource = `
|
|
110
|
-
page Profile
|
|
111
|
-
|
|
112
|
-
template
|
|
113
|
-
<div>
|
|
114
|
-
<h1>Ada Lovelace</h1>
|
|
115
|
-
<Badge />
|
|
116
|
-
</div>
|
|
117
|
-
`
|
|
118
|
-
|
|
119
|
-
const html = await renderPageDocument(pageSource, { Badge: badgeSource })
|
|
120
|
-
const dom = await loadDocument(html)
|
|
121
|
-
const { document } = dom.window
|
|
122
|
-
|
|
123
|
-
assert.equal(document.querySelector('#app h1').textContent, 'Ada Lovelace')
|
|
124
|
-
assert.equal(document.querySelector('#app .badge').textContent, 'Verified')
|
|
125
|
-
})
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { reactive, effect } from "./reactive.js";
|
|
4
|
-
|
|
5
|
-
test("reading a reactive property inside an effect tracks it", () => {
|
|
6
|
-
const state = reactive({ count: 0 });
|
|
7
|
-
let seen = null;
|
|
8
|
-
|
|
9
|
-
effect(() => {
|
|
10
|
-
seen = state.count;
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
assert.equal(seen, 0);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test("writing a tracked property re-runs the effect", () => {
|
|
17
|
-
const state = reactive({ count: 0 });
|
|
18
|
-
let runs = 0;
|
|
19
|
-
|
|
20
|
-
effect(() => {
|
|
21
|
-
state.count; // read to establish dependency
|
|
22
|
-
runs++;
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
assert.equal(runs, 1);
|
|
26
|
-
state.count = 1;
|
|
27
|
-
assert.equal(runs, 2);
|
|
28
|
-
state.count = 2;
|
|
29
|
-
assert.equal(runs, 3);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test("writing an untracked property does not re-run unrelated effects", () => {
|
|
33
|
-
const state = reactive({ count: 0, other: "x" });
|
|
34
|
-
let runs = 0;
|
|
35
|
-
|
|
36
|
-
effect(() => {
|
|
37
|
-
state.count;
|
|
38
|
-
runs++;
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
state.other = "y";
|
|
42
|
-
assert.equal(runs, 1, "effect should not re-run for a property it never read");
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test("setting a property to the same value does not re-trigger", () => {
|
|
46
|
-
const state = reactive({ count: 5 });
|
|
47
|
-
let runs = 0;
|
|
48
|
-
|
|
49
|
-
effect(() => {
|
|
50
|
-
state.count;
|
|
51
|
-
runs++;
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
state.count = 5;
|
|
55
|
-
assert.equal(runs, 1);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test("multiple independent effects each track their own dependencies", () => {
|
|
59
|
-
const state = reactive({ a: 1, b: 2 });
|
|
60
|
-
let aRuns = 0;
|
|
61
|
-
let bRuns = 0;
|
|
62
|
-
|
|
63
|
-
effect(() => {
|
|
64
|
-
state.a;
|
|
65
|
-
aRuns++;
|
|
66
|
-
});
|
|
67
|
-
effect(() => {
|
|
68
|
-
state.b;
|
|
69
|
-
bRuns++;
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
state.a = 10;
|
|
73
|
-
assert.equal(aRuns, 2);
|
|
74
|
-
assert.equal(bRuns, 1);
|
|
75
|
-
|
|
76
|
-
state.b = 20;
|
|
77
|
-
assert.equal(aRuns, 2);
|
|
78
|
-
assert.equal(bRuns, 2);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test("an effect reading multiple properties re-runs when any of them change", () => {
|
|
82
|
-
const state = reactive({ first: "Ada", last: "Lovelace" });
|
|
83
|
-
const fullNames = [];
|
|
84
|
-
|
|
85
|
-
effect(() => {
|
|
86
|
-
fullNames.push(`${state.first} ${state.last}`);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
state.first = "Grace";
|
|
90
|
-
state.last = "Hopper";
|
|
91
|
-
|
|
92
|
-
assert.deepEqual(fullNames, ["Ada Lovelace", "Grace Lovelace", "Grace Hopper"]);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test("computed-style derived effect updates when its source changes", () => {
|
|
96
|
-
const state = reactive({ count: 3 });
|
|
97
|
-
let doubled = null;
|
|
98
|
-
|
|
99
|
-
effect(() => {
|
|
100
|
-
doubled = state.count * 2;
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
assert.equal(doubled, 6);
|
|
104
|
-
state.count = 10;
|
|
105
|
-
assert.equal(doubled, 20);
|
|
106
|
-
});
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { test } from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises'
|
|
4
|
-
import { tmpdir } from 'node:os'
|
|
5
|
-
import path from 'node:path'
|
|
6
|
-
import { parsePageFile } from '../src/parser/parsePageFile.js'
|
|
7
|
-
import { Lexer } from '../src/lexer/Lexer.js'
|
|
8
|
-
import { Parser } from '../src/parser/Parser.js'
|
|
9
|
-
import { generatePage } from '../src/codegen/generatePage.js'
|
|
10
|
-
import { renderPageDocument } from '../src/renderPageDocument.js'
|
|
11
|
-
|
|
12
|
-
const SOURCE = `page Dashboard
|
|
13
|
-
title "Dashboard"
|
|
14
|
-
layout DashboardLayout
|
|
15
|
-
needs
|
|
16
|
-
Auth
|
|
17
|
-
uses
|
|
18
|
-
Theme
|
|
19
|
-
Posts
|
|
20
|
-
prefetch
|
|
21
|
-
Posts.latest
|
|
22
|
-
background
|
|
23
|
-
Analytics
|
|
24
|
-
permissions
|
|
25
|
-
posts.view
|
|
26
|
-
meta
|
|
27
|
-
transition "fade"
|
|
28
|
-
keepAlive true
|
|
29
|
-
cache 10m
|
|
30
|
-
state
|
|
31
|
-
search: ""
|
|
32
|
-
computed
|
|
33
|
-
filteredPosts() {
|
|
34
|
-
return Posts.where(post => post.title.contains(this.search))
|
|
35
|
-
}
|
|
36
|
-
actions
|
|
37
|
-
async publishPost(post) {
|
|
38
|
-
await Posts.publish(post)
|
|
39
|
-
}
|
|
40
|
-
template
|
|
41
|
-
<div>{{ search }}</div>
|
|
42
|
-
`
|
|
43
|
-
|
|
44
|
-
function parseScript() {
|
|
45
|
-
const { script } = parsePageFile(SOURCE)
|
|
46
|
-
const tokens = new Lexer(script).tokenize()
|
|
47
|
-
return new Parser(tokens).parse()
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
test('parses all six page-level sections without throwing', () => {
|
|
51
|
-
const page = parseScript()
|
|
52
|
-
|
|
53
|
-
assert.equal(page.title, 'Dashboard')
|
|
54
|
-
assert.equal(page.layout, 'DashboardLayout')
|
|
55
|
-
assert.deepEqual(
|
|
56
|
-
page.needs.map((n) => n.name),
|
|
57
|
-
['Auth'],
|
|
58
|
-
)
|
|
59
|
-
assert.deepEqual(
|
|
60
|
-
page.uses.map((u) => u.name),
|
|
61
|
-
['Theme', 'Posts'],
|
|
62
|
-
)
|
|
63
|
-
assert.equal(page.prefetch[0].capability, 'Posts')
|
|
64
|
-
assert.equal(page.prefetch[0].member, 'latest')
|
|
65
|
-
assert.deepEqual(
|
|
66
|
-
page.background.map((b) => b.name),
|
|
67
|
-
['Analytics'],
|
|
68
|
-
)
|
|
69
|
-
assert.deepEqual(
|
|
70
|
-
page.permissions.map((p) => p.name),
|
|
71
|
-
['posts.view'],
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
const meta = Object.fromEntries(page.meta.map((m) => [m.key, m.value]))
|
|
75
|
-
assert.equal(meta.transition, 'fade')
|
|
76
|
-
assert.equal(meta.keepAlive, true)
|
|
77
|
-
assert.equal(meta.cache, '10m')
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
test('generated page class is syntactically valid JS', () => {
|
|
81
|
-
const page = parseScript()
|
|
82
|
-
const classSource = generatePage(page, 'Dashboard')
|
|
83
|
-
|
|
84
|
-
assert.doesNotThrow(() => new Function(classSource), classSource)
|
|
85
|
-
assert.match(classSource, /async __ready\(\)/)
|
|
86
|
-
assert.match(classSource, /resolveCapability\("Auth"\)\.ready\(\)/)
|
|
87
|
-
assert.match(classSource, /resolveCapability\("Posts"\)\.latest\(\)/)
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
test('renderPageDocument compiles the full Dashboard against stub capabilities', async () => {
|
|
91
|
-
const dir = await mkdtemp(path.join(tmpdir(), 'tylix-test-'))
|
|
92
|
-
const capabilitiesDir = path.join(dir, 'capabilities')
|
|
93
|
-
await mkdir(capabilitiesDir, { recursive: true })
|
|
94
|
-
|
|
95
|
-
await writeFile(
|
|
96
|
-
path.join(capabilitiesDir, 'Auth.tyx'),
|
|
97
|
-
`capability Auth\nstate\n user: null\naction\n noop() {}\n`,
|
|
98
|
-
)
|
|
99
|
-
await writeFile(
|
|
100
|
-
path.join(capabilitiesDir, 'Theme.tyx'),
|
|
101
|
-
`capability Theme\nstate\n mode: "dark"\naction\n noop() {}\n`,
|
|
102
|
-
)
|
|
103
|
-
await writeFile(
|
|
104
|
-
path.join(capabilitiesDir, 'Posts.tyx'),
|
|
105
|
-
`capability Posts\nstate\n data: []\naction\n async latest() {}\n where(fn) { return this.data.filter(fn) }\n`,
|
|
106
|
-
)
|
|
107
|
-
await writeFile(
|
|
108
|
-
path.join(capabilitiesDir, 'Analytics.tyx'),
|
|
109
|
-
`capability Analytics\nstate\n visitorsToday: 0\naction\n noop() {}\n`,
|
|
110
|
-
)
|
|
111
|
-
|
|
112
|
-
const html = await renderPageDocument(SOURCE, {}, { capabilitiesDir })
|
|
113
|
-
assert.match(html, /<title>Dashboard<\/title>/)
|
|
114
|
-
|
|
115
|
-
await rm(dir, { recursive: true, force: true })
|
|
116
|
-
})
|