@tylix/compiler 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +30 -2
  3. package/src/ast/nodes.js +145 -23
  4. package/src/ast/substituteCapabilityRefs.js +52 -5
  5. package/src/codegen/emitStatementLines.js +60 -10
  6. package/src/codegen/generateExpression.js +294 -35
  7. package/src/codegen/generateStatement.js +125 -20
  8. package/src/codegen/generateTemplate.js +107 -96
  9. package/src/codegen/generateTemplateExpression.js +63 -10
  10. package/src/codegen/jsGlobals.js +62 -2
  11. package/src/compileComponent.js +37 -6
  12. package/src/lexer/Lexer.js +128 -28
  13. package/src/parser/Parser.js +1840 -500
  14. package/src/parser/parseExpressionString.js +5 -0
  15. package/src/parser/parseTemplate.js +153 -41
  16. package/src/renderPageDocument.js +71 -8
  17. package/src/runtime/capability.js +8 -1
  18. package/src/runtime/reactive.js +289 -61
  19. package/src/runtime/useApi.js +74 -13
  20. package/src/runtime/useChannel.js +27 -6
  21. package/src/typecheck/buildVirtualTs.js +11 -1
  22. package/src/typecheck/tylix-globals.d.ts +23 -4
  23. package/src/typecheck/typedTemplateExpr.js +90 -27
  24. package/src/codegen/generateExpression.array.test.js +0 -13
  25. package/src/codegen/generateExpression.callexpr.test.js +0 -19
  26. package/src/codegen/generateExpression.test.js +0 -59
  27. package/src/codegen/generateMethod.asyncawait.test.js +0 -37
  28. package/src/codegen/generateMethod.ifstatement.test.js +0 -20
  29. package/src/codegen/generateMethod.test.js +0 -81
  30. package/src/codegen/generatePage.onmount.test.js +0 -46
  31. package/src/codegen/generatePage.test.js +0 -138
  32. package/src/codegen/generateStatement.test.js +0 -31
  33. package/src/codegen/generateTemplate.ifeach.test.js +0 -138
  34. package/src/codegen/generateTemplate.test.js +0 -149
  35. package/src/codegen/generateTemplateExpression.test.js +0 -34
  36. package/src/compileComponent.test.js +0 -121
  37. package/src/lexer/Lexer.test.js +0 -151
  38. package/src/parser/Parser.array.test.js +0 -37
  39. package/src/parser/Parser.asyncawait.test.js +0 -33
  40. package/src/parser/Parser.ifstatement.test.js +0 -20
  41. package/src/parser/Parser.onmount.test.js +0 -18
  42. package/src/parser/Parser.test.js +0 -315
  43. package/src/parser/parseComponent.test.js +0 -53
  44. package/src/parser/parsePageFile.test.js +0 -101
  45. package/src/parser/parseTemplate.ifeach.test.js +0 -33
  46. package/src/parser/parseTemplate.pre.test.js +0 -39
  47. package/src/parser/parseTemplate.test.js +0 -112
  48. package/src/renderPageDocument.test.js +0 -125
  49. package/src/runtime/reactive.test.js +0 -106
  50. package/test/dashboard-syntax.test.js +0 -116
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mbiyrukzz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json CHANGED
@@ -1,13 +1,41 @@
1
1
  {
2
2
  "name": "@tylix/compiler",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
+ "description": "Compiler for Tylix .tyx pages and components: parses, type-checks and compiles them to server-rendered HTML and client JavaScript.",
5
+ "license": "MIT",
6
+ "author": "Mbiyrukzz",
4
7
  "type": "module",
5
8
  "keywords": [
6
9
  "tylix",
7
10
  "compiler",
8
- "ui"
11
+ "ui",
12
+ "templates",
13
+ "ssr",
14
+ "components"
9
15
  ],
16
+ "homepage": "https://github.com/Mbiyrukzz/tylix/tree/main/packages/compiler#readme",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/Mbiyrukzz/tylix.git",
20
+ "directory": "packages/compiler"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/Mbiyrukzz/tylix/issues"
24
+ },
25
+ "engines": {
26
+ "node": ">=22.13"
27
+ },
10
28
  "main": "./src/index.js",
29
+ "files": [
30
+ "src",
31
+ "!**/*.test.js",
32
+ "!**/*.test.mjs",
33
+ "README.md",
34
+ "LICENSE"
35
+ ],
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
11
39
  "scripts": {
12
40
  "test": "node --test src/*.test.js src/**/*.test.js"
13
41
  },
