@tylix/compiler 0.1.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/package.json +12 -0
- package/src/ast/nodes.js +143 -0
- package/src/codegen/generateExpression.array.test.js +13 -0
- package/src/codegen/generateExpression.callexpr.test.js +19 -0
- package/src/codegen/generateExpression.js +92 -0
- package/src/codegen/generateExpression.test.js +59 -0
- package/src/codegen/generateMethod.asyncawait.test.js +37 -0
- package/src/codegen/generateMethod.ifstatement.test.js +20 -0
- package/src/codegen/generateMethod.js +13 -0
- package/src/codegen/generateMethod.test.js +79 -0
- package/src/codegen/generatePage.js +79 -0
- package/src/codegen/generatePage.onmount.test.js +33 -0
- package/src/codegen/generatePage.test.js +138 -0
- package/src/codegen/generateStatement.js +64 -0
- package/src/codegen/generateStatement.test.js +31 -0
- package/src/codegen/generateTemplate.ifeach.test.js +138 -0
- package/src/codegen/generateTemplate.js +264 -0
- package/src/codegen/generateTemplate.test.js +149 -0
- package/src/codegen/generateTemplateExpression.js +55 -0
- package/src/codegen/generateTemplateExpression.test.js +34 -0
- package/src/compileComponent.js +54 -0
- package/src/compileComponent.test.js +121 -0
- package/src/index.js +15 -0
- package/src/lexer/Lexer.js +336 -0
- package/src/lexer/Lexer.test.js +151 -0
- package/src/lexer/tokenTypes.js +114 -0
- package/src/parser/Parser.array.test.js +37 -0
- package/src/parser/Parser.asyncawait.test.js +33 -0
- package/src/parser/Parser.ifstatement.test.js +20 -0
- package/src/parser/Parser.js +677 -0
- package/src/parser/Parser.onmount.test.js +18 -0
- package/src/parser/Parser.test.js +227 -0
- package/src/parser/parseComponent.js +29 -0
- package/src/parser/parseComponent.test.js +53 -0
- package/src/parser/parseComponentFile.js +55 -0
- package/src/parser/parseExpressionString.js +13 -0
- package/src/parser/parsePageFile.js +94 -0
- package/src/parser/parsePageFile.test.js +101 -0
- package/src/parser/parseTemplate.ifeach.test.js +33 -0
- package/src/parser/parseTemplate.js +319 -0
- package/src/parser/parseTemplate.pre.test.js +39 -0
- package/src/parser/parseTemplate.test.js +96 -0
- package/src/renderPageDocument.js +250 -0
- package/src/renderPageDocument.test.js +125 -0
- package/src/runtime/loadingIndicator.js +35 -0
- package/src/runtime/reactive.js +59 -0
- package/src/runtime/reactive.test.js +106 -0
- package/src/runtime/useApi.js +45 -0
- package/src/runtime/useChannel.js +38 -0
|
@@ -0,0 +1,227 @@
|
|
|
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
|
+
test('respects operator precedence: multiplication before addition', () => {
|
|
12
|
+
const source = `action\n compute() {\n return 1 + 2 * 3\n }`
|
|
13
|
+
const ast = new Parser(new Lexer(source).tokenize()).parse()
|
|
14
|
+
const method = ast.actions[0]
|
|
15
|
+
// 1 + (2 * 3), not (1 + 2) * 3
|
|
16
|
+
assert.equal(method.body[0].argument.type, 'BinaryExpression')
|
|
17
|
+
assert.equal(method.body[0].argument.operator, '+')
|
|
18
|
+
assert.equal(method.body[0].argument.right.operator, '*')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
// Phase 5
|
|
22
|
+
test('desugars += into target = target + value', () => {
|
|
23
|
+
const ast = new Parser(
|
|
24
|
+
new Lexer('action\n inc() {\n this.count += 1\n }').tokenize(),
|
|
25
|
+
).parse()
|
|
26
|
+
const stmt = ast.actions[0].body[0]
|
|
27
|
+
assert.equal(stmt.expression.type, 'AssignmentExpression')
|
|
28
|
+
assert.equal(stmt.expression.value.type, 'BinaryExpression')
|
|
29
|
+
assert.equal(stmt.expression.value.operator, '+')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
// Phase 6
|
|
33
|
+
test('parses a single-param arrow function used as a call argument', () => {
|
|
34
|
+
const ast = new Parser(
|
|
35
|
+
new Lexer(
|
|
36
|
+
'computed\n activeItems() {\n return this.items.filter(item => item.active)\n }',
|
|
37
|
+
).tokenize(),
|
|
38
|
+
).parse()
|
|
39
|
+
const returnArg = ast.computed[0].body[0].argument
|
|
40
|
+
const arrowArg = returnArg.args[0]
|
|
41
|
+
assert.equal(arrowArg.type, 'ArrowFunctionExpression')
|
|
42
|
+
assert.deepEqual(arrowArg.params, ['item'])
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
// Phase 4
|
|
46
|
+
test('parses a template literal with a single interpolation', () => {
|
|
47
|
+
const ast = new Parser(
|
|
48
|
+
new Lexer(
|
|
49
|
+
'action\n greet() {\n this.message = `Hello ${this.name}`\n }',
|
|
50
|
+
).tokenize(),
|
|
51
|
+
).parse()
|
|
52
|
+
const value = ast.actions[0].body[0].expression.value
|
|
53
|
+
assert.equal(value.type, 'TemplateLiteralExpression')
|
|
54
|
+
assert.equal(value.parts[0].value, 'Hello ')
|
|
55
|
+
assert.equal(value.parts[1].expression.type, 'MemberExpression')
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('parses a props block', () => {
|
|
59
|
+
const ast = parse(`props { title: String }`)
|
|
60
|
+
assert.deepEqual(ast.props, [
|
|
61
|
+
{ type: 'Prop', name: 'title', propType: 'String' },
|
|
62
|
+
])
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('parses a state block with number and string values', () => {
|
|
66
|
+
const ast = parse(`state { count: 0, label: "hi" }`)
|
|
67
|
+
assert.deepEqual(ast.state, [
|
|
68
|
+
{ type: 'State', name: 'count', value: { type: 'Literal', value: 0 } },
|
|
69
|
+
{ type: 'State', name: 'label', value: { type: 'Literal', value: 'hi' } },
|
|
70
|
+
])
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('parses a computed block with return + member/binary expressions', () => {
|
|
74
|
+
const ast = parse(`
|
|
75
|
+
computed {
|
|
76
|
+
doubled() {
|
|
77
|
+
return this.count * 2
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
`)
|
|
81
|
+
const method = ast.computed[0]
|
|
82
|
+
assert.equal(method.name, 'doubled')
|
|
83
|
+
assert.deepEqual(method.params, [])
|
|
84
|
+
assert.equal(method.body[0].type, 'ReturnStatement')
|
|
85
|
+
assert.deepEqual(method.body[0].argument, {
|
|
86
|
+
type: 'BinaryExpression',
|
|
87
|
+
operator: '*',
|
|
88
|
+
left: {
|
|
89
|
+
type: 'MemberExpression',
|
|
90
|
+
object: { type: 'Identifier', name: 'this' },
|
|
91
|
+
property: 'count',
|
|
92
|
+
},
|
|
93
|
+
right: { type: 'Literal', value: 2 },
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test('parses an action block with an assignment statement', () => {
|
|
98
|
+
const ast = parse(`
|
|
99
|
+
action {
|
|
100
|
+
increment() {
|
|
101
|
+
count = count + 1;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
`)
|
|
105
|
+
const method = ast.actions[0]
|
|
106
|
+
assert.equal(method.name, 'increment')
|
|
107
|
+
assert.deepEqual(method.body[0].expression, {
|
|
108
|
+
type: 'AssignmentExpression',
|
|
109
|
+
target: { type: 'Identifier', name: 'count' },
|
|
110
|
+
value: {
|
|
111
|
+
type: 'BinaryExpression',
|
|
112
|
+
operator: '+',
|
|
113
|
+
left: { type: 'Identifier', name: 'count' },
|
|
114
|
+
right: { type: 'Literal', value: 1 },
|
|
115
|
+
},
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('parses a method with parameters', () => {
|
|
120
|
+
const ast = parse(`
|
|
121
|
+
action {
|
|
122
|
+
setCount(newValue) {
|
|
123
|
+
count = newValue;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
`)
|
|
127
|
+
assert.deepEqual(ast.actions[0].params, ['newValue'])
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
test('parses a full realistic page script', () => {
|
|
131
|
+
const ast = parse(`
|
|
132
|
+
props { title: String }
|
|
133
|
+
state { count: 0 }
|
|
134
|
+
computed {
|
|
135
|
+
doubled() { return count * 2 }
|
|
136
|
+
}
|
|
137
|
+
action {
|
|
138
|
+
increment() { count = count + 1; }
|
|
139
|
+
}
|
|
140
|
+
`)
|
|
141
|
+
assert.equal(ast.props.length, 1)
|
|
142
|
+
assert.equal(ast.state.length, 1)
|
|
143
|
+
assert.equal(ast.computed.length, 1)
|
|
144
|
+
assert.equal(ast.actions.length, 1)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('throws a clear error when a state entry is missing its colon', () => {
|
|
148
|
+
assert.throws(() => parse(`state { count 0 }`), /Expected ':'/)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
test('throws a clear error on an unknown top-level keyword', () => {
|
|
152
|
+
assert.throws(() => parse(`bogus { }`), /Unexpected token/)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test('parses a bare (unbraced) state block', () => {
|
|
156
|
+
const source = `state\ncount: 0\nname: "Ada"`
|
|
157
|
+
const tokens = new Lexer(source).tokenize()
|
|
158
|
+
const page = new Parser(tokens).parse()
|
|
159
|
+
assert.equal(page.state.length, 2)
|
|
160
|
+
assert.equal(page.state[0].name, 'count')
|
|
161
|
+
assert.equal(page.state[1].name, 'name')
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
test('parses bare action and computed sections back to back', () => {
|
|
165
|
+
const source = `
|
|
166
|
+
computed
|
|
167
|
+
doubled() {
|
|
168
|
+
return this.count * 2
|
|
169
|
+
}
|
|
170
|
+
action
|
|
171
|
+
increment() {
|
|
172
|
+
this.count = this.count + 1
|
|
173
|
+
}
|
|
174
|
+
`
|
|
175
|
+
const tokens = new Lexer(source).tokenize()
|
|
176
|
+
const page = new Parser(tokens).parse()
|
|
177
|
+
assert.equal(page.computed.length, 1)
|
|
178
|
+
assert.equal(page.computed[0].name, 'doubled')
|
|
179
|
+
assert.equal(page.actions.length, 1)
|
|
180
|
+
assert.equal(page.actions[0].name, 'increment')
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
test('bare state block correctly stops at the next section keyword', () => {
|
|
184
|
+
const source = `state\ncount: 0\naction\nincrement() { this.count = this.count + 1 }`
|
|
185
|
+
const tokens = new Lexer(source).tokenize()
|
|
186
|
+
const page = new Parser(tokens).parse()
|
|
187
|
+
assert.equal(page.state.length, 1)
|
|
188
|
+
assert.equal(page.actions.length, 1)
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
test('braced and bare syntax produce identical ASTs for the same content', () => {
|
|
192
|
+
const braced = `state { count: 0 }`
|
|
193
|
+
const bare = `state\ncount: 0`
|
|
194
|
+
|
|
195
|
+
const bracedPage = new Parser(new Lexer(braced).tokenize()).parse()
|
|
196
|
+
const barePage = new Parser(new Lexer(bare).tokenize()).parse()
|
|
197
|
+
|
|
198
|
+
assert.deepEqual(bracedPage.state, barePage.state)
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
test('parses a method call with no arguments inside an action body', () => {
|
|
202
|
+
const source = `action\nload() {\n this.fetch()\n}`
|
|
203
|
+
const tokens = new Lexer(source).tokenize()
|
|
204
|
+
const page = new Parser(tokens).parse()
|
|
205
|
+
const stmt = page.actions[0].body[0]
|
|
206
|
+
assert.equal(stmt.expression.type, 'CallExpression')
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
test('parses a call with arguments', () => {
|
|
210
|
+
const source = `action\nremove(id) {\n this.deleteItem(id)\n}`
|
|
211
|
+
const tokens = new Lexer(source).tokenize()
|
|
212
|
+
const page = new Parser(tokens).parse()
|
|
213
|
+
const call = page.actions[0].body[0].expression
|
|
214
|
+
assert.equal(call.type, 'CallExpression')
|
|
215
|
+
assert.equal(call.args.length, 1)
|
|
216
|
+
assert.equal(call.args[0].name, 'id')
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
test('parses chained member access followed by a call', () => {
|
|
220
|
+
const source = `action\ndoIt() {\n this.api.remove(1)\n}`
|
|
221
|
+
const tokens = new Lexer(source).tokenize()
|
|
222
|
+
const page = new Parser(tokens).parse()
|
|
223
|
+
const call = page.actions[0].body[0].expression
|
|
224
|
+
assert.equal(call.type, 'CallExpression')
|
|
225
|
+
assert.equal(call.callee.type, 'MemberExpression')
|
|
226
|
+
assert.equal(call.callee.property, 'remove')
|
|
227
|
+
})
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits a .tyx single-file component into its three named sections.
|
|
3
|
+
* Tylix components always have exactly this shape:
|
|
4
|
+
*
|
|
5
|
+
* <script> ... </script>
|
|
6
|
+
* <template> ... </template>
|
|
7
|
+
* <style> ... </style> (optional)
|
|
8
|
+
*/
|
|
9
|
+
export function parseComponent(source) {
|
|
10
|
+
const script = extractTag(source, "script");
|
|
11
|
+
const template = extractTag(source, "template");
|
|
12
|
+
const style = extractTag(source, "style");
|
|
13
|
+
|
|
14
|
+
if (template === null) {
|
|
15
|
+
throw new Error(".tyx component is missing a required <template> block.");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
script: script ?? "",
|
|
20
|
+
template,
|
|
21
|
+
style: style ?? "",
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function extractTag(source, tagName) {
|
|
26
|
+
const pattern = new RegExp(`<${tagName}>([\\s\\S]*?)</${tagName}>`);
|
|
27
|
+
const match = pattern.exec(source);
|
|
28
|
+
return match ? match[1].trim() : null;
|
|
29
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses a native "component Name" .tyx file -- structurally
|
|
3
|
+
* identical to parsePageFile's "page Name" format (same state/
|
|
4
|
+
* computed/action/template/style sections, same boundary-finding),
|
|
5
|
+
* just a different header keyword and no page-specific semantics.
|
|
6
|
+
*/
|
|
7
|
+
import { findSectionBoundaries } from './parsePageFile.js'
|
|
8
|
+
|
|
9
|
+
export function parseComponentFile(source) {
|
|
10
|
+
const componentMatch = /^\s*component\s+([A-Za-z_$][A-Za-z0-9_$]*)/.exec(
|
|
11
|
+
source,
|
|
12
|
+
)
|
|
13
|
+
if (!componentMatch) {
|
|
14
|
+
throw new Error('.tyx component file must start with "component <Name>"')
|
|
15
|
+
}
|
|
16
|
+
const componentName = componentMatch[1]
|
|
17
|
+
|
|
18
|
+
const afterDeclaration = source.slice(
|
|
19
|
+
componentMatch.index + componentMatch[0].length,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
const boundaries = findSectionBoundaries(afterDeclaration)
|
|
23
|
+
|
|
24
|
+
const scriptEnd =
|
|
25
|
+
boundaries.find((b) => b.keyword === 'template')?.start ??
|
|
26
|
+
afterDeclaration.length
|
|
27
|
+
const scriptSource = afterDeclaration.slice(0, scriptEnd).trim()
|
|
28
|
+
|
|
29
|
+
const templateBoundary = boundaries.find((b) => b.keyword === 'template')
|
|
30
|
+
const styleBoundary = boundaries.find((b) => b.keyword === 'style')
|
|
31
|
+
|
|
32
|
+
let template = ''
|
|
33
|
+
if (templateBoundary) {
|
|
34
|
+
const templateEnd =
|
|
35
|
+
styleBoundary && styleBoundary.start > templateBoundary.start
|
|
36
|
+
? styleBoundary.start
|
|
37
|
+
: afterDeclaration.length
|
|
38
|
+
template = afterDeclaration
|
|
39
|
+
.slice(templateBoundary.contentStart, templateEnd)
|
|
40
|
+
.trim()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let style = ''
|
|
44
|
+
if (styleBoundary) {
|
|
45
|
+
style = afterDeclaration.slice(styleBoundary.contentStart).trim()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!templateBoundary) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Component "${componentName}" is missing a required "template" section.`,
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return { componentName, script: scriptSource, template, style }
|
|
55
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Lexer } from "../lexer/Lexer.js";
|
|
2
|
+
import { Parser } from "./Parser.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Parses a single expression (no statements) from a raw string, e.g.
|
|
6
|
+
* the inside of a template interpolation: "count * 2" or "increment".
|
|
7
|
+
* Reuses Parser's expression grammar directly rather than duplicating it.
|
|
8
|
+
*/
|
|
9
|
+
export function parseExpressionString(source) {
|
|
10
|
+
const tokens = new Lexer(source).tokenize();
|
|
11
|
+
const parser = new Parser(tokens);
|
|
12
|
+
return parser.parseExpression();
|
|
13
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
const SECTION_KEYWORDS = ['state', 'computed', 'action', 'template', 'style']
|
|
2
|
+
|
|
3
|
+
export function parsePageFile(source) {
|
|
4
|
+
const pageMatch = /^\s*page\s+([A-Za-z_$][A-Za-z0-9_$]*)/.exec(source)
|
|
5
|
+
if (!pageMatch) {
|
|
6
|
+
throw new Error('.tyx file must start with "page <Name>"')
|
|
7
|
+
}
|
|
8
|
+
const pageName = pageMatch[1]
|
|
9
|
+
|
|
10
|
+
const afterPageDeclaration = source.slice(
|
|
11
|
+
pageMatch.index + pageMatch[0].length,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
const boundaries = findSectionBoundaries(afterPageDeclaration)
|
|
15
|
+
|
|
16
|
+
const scriptEnd =
|
|
17
|
+
boundaries.find((b) => b.keyword === 'template')?.start ??
|
|
18
|
+
afterPageDeclaration.length
|
|
19
|
+
const rawScriptSource = afterPageDeclaration.slice(0, scriptEnd)
|
|
20
|
+
const scriptSource = rawScriptSource.trim()
|
|
21
|
+
|
|
22
|
+
const leadingWhitespaceLength =
|
|
23
|
+
rawScriptSource.length - rawScriptSource.trimStart().length
|
|
24
|
+
const trimmedPrefix = rawScriptSource.slice(0, leadingWhitespaceLength)
|
|
25
|
+
const linesBeforeScript =
|
|
26
|
+
source.slice(0, pageMatch.index + pageMatch[0].length).split('\n').length -
|
|
27
|
+
1
|
|
28
|
+
const scriptStartLine = linesBeforeScript + trimmedPrefix.split('\n').length
|
|
29
|
+
|
|
30
|
+
const templateBoundary = boundaries.find((b) => b.keyword === 'template')
|
|
31
|
+
const styleBoundary = boundaries.find((b) => b.keyword === 'style')
|
|
32
|
+
|
|
33
|
+
let template = ''
|
|
34
|
+
let templateStartLine = null
|
|
35
|
+
if (templateBoundary) {
|
|
36
|
+
const templateEnd =
|
|
37
|
+
styleBoundary && styleBoundary.start > templateBoundary.start
|
|
38
|
+
? styleBoundary.start
|
|
39
|
+
: afterPageDeclaration.length
|
|
40
|
+
const rawTemplateSource = afterPageDeclaration.slice(
|
|
41
|
+
templateBoundary.contentStart,
|
|
42
|
+
templateEnd,
|
|
43
|
+
)
|
|
44
|
+
template = rawTemplateSource.trim()
|
|
45
|
+
|
|
46
|
+
const templateLeadingWhitespaceLength =
|
|
47
|
+
rawTemplateSource.length - rawTemplateSource.trimStart().length
|
|
48
|
+
const templateTrimmedPrefix = rawTemplateSource.slice(
|
|
49
|
+
0,
|
|
50
|
+
templateLeadingWhitespaceLength,
|
|
51
|
+
)
|
|
52
|
+
const absoluteContentStart =
|
|
53
|
+
pageMatch.index + pageMatch[0].length + templateBoundary.contentStart
|
|
54
|
+
const linesBeforeTemplate =
|
|
55
|
+
source.slice(0, absoluteContentStart).split('\n').length - 1
|
|
56
|
+
templateStartLine =
|
|
57
|
+
linesBeforeTemplate + templateTrimmedPrefix.split('\n').length
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let style = ''
|
|
61
|
+
if (styleBoundary) {
|
|
62
|
+
style = afterPageDeclaration.slice(styleBoundary.contentStart).trim()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!templateBoundary) {
|
|
66
|
+
throw new Error('.tyx file is missing a required "template" section.')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
pageName,
|
|
71
|
+
script: scriptSource,
|
|
72
|
+
template,
|
|
73
|
+
style,
|
|
74
|
+
scriptStartLine,
|
|
75
|
+
templateStartLine,
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function findSectionBoundaries(source) {
|
|
80
|
+
const boundaries = []
|
|
81
|
+
const lineStartPattern =
|
|
82
|
+
/(^|\n)[ \t]*(state|computed|action|template|style)\b/g
|
|
83
|
+
let match
|
|
84
|
+
while ((match = lineStartPattern.exec(source)) !== null) {
|
|
85
|
+
const keyword = match[2]
|
|
86
|
+
const keywordStart = match.index + match[0].indexOf(keyword)
|
|
87
|
+
boundaries.push({
|
|
88
|
+
keyword,
|
|
89
|
+
start: keywordStart,
|
|
90
|
+
contentStart: keywordStart + keyword.length,
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
return boundaries
|
|
94
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
});
|