@tylix/compiler 0.6.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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,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
|
-
});
|
|
@@ -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
|
-
});
|