@tylix/compiler 0.6.0 → 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 +145 -23
- package/src/ast/substituteCapabilityRefs.js +52 -5
- package/src/codegen/emitStatementLines.js +60 -10
- package/src/codegen/generateExpression.js +294 -35
- package/src/codegen/generateStatement.js +125 -20
- package/src/codegen/generateTemplate.js +107 -96
- package/src/codegen/generateTemplateExpression.js +63 -10
- package/src/codegen/jsGlobals.js +62 -2
- package/src/compileComponent.js +37 -6
- package/src/lexer/Lexer.js +128 -28
- package/src/parser/Parser.js +1840 -500
- package/src/parser/parseExpressionString.js +5 -0
- package/src/parser/parseTemplate.js +153 -41
- package/src/renderPageDocument.js +71 -8
- package/src/runtime/capability.js +8 -1
- package/src/runtime/reactive.js +289 -61
- package/src/runtime/useApi.js +74 -13
- package/src/runtime/useChannel.js +27 -6
- package/src/typecheck/buildVirtualTs.js +11 -1
- package/src/typecheck/tylix-globals.d.ts +23 -4
- package/src/typecheck/typedTemplateExpr.js +90 -27
- 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/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,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
|
-
});
|
package/src/lexer/Lexer.test.js
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { Lexer } from "./Lexer.js";
|
|
4
|
-
import { TokenType } from "./tokenTypes.js";
|
|
5
|
-
|
|
6
|
-
function tokenTypes(source) {
|
|
7
|
-
return new Lexer(source).tokenize().map((t) => t.type);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
test("tokenizes keywords", () => {
|
|
11
|
-
const types = tokenTypes("page state action computed watch template props");
|
|
12
|
-
assert.deepEqual(types, [
|
|
13
|
-
TokenType.PAGE,
|
|
14
|
-
TokenType.STATE,
|
|
15
|
-
TokenType.ACTION,
|
|
16
|
-
TokenType.COMPUTED,
|
|
17
|
-
TokenType.WATCH,
|
|
18
|
-
TokenType.TEMPLATE,
|
|
19
|
-
TokenType.PROPS,
|
|
20
|
-
TokenType.EOF,
|
|
21
|
-
]);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test("tokenizes an identifier that is not a keyword", () => {
|
|
25
|
-
const tokens = new Lexer("count").tokenize();
|
|
26
|
-
assert.equal(tokens[0].type, TokenType.IDENTIFIER);
|
|
27
|
-
assert.equal(tokens[0].value, "count");
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("tokenizes a number literal", () => {
|
|
31
|
-
const tokens = new Lexer("42").tokenize();
|
|
32
|
-
assert.equal(tokens[0].type, TokenType.NUMBER);
|
|
33
|
-
assert.equal(tokens[0].value, 42);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test("tokenizes a decimal number literal", () => {
|
|
37
|
-
const tokens = new Lexer("3.14").tokenize();
|
|
38
|
-
assert.equal(tokens[0].value, 3.14);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("tokenizes single and double quoted strings", () => {
|
|
42
|
-
const tokens = new Lexer(`"hello" 'world'`).tokenize();
|
|
43
|
-
assert.equal(tokens[0].type, TokenType.STRING);
|
|
44
|
-
assert.equal(tokens[0].value, "hello");
|
|
45
|
-
assert.equal(tokens[1].type, TokenType.STRING);
|
|
46
|
-
assert.equal(tokens[1].value, "world");
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test("tokenizes braces, parens, and punctuation", () => {
|
|
50
|
-
const types = tokenTypes("{ } ( ) : , ; . =");
|
|
51
|
-
assert.deepEqual(types, [
|
|
52
|
-
TokenType.LBRACE,
|
|
53
|
-
TokenType.RBRACE,
|
|
54
|
-
TokenType.LPAREN,
|
|
55
|
-
TokenType.RPAREN,
|
|
56
|
-
TokenType.COLON,
|
|
57
|
-
TokenType.COMMA,
|
|
58
|
-
TokenType.SEMICOLON,
|
|
59
|
-
TokenType.DOT,
|
|
60
|
-
TokenType.EQUALS,
|
|
61
|
-
TokenType.EOF,
|
|
62
|
-
]);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
test("tokenizes the arrow operator distinctly from equals", () => {
|
|
66
|
-
const types = tokenTypes("=>");
|
|
67
|
-
assert.deepEqual(types, [TokenType.ARROW, TokenType.EOF]);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
test("tokenizes interpolation markers distinctly from braces", () => {
|
|
71
|
-
const types = tokenTypes("{{ count }}");
|
|
72
|
-
assert.deepEqual(types, [
|
|
73
|
-
TokenType.INTERP_START,
|
|
74
|
-
TokenType.IDENTIFIER,
|
|
75
|
-
TokenType.INTERP_END,
|
|
76
|
-
TokenType.EOF,
|
|
77
|
-
]);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test("skips single-line comments", () => {
|
|
81
|
-
const types = tokenTypes("count // this is a comment\naction");
|
|
82
|
-
assert.deepEqual(types, [TokenType.IDENTIFIER, TokenType.ACTION, TokenType.EOF]);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
test("skips multi-line comments", () => {
|
|
86
|
-
const types = tokenTypes("count /* skip\nthis */ action");
|
|
87
|
-
assert.deepEqual(types, [TokenType.IDENTIFIER, TokenType.ACTION, TokenType.EOF]);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
test("tokenizes a realistic state block", () => {
|
|
91
|
-
const source = `state { count: 0 }`;
|
|
92
|
-
const types = tokenTypes(source);
|
|
93
|
-
assert.deepEqual(types, [
|
|
94
|
-
TokenType.STATE,
|
|
95
|
-
TokenType.LBRACE,
|
|
96
|
-
TokenType.IDENTIFIER,
|
|
97
|
-
TokenType.COLON,
|
|
98
|
-
TokenType.NUMBER,
|
|
99
|
-
TokenType.RBRACE,
|
|
100
|
-
TokenType.EOF,
|
|
101
|
-
]);
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
test("tokenizes a realistic action block with a method call body", () => {
|
|
105
|
-
const source = `action { increment() { count = count + 1; } }`;
|
|
106
|
-
const types = tokenTypes(source);
|
|
107
|
-
assert.deepEqual(types, [
|
|
108
|
-
TokenType.ACTION,
|
|
109
|
-
TokenType.LBRACE,
|
|
110
|
-
TokenType.IDENTIFIER,
|
|
111
|
-
TokenType.LPAREN,
|
|
112
|
-
TokenType.RPAREN,
|
|
113
|
-
TokenType.LBRACE,
|
|
114
|
-
TokenType.IDENTIFIER,
|
|
115
|
-
TokenType.EQUALS,
|
|
116
|
-
TokenType.IDENTIFIER,
|
|
117
|
-
TokenType.PLUS,
|
|
118
|
-
TokenType.NUMBER,
|
|
119
|
-
TokenType.SEMICOLON,
|
|
120
|
-
TokenType.RBRACE,
|
|
121
|
-
TokenType.RBRACE,
|
|
122
|
-
TokenType.EOF,
|
|
123
|
-
]);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test("tokenizes arithmetic and comparison operators", () => {
|
|
127
|
-
const types = tokenTypes("+ - * / > <");
|
|
128
|
-
assert.deepEqual(types, [
|
|
129
|
-
TokenType.PLUS,
|
|
130
|
-
TokenType.MINUS,
|
|
131
|
-
TokenType.STAR,
|
|
132
|
-
TokenType.SLASH,
|
|
133
|
-
TokenType.GT,
|
|
134
|
-
TokenType.LT,
|
|
135
|
-
TokenType.EOF,
|
|
136
|
-
]);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
test("throws a clear error on an unexpected character", () => {
|
|
140
|
-
assert.throws(() => new Lexer("state # invalid").tokenize(), /Unexpected character "#"/);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
test("throws a clear error on an unterminated string", () => {
|
|
144
|
-
assert.throws(() => new Lexer(`"never closed`).tokenize(), /Unterminated string/);
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
test("tracks line numbers across newlines", () => {
|
|
148
|
-
const tokens = new Lexer("count\naction").tokenize();
|
|
149
|
-
assert.equal(tokens[0].line, 1);
|
|
150
|
-
assert.equal(tokens[1].line, 2);
|
|
151
|
-
});
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { Lexer } from "../lexer/Lexer.js";
|
|
4
|
-
import { Parser } from "./Parser.js";
|
|
5
|
-
|
|
6
|
-
function parse(source) {
|
|
7
|
-
return new Parser(new Lexer(source).tokenize()).parse();
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
test("parses an empty array literal as a state initial value", () => {
|
|
11
|
-
const page = parse(`state\nproducts: []`);
|
|
12
|
-
assert.equal(page.state[0].value.type, "ArrayExpression");
|
|
13
|
-
assert.deepEqual(page.state[0].value.elements, []);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test("parses an array literal with elements", () => {
|
|
17
|
-
const page = parse(`state\nids: [1, 2, 3]`);
|
|
18
|
-
const elements = page.state[0].value.elements;
|
|
19
|
-
assert.equal(elements.length, 3);
|
|
20
|
-
assert.deepEqual(elements.map((e) => e.value), [1, 2, 3]);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test("parses an array literal assigned inside an action body", () => {
|
|
24
|
-
const page = parse(`action\nclear() {\n this.items = []\n}`);
|
|
25
|
-
const assign = page.actions[0].body[0].expression;
|
|
26
|
-
assert.equal(assign.type, "AssignmentExpression");
|
|
27
|
-
assert.equal(assign.value.type, "ArrayExpression");
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("parses a bare state block with inconsistent trailing commas between entries", () => {
|
|
31
|
-
const source = `state
|
|
32
|
-
products: []
|
|
33
|
-
title: "",
|
|
34
|
-
price: ""`;
|
|
35
|
-
const page = parse(source);
|
|
36
|
-
assert.deepEqual(page.state.map((s) => s.name), ["products", "title", "price"]);
|
|
37
|
-
});
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { Lexer } from "../lexer/Lexer.js";
|
|
4
|
-
import { Parser } from "./Parser.js";
|
|
5
|
-
|
|
6
|
-
test("parses an async method with await and const declarations", () => {
|
|
7
|
-
const source = `
|
|
8
|
-
action
|
|
9
|
-
async load() {
|
|
10
|
-
const response = await fetch("/api/products")
|
|
11
|
-
const data = await response.json()
|
|
12
|
-
this.products = data
|
|
13
|
-
}`;
|
|
14
|
-
const page = new Parser(new Lexer(source).tokenize()).parse();
|
|
15
|
-
const method = page.actions[0];
|
|
16
|
-
assert.equal(method.isAsync, true);
|
|
17
|
-
assert.equal(method.body[0].type, "VariableDeclaration");
|
|
18
|
-
assert.equal(method.body[0].kind, "const");
|
|
19
|
-
assert.equal(method.body[0].init.type, "AwaitExpression");
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("parses an object literal expression", () => {
|
|
23
|
-
const source = `
|
|
24
|
-
action
|
|
25
|
-
async save() {
|
|
26
|
-
await fetch("/api/products", { method: "POST", body: this.name })
|
|
27
|
-
}`;
|
|
28
|
-
const page = new Parser(new Lexer(source).tokenize()).parse();
|
|
29
|
-
const call = page.actions[0].body[0].expression.argument;
|
|
30
|
-
assert.equal(call.type, "CallExpression");
|
|
31
|
-
assert.equal(call.args[1].type, "ObjectExpression");
|
|
32
|
-
assert.equal(call.args[1].properties[0].key, "method");
|
|
33
|
-
});
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { Lexer } from "../lexer/Lexer.js";
|
|
4
|
-
import { Parser } from "./Parser.js";
|
|
5
|
-
|
|
6
|
-
test("parses an if statement with no else", () => {
|
|
7
|
-
const source = `action\ncheck() {\n if (this.ok) {\n this.result = 1\n }\n}`;
|
|
8
|
-
const page = new Parser(new Lexer(source).tokenize()).parse();
|
|
9
|
-
const stmt = page.actions[0].body[0];
|
|
10
|
-
assert.equal(stmt.type, "IfStatement");
|
|
11
|
-
assert.equal(stmt.alternate, null);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
test("parses an if/else statement", () => {
|
|
15
|
-
const source = `action\ncheck() {\n if (this.ok) {\n this.result = 1\n } else {\n this.result = 0\n }\n}`;
|
|
16
|
-
const page = new Parser(new Lexer(source).tokenize()).parse();
|
|
17
|
-
const stmt = page.actions[0].body[0];
|
|
18
|
-
assert.ok(stmt.alternate);
|
|
19
|
-
assert.equal(stmt.alternate.length, 1);
|
|
20
|
-
});
|