package/src/ast/nodes.js CHANGED
@@ -16,6 +16,7 @@ export function PageNode({
16
16
  meta = [],
17
17
  computed = [],
18
18
  actions = [],
19
+ helpers = [],
19
20
  onMount = null,
20
21
  }) {
21
22
  return {
@@ -32,6 +33,7 @@ export function PageNode({
32
33
  meta,
33
34
  computed,
34
35
  actions,
36
+ helpers,
35
37
  onMount,
36
38
  }
37
39
  }
@@ -59,17 +61,19 @@ export function MethodNode(name, params, body, returnType = null) {
59
61
  }
60
62
 
61
63
  // A function/method parameter:
62
- // name plain name, or destructuring source text ("{ a, b }")
64
+ // name plain name, or destructuring source text ("{ a, b }")
63
65
  // typeAnnotation TS type text or null
64
- // defaultValue expression node or null
65
- // rest true for `...name`
66
+ // defaultValue expression node or null
67
+ // rest true for `...name`
68
+ // optional true for `name?: Type`
66
69
  export function Param(
67
70
  name,
68
71
  typeAnnotation = null,
69
72
  defaultValue = null,
70
73
  rest = false,
74
+ optional = false,
71
75
  ) {
72
- return { name, typeAnnotation, defaultValue, rest }
76
+ return { name, typeAnnotation, defaultValue, rest, optional }
73
77
  }
74
78
 
75
79
  export function AssignmentExpr(target, value) {
@@ -173,19 +177,29 @@ export function AwaitExpr(argument) {
173
177
  }
174
178
 
175
179
  // `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 }
180
+ // [{ name, init, typeAnnotation }]. init may be null for `let x`.
181
+ // typeAnnotation (TS type text or null) belongs to the FIRST declarator;
182
+ // extra declarators carry their own in `more[i].typeAnnotation`.
183
+ export function VariableDeclaration(
184
+ kind,
185
+ name,
186
+ init,
187
+ more = [],
188
+ typeAnnotation = null,
189
+ ) {
190
+ return { type: 'VariableDeclaration', kind, name, init, more, typeAnnotation }
179
191
  }
180
192
 
181
193
  export function ObjectExpr(properties) {
182
194
  // properties: [{ key, value }] plain
183
195
  // [{ key: null, keyExpr, value }] computed key
184
- // [{ key, keyExpr, method: true, value: FunctionExpr }]
196
+ // [{ key, keyExpr, method: true, kind: 'method' | 'get' | 'set',
197
+ // value: FunctionExpr }] method shorthand / accessors
185
198
  // [{ key: null, spread: true, value }]
186
199
  return { type: 'ObjectExpression', properties }
187
200
  }
188
201
 
202
+ // elements may contain `null` for array holes: [1, , 3]
189
203
  export function ArrayExpr(elements) {
190
204
  return { type: 'ArrayExpression', elements }
191
205
  }
@@ -198,6 +212,12 @@ export function UnaryExpr(operator, argument) {
198
212
  return { type: 'UnaryExpression', operator, argument }
199
213
  }
200
214
 
215
+ // `++x` / `x++` / `--x` / `x--`. Kept as its own node (not desugared to
216
+ // `x = x + 1`) so a postfix update evaluates to the old value.
217
+ export function UpdateExpr(operator, argument, prefix) {
218
+ return { type: 'UpdateExpression', operator, argument, prefix }
219
+ }
220
+
201
221
  export function NullishCoalescingExpr(left, right) {
202
222
  return { type: 'NullishCoalescingExpression', left, right }
203
223
  }
@@ -207,9 +227,16 @@ export function TernaryExpr(condition, consequent, alternate) {
207
227
  }
208
228
 
209
229
  // 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 }
230
+ // 'const' (default) or 'let'. Iterates VALUES (for...of). isAwait
231
+ // marks `for await (... of ...)`.
232
+ export function ForInStatement(
233
+ varName,
234
+ iterable,
235
+ body,
236
+ declKind = 'const',
237
+ isAwait = false,
238
+ ) {
239
+ return { type: 'ForInStatement', varName, iterable, body, declKind, isAwait }
213
240
  }
214
241
 
215
242
  // JS `for (const k in obj)` -- iterates KEYS.
@@ -233,12 +260,22 @@ export function RepeatStatement(count, body) {
233
260
  return { type: 'RepeatStatement', count, body }
234
261
  }
235
262
 
236
- export function BreakStatement() {
237
- return { type: 'BreakStatement' }
263
+ // label is the target of `break outer` / `continue outer`, or null.
264
+ export function BreakStatement(label = null) {
265
+ return { type: 'BreakStatement', label }
238
266
  }
239
267
 
240
- export function ContinueStatement() {
241
- return { type: 'ContinueStatement' }
268
+ export function ContinueStatement(label = null) {
269
+ return { type: 'ContinueStatement', label }
270
+ }
271
+
272
+ // `outer: for (...) { ... }`
273
+ export function LabeledStatement(label, body) {
274
+ return { type: 'LabeledStatement', label, body }
275
+ }
276
+
277
+ export function DebuggerStatement() {
278
+ return { type: 'DebuggerStatement' }
242
279
  }
243
280
 
244
281
  export function ArrowFunctionExpr(
@@ -247,14 +284,18 @@ export function ArrowFunctionExpr(
247
284
  paramTypes = null,
248
285
  isAsync = false,
249
286
  paramDefaults = null,
287
+ paramOptional = null,
288
+ returnType = null,
250
289
  ) {
251
290
  // body is either an expression node, or a BlockStatement node for
252
291
  // block-bodied arrows (`x => { ... }`). Block bodies are only valid
253
292
  // in script code; the template generators reject BlockStatement
254
- // with their normal "unsupported node type" error.
293
+ // with a clear error.
255
294
  // isAsync marks `async x => ...` / `async (a, b) => ...`.
256
295
  // params are source strings ("x", "[k, v]", "{ id }", "...rest");
257
- // paramDefaults is a parallel array of expression nodes or null.
296
+ // paramDefaults is a parallel array of expression nodes or null;
297
+ // paramOptional is a parallel array of booleans (`b?: string`);
298
+ // returnType is TS type text or null (`(x): number => ...`).
258
299
  return {
259
300
  type: 'ArrowFunctionExpression',
260
301
  params,
@@ -262,32 +303,102 @@ export function ArrowFunctionExpr(
262
303
  paramTypes,
263
304
  isAsync,
264
305
  paramDefaults,
306
+ paramOptional,
307
+ returnType,
265
308
  }
266
309
  }
267
310
 
268
311
  // `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 }
312
+ // (also the value of object-literal methods and accessors).
313
+ // isGenerator marks `function*`. Script only.
314
+ export function FunctionExpr(
315
+ params,
316
+ body,
317
+ isAsync = false,
318
+ name = null,
319
+ isGenerator = false,
320
+ ) {
321
+ return { type: 'FunctionExpression', params, body, isAsync, name, isGenerator }
272
322
  }
273
323
 
274
- export function FunctionDeclaration(name, params, body, isAsync = false) {
275
- return { type: 'FunctionDeclaration', name, params, body, isAsync }
324
+ export function FunctionDeclaration(
325
+ name,
326
+ params,
327
+ body,
328
+ isAsync = false,
329
+ isGenerator = false,
330
+ ) {
331
+ return {
332
+ type: 'FunctionDeclaration',
333
+ name,
334
+ params,
335
+ body,
336
+ isAsync,
337
+ isGenerator,
338
+ }
276
339
  }
277
340
 
278
341
  export function TemplateLiteralExpr(parts) {
279
342
  return { type: 'TemplateLiteralExpression', parts }
280
343
  }
281
344
 
345
+ // tag`text ${x}` -- quasi is a TemplateLiteralExpression whose text
346
+ // parts carry `raw` (the text as written) for String.raw.
347
+ export function TaggedTemplateExpr(tag, quasi) {
348
+ return { type: 'TaggedTemplateExpression', tag, quasi }
349
+ }
350
+
282
351
  export function SpreadElement(argument) {
283
352
  return { type: 'SpreadElement', argument }
284
353
  }
285
354
 
286
- export function TryStatement(tryBlock, catchParam, catchBlock, finallyBlock) {
355
+ // `(a, b, c)` -- the comma operator.
356
+ export function SequenceExpr(expressions) {
357
+ return { type: 'SequenceExpression', expressions }
358
+ }
359
+
360
+ // `yield x` / `yield* x` / bare `yield` (argument null).
361
+ export function YieldExpr(argument, delegate = false) {
362
+ return { type: 'YieldExpression', argument, delegate }
363
+ }
364
+
365
+ // `expr as Type` / `expr satisfies Type` -- the cast exists only in
366
+ // the virtual .ts file; real JS output is just the inner expression.
367
+ export function AsExpr(expression, typeAnnotation, keyword = 'as') {
368
+ return { type: 'AsExpression', expression, typeAnnotation, keyword }
369
+ }
370
+
371
+ // `expr!` -- non-null assertion, same deal: .ts only.
372
+ export function NonNullExpr(expression) {
373
+ return { type: 'NonNullExpression', expression }
374
+ }
375
+
376
+ // members: see Parser.parseClassMember. Each is
377
+ // { kind: 'constructor' | 'method' | 'get' | 'set' | 'field' | 'staticBlock',
378
+ // static, key, keyExpr, keyKind: 'name' | 'string' | 'number' | 'computed',
379
+ // isAsync, isGenerator, params, body, returnType, typeAnnotation, value }
380
+ export function ClassDeclaration(name, superClass, members) {
381
+ return { type: 'ClassDeclaration', name, superClass, members }
382
+ }
383
+
384
+ export function ClassExpr(name, superClass, members) {
385
+ return { type: 'ClassExpression', name, superClass, members }
386
+ }
387
+
388
+ // catchParam: null, a plain name, or a destructuring pattern node.
389
+ // catchType: optional `any` / `unknown` written after a plain name.
390
+ export function TryStatement(
391
+ tryBlock,
392
+ catchParam,
393
+ catchBlock,
394
+ finallyBlock,
395
+ catchType = null,
396
+ ) {
287
397
  return {
288
398
  type: 'TryStatement',
289
399
  tryBlock,
290
400
  catchParam,
401
+ catchType,
291
402
  catchBlock,
292
403
  finallyBlock,
293
404
  }
@@ -313,4 +424,15 @@ export function DoWhileStatement(body, condition) {
313
424
  // tests === null marks the `default` clause.
314
425
  export function SwitchStatement(discriminant, cases) {
315
426
  return { type: 'SwitchStatement', discriminant, cases }
427
+ }
428
+
429
+ // `type X = ...` / `interface X {...}`. `source` is the full TS text;
430
+ // it only appears in the virtual .ts file, never in real JS output.
431
+ export function TypeDeclaration(kind, name, source) {
432
+ return { type: 'TypeDeclaration', kind, name, source }
433
+ }
434
+
435
+ // members: [{ name, init: expression | null }]
436
+ export function EnumDeclaration(name, members) {
437
+ return { type: 'EnumDeclaration', name, members }
316
438
  }
@@ -1,4 +1,13 @@
1
- import { Identifier, MemberExpr } from './nodes.js'
1
+ import { Identifier, CallExpr, Literal } from './nodes.js'
2
+
3
+ // A bare capability name (`Auth`) used to become `this.Auth`, which is
4
+ // wrong inside `function () {}` bodies, object-method shorthand and
5
+ // class methods, where `this` is not the page. resolveCapability("Auth")
6
+ // is a global of the inline script and returns the same memoized
7
+ // singleton, so it works in every kind of function.
8
+ function capabilityRef(name) {
9
+ return CallExpr(Identifier('resolveCapability'), [Literal(name)])
10
+ }
2
11
 
3
12
  function rewriteList(list, names) {
4
13
  return list.map((s) => rewriteStatement(s, names))
@@ -12,13 +21,25 @@ function rewriteParams(params, names) {
12
21
  )
13
22
  }
14
23
 
24
+ function rewriteClass(node, names) {
25
+ return {
26
+ ...node,
27
+ superClass: rewriteExpr(node.superClass, names),
28
+ members: node.members.map((m) => ({
29
+ ...m,
30
+ keyExpr: m.keyExpr ? rewriteExpr(m.keyExpr, names) : m.keyExpr,
31
+ params: m.params ? rewriteParams(m.params, names) : m.params,
32
+ body: m.body ? rewriteList(m.body, names) : m.body,
33
+ value: m.value ? rewriteExpr(m.value, names) : m.value,
34
+ })),
35
+ }
36
+ }
37
+
15
38
  function rewriteExpr(node, names) {
16
39
  if (node == null) return node
17
40
  switch (node.type) {
18
41
  case 'Identifier':
19
- return names.has(node.name)
20
- ? MemberExpr(Identifier('this'), node.name)
21
- : node
42
+ return names.has(node.name) ? capabilityRef(node.name) : node
22
43
  case 'MemberExpression':
23
44
  return {
24
45
  ...node,
@@ -48,9 +69,27 @@ function rewriteExpr(node, names) {
48
69
  right: rewriteExpr(node.right, names),
49
70
  }
50
71
  case 'UnaryExpression':
72
+ case 'UpdateExpression':
51
73
  case 'AwaitExpression':
52
74
  case 'SpreadElement':
75
+ case 'YieldExpression':
53
76
  return { ...node, argument: rewriteExpr(node.argument, names) }
77
+ case 'AsExpression':
78
+ case 'NonNullExpression':
79
+ return { ...node, expression: rewriteExpr(node.expression, names) }
80
+ case 'SequenceExpression':
81
+ return {
82
+ ...node,
83
+ expressions: node.expressions.map((e) => rewriteExpr(e, names)),
84
+ }
85
+ case 'TaggedTemplateExpression':
86
+ return {
87
+ ...node,
88
+ tag: rewriteExpr(node.tag, names),
89
+ quasi: rewriteExpr(node.quasi, names),
90
+ }
91
+ case 'ClassExpression':
92
+ return rewriteClass(node, names)
54
93
  case 'TernaryExpression':
55
94
  return {
56
95
  ...node,
@@ -123,11 +162,17 @@ function rewriteStatement(stmt, names) {
123
162
  return { ...stmt, argument: rewriteExpr(stmt.argument, names) }
124
163
  case 'VariableDeclaration':
125
164
  return rewriteDeclaration(stmt, names)
165
+ case 'ClassDeclaration':
166
+ return rewriteClass(stmt, names)
167
+ case 'BlockStatement':
168
+ return { ...stmt, body: rewriteList(stmt.body, names) }
169
+ case 'LabeledStatement':
170
+ return { ...stmt, body: rewriteStatement(stmt.body, names) }
126
171
  case 'FunctionDeclaration':
127
172
  return {
128
173
  ...stmt,
129
174
  params: rewriteParams(stmt.params, names),
130
- body: rewriteList(stmt.body, names),
175
+ body: stmt.body ? rewriteList(stmt.body, names) : stmt.body,
131
176
  }
132
177
  case 'IfStatement':
133
178
  return {
@@ -221,12 +266,14 @@ export function substituteCapabilityRefs(pageNode) {
221
266
  ...pageNode,
222
267
  computed: (pageNode.computed ?? []).map((c) => ({
223
268
  ...c,
269
+ helpers: rewriteList(pageNode.helpers ?? [], capabilityNames),
224
270
  params: rewriteParams(c.params ?? [], capabilityNames),
225
271
  body: rewriteList(c.body, capabilityNames),
226
272
  })),
227
273
  actions: (pageNode.actions ?? []).map((a) => ({
228
274
  ...a,
229
275
  params: rewriteParams(a.params ?? [], capabilityNames),
276
+ helpers: rewriteList(pageNode.helpers ?? [], capabilityNames),
230
277
  body: rewriteList(a.body, capabilityNames),
231
278
  })),
232
279
  onMount: pageNode.onMount
@@ -1,10 +1,12 @@
1
- import { generateExpression, generateParams } from './generateExpression.js'
2
1
  import {
3
2
  declarationSource,
4
3
  forHeaderSource,
5
4
  generateDeclarationTarget,
5
+ generateStatement,
6
6
  } from './generateStatement.js'
7
7
 
8
+ import { generateExpression, generateParams, functionSignature } from './generateExpression.js'
9
+
8
10
  // Structurally the same cases as generateStatement.js, but emits one
9
11
  // line at a time through `sink.pushLine(text, originalLine)` instead
10
12
  // of returning a single joined string -- needed so the virtual .ts
@@ -21,7 +23,8 @@ import {
21
23
  //
22
24
  // Block-bodied arrows never reach this file directly: they are
23
25
  // emitted by generateExpression, which delegates their statements to
24
- // generateStatement (typed), so there is no BlockStatement case here.
26
+ // generateStatement (typed). A standalone `{ ... }` block statement
27
+ // does reach it and is handled below.
25
28
  export function emitStatementLines(node, sink, indent = ' ') {
26
29
  const line = node.line
27
30
  const push = (text) => sink.pushLine(`${indent}${text}`, line)
@@ -35,6 +38,22 @@ export function emitStatementLines(node, sink, indent = ' ') {
35
38
  push(';')
36
39
  break
37
40
 
41
+ case 'DebuggerStatement':
42
+ push('debugger;')
43
+ break
44
+
45
+ case 'BlockStatement':
46
+ push('{')
47
+ nested(node.body)
48
+ push('}')
49
+ break
50
+
51
+ // The label sits on its own line, directly above its statement.
52
+ case 'LabeledStatement':
53
+ push(`${node.label}:`)
54
+ emitStatementLines(node.body, sink, indent)
55
+ break
56
+
38
57
  case 'ReturnStatement':
39
58
  push(node.argument ? `return ${expr(node.argument)};` : 'return;')
40
59
  break
@@ -51,13 +70,33 @@ export function emitStatementLines(node, sink, indent = ' ') {
51
70
  push(`${declarationSource(node, true)};`)
52
71
  break
53
72
 
54
- case 'FunctionDeclaration':
55
- push(
56
- `${node.isAsync ? 'async ' : ''}function ${node.name}(${generateParams(node.params, true)}) {`,
57
- )
73
+ // A class is rendered by generateStatement as one multi-line
74
+ // string; split it so every generated line gets its own lineMap
75
+ // entry (embedded newlines would shift all later line numbers).
76
+ case 'ClassDeclaration':
77
+ for (const text of generateStatement(node, true).split('\n')) push(text)
78
+ break
79
+
80
+ case 'TypeDeclaration':
81
+ push(node.source)
82
+ break
83
+
84
+ case 'EnumDeclaration':
85
+ push(generateStatement(node, true))
86
+ break
87
+
88
+ case 'FunctionDeclaration': {
89
+ const { generics, ret } = functionSignature(node, true)
90
+ const head = `${node.isAsync ? 'async ' : ''}function${node.isGenerator ? '*' : ''} ${node.name}${generics}(${generateParams(node.params, true)})${ret}`
91
+ if (node.body == null) {
92
+ push(`${head};`) // overload signature
93
+ break
94
+ }
95
+ push(`${head} {`)
58
96
  nested(node.body)
59
97
  push('}')
60
98
  break
99
+ }
61
100
 
62
101
  case 'IfStatement':
63
102
  push(`if (${expr(node.condition)}) {`)
@@ -104,7 +143,7 @@ export function emitStatementLines(node, sink, indent = ' ') {
104
143
 
105
144
  case 'ForInStatement':
106
145
  push(
107
- `for (${node.declKind ?? 'const'} ${generateDeclarationTarget(node.varName, true)} of ${expr(node.iterable)}) {`,
146
+ `for ${node.isAwait ? 'await ' : ''}(${node.declKind ?? 'const'} ${generateDeclarationTarget(node.varName, true)} of ${expr(node.iterable)}) {`,
108
147
  )
109
148
  nested(node.body)
110
149
  push('}')
@@ -139,18 +178,29 @@ export function emitStatementLines(node, sink, indent = ' ') {
139
178
  break
140
179
 
141
180
  case 'BreakStatement':
142
- push('break;')
181
+ push(node.label ? `break ${node.label};` : 'break;')
143
182
  break
144
183
 
145
184
  case 'ContinueStatement':
146
- push('continue;')
185
+ push(node.label ? `continue ${node.label};` : 'continue;')
147
186
  break
148
187
 
149
188
  case 'TryStatement':
150
189
  push('try {')
151
190
  nested(node.tryBlock)
152
191
  if (node.catchBlock) {
153
- push(node.catchParam ? `} catch (${node.catchParam}) {` : '} catch {')
192
+ if (node.catchParam == null) {
193
+ push('} catch {')
194
+ } else if (typeof node.catchParam === 'string') {
195
+ // JS gives the catch variable `any` semantics; TypeScript's
196
+ // default under strict is `unknown`.
197
+ push(`} catch (${node.catchParam}: ${node.catchType ?? 'any'}) {`)
198
+ } else {
199
+ push('} catch (__caught: any) {')
200
+ push(
201
+ ` const ${generateDeclarationTarget(node.catchParam, true)} = __caught;`,
202
+ )
203
+ }
154
204
  nested(node.catchBlock)
155
205
  }
156
206
  if (node.finallyBlock) {