@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,41 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs/promises'
|
|
2
|
-
import path from 'node:path'
|
|
3
|
-
|
|
4
|
-
// A component package (like tylix-icons) ships a manifest at its
|
|
5
|
-
// root mapping exported names to the .tyx file that defines them:
|
|
6
|
-
// { "IconCheck": "icons/IconCheck.tyx", "IconTrash": "icons/IconTrash.tyx" }
|
|
7
|
-
// This keeps resolution to "read one JSON file, then read the .tyx
|
|
8
|
-
// files it points at" -- no bundler, no special package.json export
|
|
9
|
-
// map, just a plain manifest a package author (or a scaffolding tool)
|
|
10
|
-
// can hand-write.
|
|
11
|
-
const MANIFEST_FILE = 'tylix-components.json'
|
|
12
|
-
|
|
13
|
-
export async function resolvePackageComponents(imports, baseDir) {
|
|
14
|
-
const components = {}
|
|
15
|
-
|
|
16
|
-
for (const { names, package: pkgName } of imports) {
|
|
17
|
-
const pkgDir = path.join(baseDir, 'node_modules', pkgName)
|
|
18
|
-
const manifestPath = path.join(pkgDir, MANIFEST_FILE)
|
|
19
|
-
|
|
20
|
-
let manifest
|
|
21
|
-
try {
|
|
22
|
-
manifest = JSON.parse(await fs.readFile(manifestPath, 'utf-8'))
|
|
23
|
-
} catch {
|
|
24
|
-
throw new Error(
|
|
25
|
-
`Cannot resolve components from "${pkgName}": missing or invalid ${MANIFEST_FILE} in node_modules/${pkgName}. Is the package installed?`,
|
|
26
|
-
)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
for (const name of names) {
|
|
30
|
-
const relPath = manifest[name]
|
|
31
|
-
if (!relPath) {
|
|
32
|
-
throw new Error(
|
|
33
|
-
`"${pkgName}" does not export a component named "${name}" (checked ${MANIFEST_FILE})`,
|
|
34
|
-
)
|
|
35
|
-
}
|
|
36
|
-
components[name] = await fs.readFile(path.join(pkgDir, relPath), 'utf-8')
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return components
|
|
41
|
-
}
|
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
|
-
});
|
|
@@ -1,18 +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 a bare onMount body", () => {
|
|
7
|
-
const source = `state\ncount: 0\nonMount\nthis.count = 5`;
|
|
8
|
-
const page = new Parser(new Lexer(source).tokenize()).parse();
|
|
9
|
-
assert.ok(page.onMount);
|
|
10
|
-
assert.equal(page.onMount.body.length, 1);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test("parses an async onMount body with await", () => {
|
|
14
|
-
const source = `state\nposts: 0\nonMount\nasync {\n const data = await this.load()\n this.posts = data\n}`;
|
|
15
|
-
const page = new Parser(new Lexer(source).tokenize()).parse();
|
|
16
|
-
assert.equal(page.onMount.isAsync, true);
|
|
17
|
-
assert.equal(page.onMount.body.length, 2);
|
|
18
|
-
});
|
|
@@ -1,315 +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
|
-
const tokens = new Lexer(source).tokenize()
|
|
8
|
-
return new Parser(tokens).parse()
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// AST nodes carry extra bookkeeping fields (line, typeAnnotation,
|
|
12
|
-
// computed, optional) that most of these tests don't care about.
|
|
13
|
-
// pickShape keeps only the keys named in `shape`, so a test asserts
|
|
14
|
-
// on what it means to check and doesn't break when a node gains a
|
|
15
|
-
// field.
|
|
16
|
-
function pickShape(actual, shape) {
|
|
17
|
-
if (Array.isArray(shape)) {
|
|
18
|
-
return Array.isArray(actual)
|
|
19
|
-
? actual.map((item, i) => pickShape(item, shape[i]))
|
|
20
|
-
: actual
|
|
21
|
-
}
|
|
22
|
-
if (shape && typeof shape === 'object') {
|
|
23
|
-
const out = {}
|
|
24
|
-
for (const key of Object.keys(shape)) {
|
|
25
|
-
out[key] = pickShape(actual?.[key], shape[key])
|
|
26
|
-
}
|
|
27
|
-
return out
|
|
28
|
-
}
|
|
29
|
-
return actual
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function assertMatches(actual, shape) {
|
|
33
|
-
assert.deepEqual(pickShape(actual, shape), shape)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// The same content written braced or bare lands on different lines,
|
|
37
|
-
// so line numbers are dropped before comparing the two ASTs.
|
|
38
|
-
function withoutLines(node) {
|
|
39
|
-
return JSON.parse(
|
|
40
|
-
JSON.stringify(node, (key, value) => (key === 'line' ? undefined : value)),
|
|
41
|
-
)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
test('respects operator precedence: multiplication before addition', () => {
|
|
45
|
-
const source = `action\n compute() {\n return 1 + 2 * 3\n }`
|
|
46
|
-
const ast = new Parser(new Lexer(source).tokenize()).parse()
|
|
47
|
-
const method = ast.actions[0]
|
|
48
|
-
// 1 + (2 * 3), not (1 + 2) * 3
|
|
49
|
-
assert.equal(method.body[0].argument.type, 'BinaryExpression')
|
|
50
|
-
assert.equal(method.body[0].argument.operator, '+')
|
|
51
|
-
assert.equal(method.body[0].argument.right.operator, '*')
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
// Phase 5
|
|
55
|
-
test('desugars += into target = target + value', () => {
|
|
56
|
-
const ast = new Parser(
|
|
57
|
-
new Lexer('action\n inc() {\n this.count += 1\n }').tokenize(),
|
|
58
|
-
).parse()
|
|
59
|
-
const stmt = ast.actions[0].body[0]
|
|
60
|
-
assert.equal(stmt.expression.type, 'AssignmentExpression')
|
|
61
|
-
assert.equal(stmt.expression.value.type, 'BinaryExpression')
|
|
62
|
-
assert.equal(stmt.expression.value.operator, '+')
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
// Phase 6
|
|
66
|
-
test('parses a single-param arrow function used as a call argument', () => {
|
|
67
|
-
const ast = new Parser(
|
|
68
|
-
new Lexer(
|
|
69
|
-
'computed\n activeItems() {\n return this.items.filter(item => item.active)\n }',
|
|
70
|
-
).tokenize(),
|
|
71
|
-
).parse()
|
|
72
|
-
const returnArg = ast.computed[0].body[0].argument
|
|
73
|
-
const arrowArg = returnArg.args[0]
|
|
74
|
-
assert.equal(arrowArg.type, 'ArrowFunctionExpression')
|
|
75
|
-
assert.deepEqual(arrowArg.params, ['item'])
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
// Phase 4
|
|
79
|
-
test('parses a template literal with a single interpolation', () => {
|
|
80
|
-
const ast = new Parser(
|
|
81
|
-
new Lexer(
|
|
82
|
-
'action\n greet() {\n this.message = `Hello ${this.name}`\n }',
|
|
83
|
-
).tokenize(),
|
|
84
|
-
).parse()
|
|
85
|
-
const value = ast.actions[0].body[0].expression.value
|
|
86
|
-
assert.equal(value.type, 'TemplateLiteralExpression')
|
|
87
|
-
assert.equal(value.parts[0].value, 'Hello ')
|
|
88
|
-
assert.equal(value.parts[1].expression.type, 'MemberExpression')
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
test('parses a props block', () => {
|
|
92
|
-
const ast = parse(`props { title: String }`)
|
|
93
|
-
assertMatches(ast.props, [
|
|
94
|
-
{ type: 'Prop', name: 'title', propType: 'String' },
|
|
95
|
-
])
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
test('parses a bare props block with a prop named title', () => {
|
|
99
|
-
const ast = parse(`props\ntitle: String`)
|
|
100
|
-
assertMatches(ast.props, [
|
|
101
|
-
{ type: 'Prop', name: 'title', propType: 'String' },
|
|
102
|
-
])
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
test('accepts other keyword tokens as prop names', () => {
|
|
106
|
-
const ast = parse(`props { title: String, layout: String }`)
|
|
107
|
-
assert.equal(ast.props.length, 2)
|
|
108
|
-
assert.equal(ast.props[0].name, 'title')
|
|
109
|
-
assert.equal(ast.props[1].name, 'layout')
|
|
110
|
-
})
|
|
111
|
-
|
|
112
|
-
test('a page-level title section still parses after a bare props block', () => {
|
|
113
|
-
const ast = parse(`props\nname: String\ntitle "Home"`)
|
|
114
|
-
assert.equal(ast.props.length, 1)
|
|
115
|
-
assert.equal(ast.title, 'Home')
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
test('parses a state block with number and string values', () => {
|
|
119
|
-
const ast = parse(`state { count: 0, label: "hi" }`)
|
|
120
|
-
assertMatches(ast.state, [
|
|
121
|
-
{ type: 'State', name: 'count', value: { type: 'Literal', value: 0 } },
|
|
122
|
-
{ type: 'State', name: 'label', value: { type: 'Literal', value: 'hi' } },
|
|
123
|
-
])
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
test('parses a computed block with return + member/binary expressions', () => {
|
|
127
|
-
const ast = parse(`
|
|
128
|
-
computed {
|
|
129
|
-
doubled() {
|
|
130
|
-
return this.count * 2
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
`)
|
|
134
|
-
const method = ast.computed[0]
|
|
135
|
-
assert.equal(method.name, 'doubled')
|
|
136
|
-
assert.deepEqual(method.params, [])
|
|
137
|
-
assert.equal(method.body[0].type, 'ReturnStatement')
|
|
138
|
-
assertMatches(method.body[0].argument, {
|
|
139
|
-
type: 'BinaryExpression',
|
|
140
|
-
operator: '*',
|
|
141
|
-
left: {
|
|
142
|
-
type: 'MemberExpression',
|
|
143
|
-
object: { type: 'Identifier', name: 'this' },
|
|
144
|
-
property: 'count',
|
|
145
|
-
},
|
|
146
|
-
right: { type: 'Literal', value: 2 },
|
|
147
|
-
})
|
|
148
|
-
})
|
|
149
|
-
|
|
150
|
-
test('parses an action block with an assignment statement', () => {
|
|
151
|
-
const ast = parse(`
|
|
152
|
-
action {
|
|
153
|
-
increment() {
|
|
154
|
-
count = count + 1;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
`)
|
|
158
|
-
const method = ast.actions[0]
|
|
159
|
-
assert.equal(method.name, 'increment')
|
|
160
|
-
assert.deepEqual(method.body[0].expression, {
|
|
161
|
-
type: 'AssignmentExpression',
|
|
162
|
-
target: { type: 'Identifier', name: 'count' },
|
|
163
|
-
value: {
|
|
164
|
-
type: 'BinaryExpression',
|
|
165
|
-
operator: '+',
|
|
166
|
-
left: { type: 'Identifier', name: 'count' },
|
|
167
|
-
right: { type: 'Literal', value: 1 },
|
|
168
|
-
},
|
|
169
|
-
})
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
test('parses a method with parameters', () => {
|
|
173
|
-
const ast = parse(`
|
|
174
|
-
action {
|
|
175
|
-
setCount(newValue) {
|
|
176
|
-
count = newValue;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
`)
|
|
180
|
-
// Params carry an optional type annotation, so they are objects.
|
|
181
|
-
assert.deepEqual(ast.actions[0].params, [
|
|
182
|
-
{ name: 'newValue', typeAnnotation: null },
|
|
183
|
-
])
|
|
184
|
-
})
|
|
185
|
-
|
|
186
|
-
test('accepts a keyword token as a method parameter name', () => {
|
|
187
|
-
const ast = parse(`
|
|
188
|
-
action {
|
|
189
|
-
setTitle(title) {
|
|
190
|
-
this.title = title
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
`)
|
|
194
|
-
assert.deepEqual(ast.actions[0].params, [
|
|
195
|
-
{ name: 'title', typeAnnotation: null },
|
|
196
|
-
])
|
|
197
|
-
})
|
|
198
|
-
|
|
199
|
-
test('accepts a keyword token as a destructured property and binding name', () => {
|
|
200
|
-
assert.doesNotThrow(() =>
|
|
201
|
-
parse(`
|
|
202
|
-
action {
|
|
203
|
-
load() {
|
|
204
|
-
const { title } = this.data
|
|
205
|
-
const layout = title
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
`),
|
|
209
|
-
)
|
|
210
|
-
})
|
|
211
|
-
|
|
212
|
-
test('accepts a keyword token as a key in an object type annotation', () => {
|
|
213
|
-
assert.doesNotThrow(() => parse(`state { post({ title: string }): null }`))
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
test('parses a full realistic page script', () => {
|
|
217
|
-
const ast = parse(`
|
|
218
|
-
props { title: String }
|
|
219
|
-
state { count: 0 }
|
|
220
|
-
computed {
|
|
221
|
-
doubled() { return count * 2 }
|
|
222
|
-
}
|
|
223
|
-
action {
|
|
224
|
-
increment() { count = count + 1; }
|
|
225
|
-
}
|
|
226
|
-
`)
|
|
227
|
-
assert.equal(ast.props.length, 1)
|
|
228
|
-
assert.equal(ast.state.length, 1)
|
|
229
|
-
assert.equal(ast.computed.length, 1)
|
|
230
|
-
assert.equal(ast.actions.length, 1)
|
|
231
|
-
})
|
|
232
|
-
|
|
233
|
-
test('throws a clear error when a state entry is missing its colon', () => {
|
|
234
|
-
assert.throws(() => parse(`state { count 0 }`), /Expected ':'/)
|
|
235
|
-
})
|
|
236
|
-
|
|
237
|
-
test('throws a clear error on an unknown top-level keyword', () => {
|
|
238
|
-
assert.throws(() => parse(`bogus { }`), /Unexpected token/)
|
|
239
|
-
})
|
|
240
|
-
|
|
241
|
-
test('parses a bare (unbraced) state block', () => {
|
|
242
|
-
const source = `state\ncount: 0\nname: "Ada"`
|
|
243
|
-
const tokens = new Lexer(source).tokenize()
|
|
244
|
-
const page = new Parser(tokens).parse()
|
|
245
|
-
assert.equal(page.state.length, 2)
|
|
246
|
-
assert.equal(page.state[0].name, 'count')
|
|
247
|
-
assert.equal(page.state[1].name, 'name')
|
|
248
|
-
})
|
|
249
|
-
|
|
250
|
-
test('parses bare action and computed sections back to back', () => {
|
|
251
|
-
const source = `
|
|
252
|
-
computed
|
|
253
|
-
doubled() {
|
|
254
|
-
return this.count * 2
|
|
255
|
-
}
|
|
256
|
-
action
|
|
257
|
-
increment() {
|
|
258
|
-
this.count = this.count + 1
|
|
259
|
-
}
|
|
260
|
-
`
|
|
261
|
-
const tokens = new Lexer(source).tokenize()
|
|
262
|
-
const page = new Parser(tokens).parse()
|
|
263
|
-
assert.equal(page.computed.length, 1)
|
|
264
|
-
assert.equal(page.computed[0].name, 'doubled')
|
|
265
|
-
assert.equal(page.actions.length, 1)
|
|
266
|
-
assert.equal(page.actions[0].name, 'increment')
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
test('bare state block correctly stops at the next section keyword', () => {
|
|
270
|
-
const source = `state\ncount: 0\naction\nincrement() { this.count = this.count + 1 }`
|
|
271
|
-
const tokens = new Lexer(source).tokenize()
|
|
272
|
-
const page = new Parser(tokens).parse()
|
|
273
|
-
assert.equal(page.state.length, 1)
|
|
274
|
-
assert.equal(page.actions.length, 1)
|
|
275
|
-
})
|
|
276
|
-
|
|
277
|
-
test('braced and bare syntax produce identical ASTs for the same content', () => {
|
|
278
|
-
const braced = `state { count: 0 }`
|
|
279
|
-
const bare = `state\ncount: 0`
|
|
280
|
-
|
|
281
|
-
const bracedPage = new Parser(new Lexer(braced).tokenize()).parse()
|
|
282
|
-
const barePage = new Parser(new Lexer(bare).tokenize()).parse()
|
|
283
|
-
|
|
284
|
-
// Same content, different line numbers (1 vs 2), so compare
|
|
285
|
-
// everything except `line`.
|
|
286
|
-
assert.deepEqual(withoutLines(bracedPage.state), withoutLines(barePage.state))
|
|
287
|
-
})
|
|
288
|
-
|
|
289
|
-
test('parses a method call with no arguments inside an action body', () => {
|
|
290
|
-
const source = `action\nload() {\n this.fetch()\n}`
|
|
291
|
-
const tokens = new Lexer(source).tokenize()
|
|
292
|
-
const page = new Parser(tokens).parse()
|
|
293
|
-
const stmt = page.actions[0].body[0]
|
|
294
|
-
assert.equal(stmt.expression.type, 'CallExpression')
|
|
295
|
-
})
|
|
296
|
-
|
|
297
|
-
test('parses a call with arguments', () => {
|
|
298
|
-
const source = `action\nremove(id) {\n this.deleteItem(id)\n}`
|
|
299
|
-
const tokens = new Lexer(source).tokenize()
|
|
300
|
-
const page = new Parser(tokens).parse()
|
|
301
|
-
const call = page.actions[0].body[0].expression
|
|
302
|
-
assert.equal(call.type, 'CallExpression')
|
|
303
|
-
assert.equal(call.args.length, 1)
|
|
304
|
-
assert.equal(call.args[0].name, 'id')
|
|
305
|
-
})
|
|
306
|
-
|
|
307
|
-
test('parses chained member access followed by a call', () => {
|
|
308
|
-
const source = `action\ndoIt() {\n this.api.remove(1)\n}`
|
|
309
|
-
const tokens = new Lexer(source).tokenize()
|
|
310
|
-
const page = new Parser(tokens).parse()
|
|
311
|
-
const call = page.actions[0].body[0].expression
|
|
312
|
-
assert.equal(call.type, 'CallExpression')
|
|
313
|
-
assert.equal(call.callee.type, 'MemberExpression')
|
|
314
|
-
assert.equal(call.callee.property, 'remove')
|
|
315
|
-
})
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { parseComponent } from "./parseComponent.js";
|
|
4
|
-
|
|
5
|
-
test("extracts script, template, and style sections", () => {
|
|
6
|
-
const source = `
|
|
7
|
-
<script>
|
|
8
|
-
state { count: 0 }
|
|
9
|
-
</script>
|
|
10
|
-
|
|
11
|
-
<template>
|
|
12
|
-
<div>{{ count }}</div>
|
|
13
|
-
</template>
|
|
14
|
-
|
|
15
|
-
<style>
|
|
16
|
-
div { color: red; }
|
|
17
|
-
</style>
|
|
18
|
-
`;
|
|
19
|
-
|
|
20
|
-
const result = parseComponent(source);
|
|
21
|
-
assert.match(result.script, /state \{ count: 0 \}/);
|
|
22
|
-
assert.match(result.template, /<div>\{\{ count \}\}<\/div>/);
|
|
23
|
-
assert.match(result.style, /div \{ color: red; \}/);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test("style is optional and defaults to empty string", () => {
|
|
27
|
-
const source = `
|
|
28
|
-
<script>
|
|
29
|
-
state { count: 0 }
|
|
30
|
-
</script>
|
|
31
|
-
|
|
32
|
-
<template>
|
|
33
|
-
<div>Hello</div>
|
|
34
|
-
</template>
|
|
35
|
-
`;
|
|
36
|
-
const result = parseComponent(source);
|
|
37
|
-
assert.equal(result.style, "");
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test("script is optional and defaults to empty string", () => {
|
|
41
|
-
const source = `
|
|
42
|
-
<template>
|
|
43
|
-
<div>Static content</div>
|
|
44
|
-
</template>
|
|
45
|
-
`;
|
|
46
|
-
const result = parseComponent(source);
|
|
47
|
-
assert.equal(result.script, "");
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test("throws when <template> is missing", () => {
|
|
51
|
-
const source = `<script>state { x: 1 }</script>`;
|
|
52
|
-
assert.throws(() => parseComponent(source), /missing a required <template>/);
|
|
53
|
-
});
|