@tylix/compiler 0.5.1 → 0.6.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 +1 -1
- package/src/ast/nodes.js +91 -10
- package/src/ast/substituteCapabilityRefs.js +135 -58
- package/src/codegen/emitStatementLines.js +42 -48
- package/src/codegen/generateExpression.js +68 -24
- package/src/codegen/generateMethod.js +5 -3
- package/src/codegen/generatePage.js +74 -44
- package/src/codegen/generateStatement.js +56 -12
- package/src/codegen/generateTemplate.js +124 -53
- package/src/codegen/generateTemplateExpression.js +40 -50
- package/src/codegen/jsGlobals.js +151 -0
- package/src/compileComponent.js +2 -2
- 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 +308 -213
- package/src/lexer/tokenTypes.js +32 -7
- package/src/parser/Parser.js +680 -169
- package/src/parser/languageOptions.js +16 -0
- package/src/parser/parseExpressionString.js +18 -7
- package/src/parser/parsePageFile.js +139 -58
- package/src/parser/parseTemplate.js +202 -26
- package/src/renderPageDocument.js +297 -51
- package/src/runtime/capability.js +32 -17
- package/src/runtime/loadingIndicator.js +91 -29
- package/src/runtime/reactive.js +128 -58
- package/src/typecheck/buildVirtualTs.js +13 -12
- package/src/typecheck/typedTemplateExpr.js +115 -36
- package/src/typecheckPage.js +28 -4
- package/src/imports/resolvePackageComponents.js +0 -41
package/package.json
CHANGED
package/src/ast/nodes.js
CHANGED
|
@@ -58,6 +58,20 @@ export function MethodNode(name, params, body, returnType = null) {
|
|
|
58
58
|
return { type: 'Method', name, params, body, returnType }
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
// A function/method parameter:
|
|
62
|
+
// name plain name, or destructuring source text ("{ a, b }")
|
|
63
|
+
// typeAnnotation TS type text or null
|
|
64
|
+
// defaultValue expression node or null
|
|
65
|
+
// rest true for `...name`
|
|
66
|
+
export function Param(
|
|
67
|
+
name,
|
|
68
|
+
typeAnnotation = null,
|
|
69
|
+
defaultValue = null,
|
|
70
|
+
rest = false,
|
|
71
|
+
) {
|
|
72
|
+
return { name, typeAnnotation, defaultValue, rest }
|
|
73
|
+
}
|
|
74
|
+
|
|
61
75
|
export function AssignmentExpr(target, value) {
|
|
62
76
|
return { type: 'AssignmentExpression', target, value }
|
|
63
77
|
}
|
|
@@ -103,6 +117,10 @@ export function ExpressionStatement(expression) {
|
|
|
103
117
|
return { type: 'ExpressionStatement', expression }
|
|
104
118
|
}
|
|
105
119
|
|
|
120
|
+
export function EmptyStatement() {
|
|
121
|
+
return { type: 'EmptyStatement' }
|
|
122
|
+
}
|
|
123
|
+
|
|
106
124
|
export function ElementNode(tag, attributes, children) {
|
|
107
125
|
return { type: 'Element', tag, attributes, children }
|
|
108
126
|
}
|
|
@@ -119,12 +137,31 @@ export function InterpolationNode(expression) {
|
|
|
119
137
|
return { type: 'Interpolation', expression }
|
|
120
138
|
}
|
|
121
139
|
|
|
122
|
-
|
|
123
|
-
|
|
140
|
+
// elseChildren: null, or the nodes of the {{#else}} branch. An
|
|
141
|
+
// `{{#else if c}}` is represented as elseChildren = [IfNode(...)].
|
|
142
|
+
export function IfNode(condition, children, elseChildren = null) {
|
|
143
|
+
return { type: 'If', condition, children, elseChildren }
|
|
124
144
|
}
|
|
125
145
|
|
|
126
|
-
|
|
127
|
-
|
|
146
|
+
// itemName may be a plain name or destructuring source text
|
|
147
|
+
// ("[k, v]", "{ id, name }"). indexName is the optional second binding
|
|
148
|
+
// of `{{#each item, i in items}}`. elseChildren renders when the list
|
|
149
|
+
// is empty.
|
|
150
|
+
export function EachNode(
|
|
151
|
+
itemName,
|
|
152
|
+
iterable,
|
|
153
|
+
children,
|
|
154
|
+
indexName = null,
|
|
155
|
+
elseChildren = null,
|
|
156
|
+
) {
|
|
157
|
+
return {
|
|
158
|
+
type: 'Each',
|
|
159
|
+
itemName,
|
|
160
|
+
iterable,
|
|
161
|
+
children,
|
|
162
|
+
indexName,
|
|
163
|
+
elseChildren,
|
|
164
|
+
}
|
|
128
165
|
}
|
|
129
166
|
|
|
130
167
|
export function CallExpr(callee, args) {
|
|
@@ -135,12 +172,17 @@ export function AwaitExpr(argument) {
|
|
|
135
172
|
return { type: 'AwaitExpression', argument }
|
|
136
173
|
}
|
|
137
174
|
|
|
138
|
-
|
|
139
|
-
|
|
175
|
+
// `more` holds extra declarators of `const a = 1, b = 2`:
|
|
176
|
+
// [{ name, init }]. init may be null for `let x`.
|
|
177
|
+
export function VariableDeclaration(kind, name, init, more = []) {
|
|
178
|
+
return { type: 'VariableDeclaration', kind, name, init, more }
|
|
140
179
|
}
|
|
141
180
|
|
|
142
181
|
export function ObjectExpr(properties) {
|
|
143
|
-
// properties: [{ key, value }]
|
|
182
|
+
// properties: [{ key, value }] plain
|
|
183
|
+
// [{ key: null, keyExpr, value }] computed key
|
|
184
|
+
// [{ key, keyExpr, method: true, value: FunctionExpr }]
|
|
185
|
+
// [{ key: null, spread: true, value }]
|
|
144
186
|
return { type: 'ObjectExpression', properties }
|
|
145
187
|
}
|
|
146
188
|
|
|
@@ -164,8 +206,23 @@ export function TernaryExpr(condition, consequent, alternate) {
|
|
|
164
206
|
return { type: 'TernaryExpression', condition, consequent, alternate }
|
|
165
207
|
}
|
|
166
208
|
|
|
167
|
-
|
|
168
|
-
|
|
209
|
+
// varName: a plain name or a destructuring pattern node. declKind is
|
|
210
|
+
// 'const' (default) or 'let'. Iterates VALUES (for...of).
|
|
211
|
+
export function ForInStatement(varName, iterable, body, declKind = 'const') {
|
|
212
|
+
return { type: 'ForInStatement', varName, iterable, body, declKind }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// JS `for (const k in obj)` -- iterates KEYS.
|
|
216
|
+
export function ForKeysStatement(varName, object, body, declKind = 'const') {
|
|
217
|
+
return { type: 'ForKeysStatement', varName, object, body, declKind }
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// C-style `for (init; test; update) { }`
|
|
221
|
+
// init: VariableDeclaration | { type: 'ExpressionList', expressions } | null
|
|
222
|
+
// test: expression | null
|
|
223
|
+
// update: expression[]
|
|
224
|
+
export function ForStatement(init, test, update, body) {
|
|
225
|
+
return { type: 'ForStatement', init, test, update, body }
|
|
169
226
|
}
|
|
170
227
|
|
|
171
228
|
export function ForRangeStatement(varName, start, end, body) {
|
|
@@ -189,13 +246,33 @@ export function ArrowFunctionExpr(
|
|
|
189
246
|
body,
|
|
190
247
|
paramTypes = null,
|
|
191
248
|
isAsync = false,
|
|
249
|
+
paramDefaults = null,
|
|
192
250
|
) {
|
|
193
251
|
// body is either an expression node, or a BlockStatement node for
|
|
194
252
|
// block-bodied arrows (`x => { ... }`). Block bodies are only valid
|
|
195
253
|
// in script code; the template generators reject BlockStatement
|
|
196
254
|
// with their normal "unsupported node type" error.
|
|
197
255
|
// isAsync marks `async x => ...` / `async (a, b) => ...`.
|
|
198
|
-
|
|
256
|
+
// params are source strings ("x", "[k, v]", "{ id }", "...rest");
|
|
257
|
+
// paramDefaults is a parallel array of expression nodes or null.
|
|
258
|
+
return {
|
|
259
|
+
type: 'ArrowFunctionExpression',
|
|
260
|
+
params,
|
|
261
|
+
body,
|
|
262
|
+
paramTypes,
|
|
263
|
+
isAsync,
|
|
264
|
+
paramDefaults,
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// `function name(a, b = 1, ...rest) { ... }` used as an expression
|
|
269
|
+
// (also the value of object-literal method shorthand). Script only.
|
|
270
|
+
export function FunctionExpr(params, body, isAsync = false, name = null) {
|
|
271
|
+
return { type: 'FunctionExpression', params, body, isAsync, name }
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function FunctionDeclaration(name, params, body, isAsync = false) {
|
|
275
|
+
return { type: 'FunctionDeclaration', name, params, body, isAsync }
|
|
199
276
|
}
|
|
200
277
|
|
|
201
278
|
export function TemplateLiteralExpr(parts) {
|
|
@@ -228,6 +305,10 @@ export function WhileStatement(condition, body) {
|
|
|
228
305
|
return { type: 'WhileStatement', condition, body }
|
|
229
306
|
}
|
|
230
307
|
|
|
308
|
+
export function DoWhileStatement(body, condition) {
|
|
309
|
+
return { type: 'DoWhileStatement', body, condition }
|
|
310
|
+
}
|
|
311
|
+
|
|
231
312
|
// cases: [{ tests: Expression[] | null, body: Statement[] }]
|
|
232
313
|
// tests === null marks the `default` clause.
|
|
233
314
|
export function SwitchStatement(discriminant, cases) {
|
|
@@ -1,132 +1,209 @@
|
|
|
1
1
|
import { Identifier, MemberExpr } from './nodes.js'
|
|
2
2
|
|
|
3
|
-
function
|
|
3
|
+
function rewriteList(list, names) {
|
|
4
|
+
return list.map((s) => rewriteStatement(s, names))
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function rewriteParams(params, names) {
|
|
8
|
+
return params.map((p) =>
|
|
9
|
+
p.defaultValue
|
|
10
|
+
? { ...p, defaultValue: rewriteExpr(p.defaultValue, names) }
|
|
11
|
+
: p,
|
|
12
|
+
)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function rewriteExpr(node, names) {
|
|
4
16
|
if (node == null) return node
|
|
5
17
|
switch (node.type) {
|
|
6
18
|
case 'Identifier':
|
|
7
|
-
return
|
|
19
|
+
return names.has(node.name)
|
|
8
20
|
? MemberExpr(Identifier('this'), node.name)
|
|
9
21
|
: node
|
|
10
22
|
case 'MemberExpression':
|
|
11
23
|
return {
|
|
12
24
|
...node,
|
|
13
|
-
object: rewriteExpr(node.object,
|
|
25
|
+
object: rewriteExpr(node.object, names),
|
|
14
26
|
property: node.computed
|
|
15
|
-
? rewriteExpr(node.property,
|
|
27
|
+
? rewriteExpr(node.property, names)
|
|
16
28
|
: node.property,
|
|
17
29
|
}
|
|
18
30
|
case 'CallExpression':
|
|
31
|
+
case 'NewExpression':
|
|
19
32
|
return {
|
|
20
33
|
...node,
|
|
21
|
-
callee: rewriteExpr(node.callee,
|
|
22
|
-
args: node.args.map((a) => rewriteExpr(a,
|
|
34
|
+
callee: rewriteExpr(node.callee, names),
|
|
35
|
+
args: node.args.map((a) => rewriteExpr(a, names)),
|
|
23
36
|
}
|
|
24
37
|
case 'AssignmentExpression':
|
|
25
38
|
return {
|
|
26
39
|
...node,
|
|
27
|
-
target: rewriteExpr(node.target,
|
|
28
|
-
value: rewriteExpr(node.value,
|
|
40
|
+
target: rewriteExpr(node.target, names),
|
|
41
|
+
value: rewriteExpr(node.value, names),
|
|
29
42
|
}
|
|
30
43
|
case 'BinaryExpression':
|
|
44
|
+
case 'NullishCoalescingExpression':
|
|
31
45
|
return {
|
|
32
46
|
...node,
|
|
33
|
-
left: rewriteExpr(node.left,
|
|
34
|
-
right: rewriteExpr(node.right,
|
|
47
|
+
left: rewriteExpr(node.left, names),
|
|
48
|
+
right: rewriteExpr(node.right, names),
|
|
35
49
|
}
|
|
36
50
|
case 'UnaryExpression':
|
|
37
|
-
return { ...node, argument: rewriteExpr(node.argument, capabilityNames) }
|
|
38
51
|
case 'AwaitExpression':
|
|
39
|
-
|
|
52
|
+
case 'SpreadElement':
|
|
53
|
+
return { ...node, argument: rewriteExpr(node.argument, names) }
|
|
40
54
|
case 'TernaryExpression':
|
|
41
55
|
return {
|
|
42
56
|
...node,
|
|
43
|
-
condition: rewriteExpr(node.condition,
|
|
44
|
-
consequent: rewriteExpr(node.consequent,
|
|
45
|
-
alternate: rewriteExpr(node.alternate,
|
|
57
|
+
condition: rewriteExpr(node.condition, names),
|
|
58
|
+
consequent: rewriteExpr(node.consequent, names),
|
|
59
|
+
alternate: rewriteExpr(node.alternate, names),
|
|
46
60
|
}
|
|
47
|
-
case '
|
|
61
|
+
case 'ArrowFunctionExpression':
|
|
62
|
+
// body is an expression or a BlockStatement
|
|
48
63
|
return {
|
|
49
64
|
...node,
|
|
50
|
-
|
|
51
|
-
|
|
65
|
+
body: rewriteExpr(node.body, names),
|
|
66
|
+
paramDefaults: node.paramDefaults
|
|
67
|
+
? node.paramDefaults.map((d) => rewriteExpr(d, names))
|
|
68
|
+
: node.paramDefaults,
|
|
52
69
|
}
|
|
53
|
-
case '
|
|
54
|
-
return {
|
|
70
|
+
case 'FunctionExpression':
|
|
71
|
+
return {
|
|
72
|
+
...node,
|
|
73
|
+
params: rewriteParams(node.params, names),
|
|
74
|
+
body: rewriteList(node.body, names),
|
|
75
|
+
}
|
|
76
|
+
case 'BlockStatement':
|
|
77
|
+
return { ...node, body: rewriteList(node.body, names) }
|
|
55
78
|
case 'ArrayExpression':
|
|
56
79
|
return {
|
|
57
80
|
...node,
|
|
58
|
-
elements: node.elements.map((e) => rewriteExpr(e,
|
|
81
|
+
elements: node.elements.map((e) => rewriteExpr(e, names)),
|
|
59
82
|
}
|
|
60
83
|
case 'ObjectExpression':
|
|
61
84
|
return {
|
|
62
85
|
...node,
|
|
63
86
|
properties: node.properties.map((p) => ({
|
|
64
87
|
...p,
|
|
65
|
-
|
|
88
|
+
keyExpr: p.keyExpr ? rewriteExpr(p.keyExpr, names) : p.keyExpr,
|
|
89
|
+
value: rewriteExpr(p.value, names),
|
|
66
90
|
})),
|
|
67
91
|
}
|
|
92
|
+
case 'TemplateLiteralExpression':
|
|
93
|
+
return {
|
|
94
|
+
...node,
|
|
95
|
+
parts: node.parts.map((p) =>
|
|
96
|
+
p.type === 'expr'
|
|
97
|
+
? { ...p, expression: rewriteExpr(p.expression, names) }
|
|
98
|
+
: p,
|
|
99
|
+
),
|
|
100
|
+
}
|
|
68
101
|
default:
|
|
69
102
|
return node
|
|
70
103
|
}
|
|
71
104
|
}
|
|
72
105
|
|
|
73
|
-
function
|
|
106
|
+
function rewriteDeclaration(stmt, names) {
|
|
107
|
+
return {
|
|
108
|
+
...stmt,
|
|
109
|
+
init: rewriteExpr(stmt.init, names),
|
|
110
|
+
more: (stmt.more ?? []).map((m) => ({
|
|
111
|
+
...m,
|
|
112
|
+
init: rewriteExpr(m.init, names),
|
|
113
|
+
})),
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function rewriteStatement(stmt, names) {
|
|
74
118
|
switch (stmt.type) {
|
|
75
119
|
case 'ExpressionStatement':
|
|
120
|
+
return { ...stmt, expression: rewriteExpr(stmt.expression, names) }
|
|
121
|
+
case 'ReturnStatement':
|
|
122
|
+
case 'ThrowStatement':
|
|
123
|
+
return { ...stmt, argument: rewriteExpr(stmt.argument, names) }
|
|
124
|
+
case 'VariableDeclaration':
|
|
125
|
+
return rewriteDeclaration(stmt, names)
|
|
126
|
+
case 'FunctionDeclaration':
|
|
76
127
|
return {
|
|
77
128
|
...stmt,
|
|
78
|
-
|
|
129
|
+
params: rewriteParams(stmt.params, names),
|
|
130
|
+
body: rewriteList(stmt.body, names),
|
|
79
131
|
}
|
|
80
|
-
case 'ReturnStatement':
|
|
81
|
-
return { ...stmt, argument: rewriteExpr(stmt.argument, capabilityNames) }
|
|
82
|
-
case 'VariableDeclaration':
|
|
83
|
-
return { ...stmt, init: rewriteExpr(stmt.init, capabilityNames) }
|
|
84
132
|
case 'IfStatement':
|
|
85
133
|
return {
|
|
86
134
|
...stmt,
|
|
87
|
-
condition: rewriteExpr(stmt.condition,
|
|
88
|
-
consequent: stmt.consequent
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
135
|
+
condition: rewriteExpr(stmt.condition, names),
|
|
136
|
+
consequent: rewriteList(stmt.consequent, names),
|
|
137
|
+
alternate: stmt.alternate ? rewriteList(stmt.alternate, names) : null,
|
|
138
|
+
}
|
|
139
|
+
case 'WhileStatement':
|
|
140
|
+
case 'DoWhileStatement':
|
|
141
|
+
return {
|
|
142
|
+
...stmt,
|
|
143
|
+
condition: rewriteExpr(stmt.condition, names),
|
|
144
|
+
body: rewriteList(stmt.body, names),
|
|
145
|
+
}
|
|
146
|
+
case 'SwitchStatement':
|
|
147
|
+
return {
|
|
148
|
+
...stmt,
|
|
149
|
+
discriminant: rewriteExpr(stmt.discriminant, names),
|
|
150
|
+
cases: stmt.cases.map((c) => ({
|
|
151
|
+
tests: c.tests ? c.tests.map((t) => rewriteExpr(t, names)) : null,
|
|
152
|
+
body: rewriteList(c.body, names),
|
|
153
|
+
})),
|
|
94
154
|
}
|
|
95
155
|
case 'ForInStatement':
|
|
96
156
|
return {
|
|
97
157
|
...stmt,
|
|
98
|
-
iterable: rewriteExpr(stmt.iterable,
|
|
99
|
-
body: stmt.body
|
|
158
|
+
iterable: rewriteExpr(stmt.iterable, names),
|
|
159
|
+
body: rewriteList(stmt.body, names),
|
|
160
|
+
}
|
|
161
|
+
case 'ForKeysStatement':
|
|
162
|
+
return {
|
|
163
|
+
...stmt,
|
|
164
|
+
object: rewriteExpr(stmt.object, names),
|
|
165
|
+
body: rewriteList(stmt.body, names),
|
|
166
|
+
}
|
|
167
|
+
case 'ForStatement': {
|
|
168
|
+
let init = stmt.init
|
|
169
|
+
if (init && init.type === 'VariableDeclaration') {
|
|
170
|
+
init = rewriteDeclaration(init, names)
|
|
171
|
+
} else if (init && init.type === 'ExpressionList') {
|
|
172
|
+
init = {
|
|
173
|
+
...init,
|
|
174
|
+
expressions: init.expressions.map((e) => rewriteExpr(e, names)),
|
|
175
|
+
}
|
|
100
176
|
}
|
|
177
|
+
return {
|
|
178
|
+
...stmt,
|
|
179
|
+
init,
|
|
180
|
+
test: rewriteExpr(stmt.test, names),
|
|
181
|
+
update: (stmt.update ?? []).map((e) => rewriteExpr(e, names)),
|
|
182
|
+
body: rewriteList(stmt.body, names),
|
|
183
|
+
}
|
|
184
|
+
}
|
|
101
185
|
case 'ForRangeStatement':
|
|
102
186
|
return {
|
|
103
187
|
...stmt,
|
|
104
|
-
start: rewriteExpr(stmt.start,
|
|
105
|
-
end: rewriteExpr(stmt.end,
|
|
106
|
-
body: stmt.body
|
|
188
|
+
start: rewriteExpr(stmt.start, names),
|
|
189
|
+
end: rewriteExpr(stmt.end, names),
|
|
190
|
+
body: rewriteList(stmt.body, names),
|
|
107
191
|
}
|
|
108
192
|
case 'RepeatStatement':
|
|
109
193
|
return {
|
|
110
194
|
...stmt,
|
|
111
|
-
count: rewriteExpr(stmt.count,
|
|
112
|
-
body: stmt.body
|
|
195
|
+
count: rewriteExpr(stmt.count, names),
|
|
196
|
+
body: rewriteList(stmt.body, names),
|
|
113
197
|
}
|
|
114
198
|
case 'TryStatement':
|
|
115
199
|
return {
|
|
116
200
|
...stmt,
|
|
117
|
-
tryBlock: stmt.tryBlock
|
|
118
|
-
|
|
119
|
-
),
|
|
120
|
-
catchBlock: stmt.catchBlock
|
|
121
|
-
? stmt.catchBlock.map((s) => rewriteStatement(s, capabilityNames))
|
|
122
|
-
: null,
|
|
201
|
+
tryBlock: rewriteList(stmt.tryBlock, names),
|
|
202
|
+
catchBlock: stmt.catchBlock ? rewriteList(stmt.catchBlock, names) : null,
|
|
123
203
|
finallyBlock: stmt.finallyBlock
|
|
124
|
-
? stmt.finallyBlock
|
|
204
|
+
? rewriteList(stmt.finallyBlock, names)
|
|
125
205
|
: null,
|
|
126
206
|
}
|
|
127
|
-
case 'BreakStatement':
|
|
128
|
-
case 'ContinueStatement':
|
|
129
|
-
return stmt
|
|
130
207
|
default:
|
|
131
208
|
return stmt
|
|
132
209
|
}
|
|
@@ -144,19 +221,19 @@ export function substituteCapabilityRefs(pageNode) {
|
|
|
144
221
|
...pageNode,
|
|
145
222
|
computed: (pageNode.computed ?? []).map((c) => ({
|
|
146
223
|
...c,
|
|
147
|
-
|
|
224
|
+
params: rewriteParams(c.params ?? [], capabilityNames),
|
|
225
|
+
body: rewriteList(c.body, capabilityNames),
|
|
148
226
|
})),
|
|
149
227
|
actions: (pageNode.actions ?? []).map((a) => ({
|
|
150
228
|
...a,
|
|
151
|
-
|
|
229
|
+
params: rewriteParams(a.params ?? [], capabilityNames),
|
|
230
|
+
body: rewriteList(a.body, capabilityNames),
|
|
152
231
|
})),
|
|
153
232
|
onMount: pageNode.onMount
|
|
154
233
|
? {
|
|
155
234
|
...pageNode.onMount,
|
|
156
|
-
body: pageNode.onMount.body
|
|
157
|
-
rewriteStatement(s, capabilityNames),
|
|
158
|
-
),
|
|
235
|
+
body: rewriteList(pageNode.onMount.body, capabilityNames),
|
|
159
236
|
}
|
|
160
237
|
: null,
|
|
161
238
|
}
|
|
162
|
-
}
|
|
239
|
+
}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import { generateExpression } from './generateExpression.js'
|
|
1
|
+
import { generateExpression, generateParams } from './generateExpression.js'
|
|
2
|
+
import {
|
|
3
|
+
declarationSource,
|
|
4
|
+
forHeaderSource,
|
|
5
|
+
generateDeclarationTarget,
|
|
6
|
+
} from './generateStatement.js'
|
|
2
7
|
|
|
3
8
|
// Structurally the same cases as generateStatement.js, but emits one
|
|
4
9
|
// line at a time through `sink.pushLine(text, originalLine)` instead
|
|
@@ -11,8 +16,8 @@ import { generateExpression } from './generateExpression.js'
|
|
|
11
16
|
// Every generateExpression() call here passes `true` for its typed
|
|
12
17
|
// param -- this file has exactly one consumer (buildVirtualPageTs,
|
|
13
18
|
// building the virtual .ts fed to the TypeScript compiler API), so
|
|
14
|
-
//
|
|
15
|
-
//
|
|
19
|
+
// there's no "untyped" mode to preserve here. Binding-target and
|
|
20
|
+
// declaration rendering is shared with generateStatement.js.
|
|
16
21
|
//
|
|
17
22
|
// Block-bodied arrows never reach this file directly: they are
|
|
18
23
|
// emitted by generateExpression, which delegates their statements to
|
|
@@ -26,6 +31,10 @@ export function emitStatementLines(node, sink, indent = ' ') {
|
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
switch (node.type) {
|
|
34
|
+
case 'EmptyStatement':
|
|
35
|
+
push(';')
|
|
36
|
+
break
|
|
37
|
+
|
|
29
38
|
case 'ReturnStatement':
|
|
30
39
|
push(node.argument ? `return ${expr(node.argument)};` : 'return;')
|
|
31
40
|
break
|
|
@@ -39,9 +48,15 @@ export function emitStatementLines(node, sink, indent = ' ') {
|
|
|
39
48
|
break
|
|
40
49
|
|
|
41
50
|
case 'VariableDeclaration':
|
|
51
|
+
push(`${declarationSource(node, true)};`)
|
|
52
|
+
break
|
|
53
|
+
|
|
54
|
+
case 'FunctionDeclaration':
|
|
42
55
|
push(
|
|
43
|
-
`${node.
|
|
56
|
+
`${node.isAsync ? 'async ' : ''}function ${node.name}(${generateParams(node.params, true)}) {`,
|
|
44
57
|
)
|
|
58
|
+
nested(node.body)
|
|
59
|
+
push('}')
|
|
45
60
|
break
|
|
46
61
|
|
|
47
62
|
case 'IfStatement':
|
|
@@ -60,6 +75,12 @@ export function emitStatementLines(node, sink, indent = ' ') {
|
|
|
60
75
|
push('}')
|
|
61
76
|
break
|
|
62
77
|
|
|
78
|
+
case 'DoWhileStatement':
|
|
79
|
+
push('do {')
|
|
80
|
+
nested(node.body)
|
|
81
|
+
push(`} while (${expr(node.condition)});`)
|
|
82
|
+
break
|
|
83
|
+
|
|
63
84
|
// Mirrors generateStatement: every clause body sits in its own
|
|
64
85
|
// braces and ends with an automatic `break`, so the virtual file
|
|
65
86
|
// has the same scoping and no-fallthrough behavior as real output.
|
|
@@ -82,7 +103,23 @@ export function emitStatementLines(node, sink, indent = ' ') {
|
|
|
82
103
|
break
|
|
83
104
|
|
|
84
105
|
case 'ForInStatement':
|
|
85
|
-
push(
|
|
106
|
+
push(
|
|
107
|
+
`for (${node.declKind ?? 'const'} ${generateDeclarationTarget(node.varName, true)} of ${expr(node.iterable)}) {`,
|
|
108
|
+
)
|
|
109
|
+
nested(node.body)
|
|
110
|
+
push('}')
|
|
111
|
+
break
|
|
112
|
+
|
|
113
|
+
case 'ForKeysStatement':
|
|
114
|
+
push(
|
|
115
|
+
`for (${node.declKind ?? 'const'} ${generateDeclarationTarget(node.varName, true)} in ${expr(node.object)}) {`,
|
|
116
|
+
)
|
|
117
|
+
nested(node.body)
|
|
118
|
+
push('}')
|
|
119
|
+
break
|
|
120
|
+
|
|
121
|
+
case 'ForStatement':
|
|
122
|
+
push(`for (${forHeaderSource(node, true)}) {`)
|
|
86
123
|
nested(node.body)
|
|
87
124
|
push('}')
|
|
88
125
|
break
|
|
@@ -126,47 +163,4 @@ export function emitStatementLines(node, sink, indent = ' ') {
|
|
|
126
163
|
default:
|
|
127
164
|
throw new Error(`emitStatementLines: unknown node type "${node.type}"`)
|
|
128
165
|
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Same binding-target rendering as generateStatement.js (kept as a
|
|
132
|
-
// local copy so that file's exports stay untouched): a plain
|
|
133
|
-
// identifier, or a nested ObjectPattern/ArrayPattern with renames,
|
|
134
|
-
// defaults and rest elements. If you change one, change both.
|
|
135
|
-
function generateDeclarationTarget(target) {
|
|
136
|
-
if (typeof target === 'string') {
|
|
137
|
-
return target
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (target.type === 'ObjectPattern') {
|
|
141
|
-
const parts = target.properties.map((p) => {
|
|
142
|
-
const bindingStr = generateDeclarationTarget(p.binding)
|
|
143
|
-
const needsColon = typeof p.binding !== 'string' || p.binding !== p.key
|
|
144
|
-
const lhs = needsColon ? `${p.key}: ${bindingStr}` : p.key
|
|
145
|
-
return p.defaultValue
|
|
146
|
-
? `${lhs} = ${generateExpression(p.defaultValue, true)}`
|
|
147
|
-
: lhs
|
|
148
|
-
})
|
|
149
|
-
if (target.rest) {
|
|
150
|
-
parts.push(`...${target.rest}`)
|
|
151
|
-
}
|
|
152
|
-
return `{ ${parts.join(', ')} }`
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
if (target.type === 'ArrayPattern') {
|
|
156
|
-
const parts = target.elements.map((el) => {
|
|
157
|
-
if (el === null) return ''
|
|
158
|
-
const bindingStr = generateDeclarationTarget(el.binding)
|
|
159
|
-
return el.defaultValue
|
|
160
|
-
? `${bindingStr} = ${generateExpression(el.defaultValue, true)}`
|
|
161
|
-
: bindingStr
|
|
162
|
-
})
|
|
163
|
-
if (target.rest) {
|
|
164
|
-
parts.push(`...${target.rest}`)
|
|
165
|
-
}
|
|
166
|
-
return `[${parts.join(', ')}]`
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
throw new Error(
|
|
170
|
-
`generateDeclarationTarget: unknown pattern type "${target.type}"`,
|
|
171
|
-
)
|
|
172
166
|
}
|