driftjs-compiler 0.0.13 → 0.0.15

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/dist/index-es.js CHANGED
@@ -1,8 +1,8 @@
1
- import { VOID_ELEMENTS as e, findTopLevelChar as t, hasMatchingOuterParens as n, scanBalancedDelimiters as r } from "driftjs-shared";
2
- import * as i from "acorn";
3
- import * as a from "acorn-walk";
1
+ import { VOID_ELEMENTS as e } from "driftjs-shared";
2
+ import * as t from "acorn";
3
+ import * as n from "acorn-walk";
4
4
  //#region types/token.ts
5
- var o = {
5
+ var r = {
6
6
  TagOpen: "TagOpen",
7
7
  TagOpenSlash: "TagOpenSlash",
8
8
  TagClose: "TagClose",
@@ -20,10 +20,13 @@ var o = {
20
20
  DirectiveSwitch: "DirectiveSwitch",
21
21
  DirectiveCase: "DirectiveCase",
22
22
  DirectiveDefault: "DirectiveDefault",
23
+ DirectiveAsync: "DirectiveAsync",
24
+ DirectiveFallback: "DirectiveFallback",
25
+ DirectiveCatch: "DirectiveCatch",
23
26
  BlockOpen: "BlockOpen",
24
27
  BlockClose: "BlockClose",
25
28
  EOF: "EOF"
26
- }, s = {
29
+ }, i = {
27
30
  Data: "Data",
28
31
  TagOpen: "TagOpen",
29
32
  EndTagOpen: "EndTagOpen",
@@ -37,9 +40,9 @@ var o = {
37
40
  Interpolation: "Interpolation",
38
41
  RawText: "RawText",
39
42
  EOF: "EOF"
40
- }, c = /* @__PURE__ */ function(e) {
43
+ }, a = /* @__PURE__ */ function(e) {
41
44
  return e[e.Start = 0] = "Start", e[e.Punctuator = 1] = "Punctuator", e[e.Keyword = 2] = "Keyword", e[e.IdentifierOrLiteral = 3] = "IdentifierOrLiteral", e[e.PostfixOp = 4] = "PostfixOp", e;
42
- }({}), l = {
45
+ }({}), o = {
43
46
  Program: "Program",
44
47
  Element: "Element",
45
48
  Text: "Text",
@@ -48,169 +51,173 @@ var o = {
48
51
  Comment: "Comment",
49
52
  If: "If",
50
53
  For: "For",
51
- Switch: "Switch"
52
- }, u = class extends Error {
54
+ Switch: "Switch",
55
+ Async: "Async"
56
+ }, s = class extends Error {
53
57
  line;
54
58
  column;
55
59
  offset;
56
60
  constructor(e, t, n, r) {
57
61
  super(`LexerError [${t}:${n}]: ${e}`), this.line = t, this.column = n, this.offset = r, this.name = "DriftLexerError";
58
62
  }
59
- }, d = class extends Error {
63
+ }, c = class extends Error {
60
64
  line;
61
65
  column;
62
66
  offset;
63
67
  constructor(e, t, n, r) {
64
68
  super(`ParserError [${t}:${n}]: ${e}`), this.line = t, this.column = n, this.offset = r, this.name = "DriftParserError";
65
69
  }
66
- }, f = /* @__PURE__ */ function(e) {
67
- return e[e.RETURN = 0] = "RETURN", e[e.CREATE_ELEMENT = 1] = "CREATE_ELEMENT", e[e.CREATE_TEXT = 2] = "CREATE_TEXT", e[e.CREATE_COMMENT = 3] = "CREATE_COMMENT", e[e.APPEND_CHILD = 4] = "APPEND_CHILD", e[e.SET_ATTR = 5] = "SET_ATTR", e[e.CREATE_FRAGMENT = 6] = "CREATE_FRAGMENT", e[e.INTERPOLATE_TEXT = 7] = "INTERPOLATE_TEXT", e[e.EXEC_SCRIPT = 12] = "EXEC_SCRIPT", e[e.REACTIVE_IF = 13] = "REACTIVE_IF", e[e.REACTIVE_FOR = 14] = "REACTIVE_FOR", e[e.MOUNT_COMPONENT = 15] = "MOUNT_COMPONENT", e;
68
- }({}), p = {
69
- [s.Data]: [
70
+ }, l = /* @__PURE__ */ function(e) {
71
+ return e[e.RETURN = 0] = "RETURN", e[e.CREATE_ELEMENT = 1] = "CREATE_ELEMENT", e[e.CREATE_TEXT = 2] = "CREATE_TEXT", e[e.CREATE_COMMENT = 3] = "CREATE_COMMENT", e[e.APPEND_CHILD = 4] = "APPEND_CHILD", e[e.SET_ATTR = 5] = "SET_ATTR", e[e.CREATE_FRAGMENT = 6] = "CREATE_FRAGMENT", e[e.INTERPOLATE_TEXT = 7] = "INTERPOLATE_TEXT", e[e.EXEC_SCRIPT = 12] = "EXEC_SCRIPT", e[e.REACTIVE_IF = 13] = "REACTIVE_IF", e[e.REACTIVE_FOR = 14] = "REACTIVE_FOR", e[e.MOUNT_COMPONENT = 15] = "MOUNT_COMPONENT", e[e.REACTIVE_ASYNC = 16] = "REACTIVE_ASYNC", e[e.REACTIVE_SWITCH = 17] = "REACTIVE_SWITCH", e;
72
+ }({}), u = {
73
+ [i.Data]: [
70
74
  {
71
- to: s.Comment,
75
+ to: i.Comment,
72
76
  when: "The next characters are <!--",
73
77
  emits: "Comment"
74
78
  },
75
79
  {
76
- to: s.EndTagOpen,
80
+ to: i.EndTagOpen,
77
81
  when: "The next characters are </",
78
82
  emits: "TagOpenSlash"
79
83
  },
80
84
  {
81
- to: s.TagOpen,
85
+ to: i.TagOpen,
82
86
  when: "The next character is '<' for a start tag",
83
87
  emits: "TagOpen"
84
88
  },
85
89
  {
86
- to: s.Interpolation,
90
+ to: i.Interpolation,
87
91
  when: "The next character is '{' in element content",
88
92
  emits: "Interpolation"
89
93
  },
90
94
  {
91
- to: s.Data,
95
+ to: i.Data,
92
96
  when: "Plain content is consumed up to the next control delimiter",
93
97
  emits: "Text"
94
98
  },
95
99
  {
96
- to: s.EOF,
100
+ to: i.EOF,
97
101
  when: "The source has been fully consumed",
98
102
  emits: "EOF"
99
103
  }
100
104
  ],
101
- [s.TagOpen]: [{
102
- to: s.BeforeAttributeName,
105
+ [i.TagOpen]: [{
106
+ to: i.BeforeAttributeName,
103
107
  when: "A valid opening tag name is consumed",
104
108
  emits: "Identifier"
105
109
  }],
106
- [s.EndTagOpen]: [{
107
- to: s.BeforeAttributeName,
110
+ [i.EndTagOpen]: [{
111
+ to: i.BeforeAttributeName,
108
112
  when: "A valid closing tag name is consumed",
109
113
  emits: "Identifier"
110
114
  }],
111
- [s.BeforeAttributeName]: [
115
+ [i.BeforeAttributeName]: [
112
116
  {
113
- to: s.AttributeName,
117
+ to: i.AttributeName,
114
118
  when: "An attribute name begins in an opening tag",
115
119
  emits: "Identifier"
116
120
  },
117
121
  {
118
- to: s.Data,
122
+ to: i.Data,
119
123
  when: "A tag is closed with '>' and raw-text mode does not apply",
120
124
  emits: "TagClose"
121
125
  },
122
126
  {
123
- to: s.RawText,
127
+ to: i.RawText,
124
128
  when: "A script/style start tag is closed with '>'",
125
129
  emits: "TagClose"
126
130
  },
127
131
  {
128
- to: s.Data,
132
+ to: i.Data,
129
133
  when: "A tag is self-closed with '/>'",
130
134
  emits: "TagSelfClose"
131
135
  }
132
136
  ],
133
- [s.AttributeName]: [{
134
- to: s.AfterAttributeName,
137
+ [i.AttributeName]: [{
138
+ to: i.AfterAttributeName,
135
139
  when: "The attribute identifier has been fully consumed",
136
140
  emits: "Identifier"
137
141
  }],
138
- [s.AfterAttributeName]: [{
139
- to: s.BeforeAttributeValue,
142
+ [i.AfterAttributeName]: [{
143
+ to: i.BeforeAttributeValue,
140
144
  when: "The next character is '='",
141
145
  emits: "Equals"
142
146
  }, {
143
- to: s.BeforeAttributeName,
147
+ to: i.BeforeAttributeName,
144
148
  when: "The attribute is boolean and the lexer moves to the next attribute or tag boundary",
145
149
  emits: "No token"
146
150
  }],
147
- [s.BeforeAttributeValue]: [{
148
- to: s.AttributeValueQuoted,
151
+ [i.BeforeAttributeValue]: [{
152
+ to: i.AttributeValueQuoted,
149
153
  when: "The attribute value starts with a quote",
150
154
  emits: "StringLiteral"
151
155
  }, {
152
- to: s.AttributeValueInterpolation,
156
+ to: i.AttributeValueInterpolation,
153
157
  when: "The attribute value starts with '{'",
154
158
  emits: "Interpolation"
155
159
  }],
156
- [s.AttributeValueQuoted]: [{
157
- to: s.BeforeAttributeName,
160
+ [i.AttributeValueQuoted]: [{
161
+ to: i.BeforeAttributeName,
158
162
  when: "The closing quote is consumed",
159
163
  emits: "StringLiteral"
160
164
  }],
161
- [s.AttributeValueInterpolation]: [{
162
- to: s.BeforeAttributeName,
165
+ [i.AttributeValueInterpolation]: [{
166
+ to: i.BeforeAttributeName,
163
167
  when: "The interpolation closes and the attribute value is complete",
164
168
  emits: "Interpolation"
165
169
  }],
166
- [s.Comment]: [{
167
- to: s.Data,
170
+ [i.Comment]: [{
171
+ to: i.Data,
168
172
  when: "The terminating --> delimiter is consumed",
169
173
  emits: "Comment"
170
174
  }],
171
- [s.Interpolation]: [{
172
- to: s.Data,
175
+ [i.Interpolation]: [{
176
+ to: i.Data,
173
177
  when: "A content interpolation closes with a matching brace",
174
178
  emits: "Interpolation"
175
179
  }, {
176
- to: s.BeforeAttributeName,
180
+ to: i.BeforeAttributeName,
177
181
  when: "An attribute interpolation closes with a matching brace",
178
182
  emits: "Interpolation"
179
183
  }],
180
- [s.RawText]: [
184
+ [i.RawText]: [
181
185
  {
182
- to: s.RawText,
186
+ to: i.RawText,
183
187
  when: "Plain raw-text content is consumed before the matching closing tag",
184
188
  emits: "Text"
185
189
  },
186
190
  {
187
- to: s.EndTagOpen,
191
+ to: i.EndTagOpen,
188
192
  when: "The matching raw-text closing tag begins with </",
189
193
  emits: "TagOpenSlash"
190
194
  },
191
195
  {
192
- to: s.EOF,
196
+ to: i.EOF,
193
197
  when: "The raw-text block reaches end of input before a closing tag appears",
194
198
  emits: "EOF"
195
199
  }
196
200
  ],
197
- [s.EOF]: [{
198
- to: s.EOF,
201
+ [i.EOF]: [{
202
+ to: i.EOF,
199
203
  when: "Additional parser requests are made after end of input",
200
204
  emits: "EOF"
201
205
  }]
202
206
  };
203
- function m(e) {
207
+ function d(e) {
204
208
  return e === "script" || e === "style";
205
209
  }
206
- var h = /* @__PURE__ */ new Set([
210
+ var f = /* @__PURE__ */ new Set([
207
211
  "if",
208
212
  "else",
209
213
  "for",
210
214
  "switch",
211
215
  "case",
212
- "default"
213
- ]), g = /* @__PURE__ */ new Set([
216
+ "default",
217
+ "async",
218
+ "fallback",
219
+ "catch"
220
+ ]), p = /* @__PURE__ */ new Set([
214
221
  "return",
215
222
  "yield",
216
223
  "await",
@@ -223,14 +230,14 @@ var h = /* @__PURE__ */ new Set([
223
230
  "do",
224
231
  "throw",
225
232
  "new"
226
- ]), _ = class {
233
+ ]), m = class {
227
234
  source;
228
235
  offset = 0;
229
236
  line = 1;
230
237
  column = 1;
231
238
  emittedTokenCount = 0;
232
239
  eofToken = null;
233
- state = { kind: s.Data };
240
+ state = { kind: i.Data };
234
241
  blockDepth = 0;
235
242
  elementDepth = 0;
236
243
  blockElementDepthStack = [];
@@ -249,352 +256,357 @@ var h = /* @__PURE__ */ new Set([
249
256
  }
250
257
  readNextToken() {
251
258
  switch (this.state.kind) {
252
- case s.Data: return this.readDataToken();
253
- case s.TagOpen: return this.readTagNameToken(!1);
254
- case s.EndTagOpen: return this.readTagNameToken(!0);
255
- case s.BeforeAttributeName: return this.readBeforeAttributeNameToken();
256
- case s.AttributeName: return this.readAttributeNameToken();
257
- case s.AfterAttributeName: return this.readAfterAttributeNameToken();
258
- case s.BeforeAttributeValue: return this.readBeforeAttributeValueToken();
259
- case s.Comment:
260
- case s.Interpolation:
261
- case s.AttributeValueQuoted:
262
- case s.AttributeValueInterpolation: throw Error(`Lexer entered transient state '${this.state.kind}' unexpectedly.`);
263
- case s.RawText: return this.readRawTextToken();
264
- case s.EOF: return this.getOrCreateEOFToken();
259
+ case i.Data: return this.readDataToken();
260
+ case i.TagOpen: return this.readTagNameToken(!1);
261
+ case i.EndTagOpen: return this.readTagNameToken(!0);
262
+ case i.BeforeAttributeName: return this.readBeforeAttributeNameToken();
263
+ case i.AttributeName: return this.readAttributeNameToken();
264
+ case i.AfterAttributeName: return this.readAfterAttributeNameToken();
265
+ case i.BeforeAttributeValue: return this.readBeforeAttributeValueToken();
266
+ case i.Comment:
267
+ case i.Interpolation:
268
+ case i.AttributeValueQuoted:
269
+ case i.AttributeValueInterpolation: throw Error(`Lexer entered transient state '${this.state.kind}' unexpectedly.`);
270
+ case i.RawText: return this.readRawTextToken();
271
+ case i.EOF: return this.getOrCreateEOFToken();
265
272
  }
266
273
  }
267
274
  readDataToken() {
268
275
  let e = this.getLocation();
269
- if (this.isAtEnd()) return this.transitionTo({ kind: s.EOF }), this.getOrCreateEOFToken(e);
270
- if (this.startsWith("<!--")) return this.consumePattern("<!--"), this.transitionTo({ kind: s.Comment }), this.readCommentToken(e);
271
- if (this.startsWith("</")) return this.consumePattern("</"), this.transitionTo({ kind: s.EndTagOpen }), this.createToken(o.TagOpenSlash, "</", e);
272
- if (this.peek() === "<") return this.advance(), this.transitionTo({ kind: s.TagOpen }), this.createToken(o.TagOpen, "<", e);
276
+ if (this.isAtEnd()) return this.transitionTo({ kind: i.EOF }), this.getOrCreateEOFToken(e);
277
+ if (this.startsWith("<!--")) return this.consumePattern("<!--"), this.transitionTo({ kind: i.Comment }), this.readCommentToken(e);
278
+ if (this.startsWith("</")) return this.consumePattern("</"), this.transitionTo({ kind: i.EndTagOpen }), this.createToken(r.TagOpenSlash, "</", e);
279
+ if (this.peek() === "<") return this.advance(), this.transitionTo({ kind: i.TagOpen }), this.createToken(r.TagOpen, "<", e);
273
280
  if (this.peek() === "{") return this.advance(), this.transitionTo({
274
- kind: s.Interpolation,
281
+ kind: i.Interpolation,
275
282
  context: "content",
276
283
  tagName: null
277
284
  }), this.readInterpolationToken(e, "content", null);
278
285
  let t = this.blockDepth > 0 && this.blockElementDepthStack.length > 0 && this.elementDepth === this.blockElementDepthStack[this.blockElementDepthStack.length - 1];
279
- return this.peek() === "}" && t ? (this.advance(), this.blockDepth--, this.blockElementDepthStack.pop(), this.createToken(o.BlockClose, "}", e)) : this.peek() === "@" ? this.readDirectiveToken(e) : this.readTextToken();
286
+ return this.peek() === "}" && t ? (this.advance(), this.blockDepth--, this.blockElementDepthStack.pop(), this.createToken(r.BlockClose, "}", e)) : this.peek() === "@" ? this.readDirectiveToken(e) : this.readTextToken();
280
287
  }
281
288
  readDirectiveToken(e) {
282
289
  this.advance();
283
290
  let t = "";
284
291
  for (; !this.isAtEnd() && /[a-zA-Z]/.test(this.peek());) t += this.advance();
285
- if (!h.has(t)) throw new u(`Unknown directive '@${t}'`, e.line, e.column, e.offset);
286
- if (t === "if") return this.readDirectiveHeader(e, o.DirectiveIf);
292
+ if (!f.has(t)) throw new s(`Unknown directive '@${t}'`, e.line, e.column, e.offset);
293
+ if (t === "if") return this.readDirectiveHeader(e, r.DirectiveIf);
287
294
  if (t === "else") {
288
- if (this.skipWhitespace(), this.startsWith("if") && !/[a-zA-Z0-9_]/.test(this.peek(2))) return this.consumePattern("if"), this.readDirectiveHeader(e, o.DirectiveElseIf);
289
- if (this.skipWhitespace(), this.peek() === "{") return this.advance(), this.blockDepth++, this.blockElementDepthStack.push(this.elementDepth), this.createToken(o.DirectiveElse, "", e);
290
- throw new u("Expected '{' after @else directive", e.line, e.column, e.offset);
291
- } else if (t === "for") return this.readDirectiveHeader(e, o.DirectiveFor);
292
- else if (t === "switch") return this.readDirectiveHeader(e, o.DirectiveSwitch);
293
- else if (t === "case") return this.readDirectiveHeader(e, o.DirectiveCase);
294
- else return this.skipWhitespace(), this.peek() === "{" && (this.advance(), this.blockDepth++, this.blockElementDepthStack.push(this.elementDepth)), this.createToken(o.DirectiveDefault, "", e);
295
+ if (this.skipWhitespace(), this.startsWith("if") && !/[a-zA-Z0-9_]/.test(this.peek(2))) return this.consumePattern("if"), this.readDirectiveHeader(e, r.DirectiveElseIf);
296
+ if (this.skipWhitespace(), this.peek() === "{") return this.advance(), this.blockDepth++, this.blockElementDepthStack.push(this.elementDepth), this.createToken(r.DirectiveElse, "", e);
297
+ throw new s("Expected '{' after @else directive", e.line, e.column, e.offset);
298
+ } else if (t === "for") return this.readDirectiveHeader(e, r.DirectiveFor);
299
+ else if (t === "switch") return this.readDirectiveHeader(e, r.DirectiveSwitch);
300
+ else if (t === "case") return this.readDirectiveHeader(e, r.DirectiveCase);
301
+ else if (t === "async") return this.readDirectiveHeader(e, r.DirectiveAsync);
302
+ else if (t === "catch") return this.readDirectiveHeader(e, r.DirectiveCatch);
303
+ else if (t === "fallback") {
304
+ if (this.skipWhitespace(), this.peek() === "{") return this.advance(), this.blockDepth++, this.blockElementDepthStack.push(this.elementDepth), this.createToken(r.DirectiveFallback, "", e);
305
+ throw new s("Expected '{' after @fallback directive", e.line, e.column, e.offset);
306
+ } else return this.skipWhitespace(), this.peek() === "{" && (this.advance(), this.blockDepth++, this.blockElementDepthStack.push(this.elementDepth)), this.createToken(r.DirectiveDefault, "", e);
295
307
  }
296
308
  readDirectiveHeader(e, t) {
297
309
  this.skipWhitespace();
298
- let n = "", r = 0, i = 0, a = 0, o = [], s = null, l = !1, d = !1, f = !1, p = !1, m = !1, h = c.Start;
310
+ let n = "", r = 0, i = 0, o = 0, c = [], l = null, u = !1, d = !1, f = !1, m = !1, h = !1, g = a.Start;
299
311
  for (; !this.isAtEnd();) {
300
- let u = this.advance();
312
+ let s = this.advance();
301
313
  if (d) {
302
- n += u, u === "\n" && (d = !1);
314
+ n += s, s === "\n" && (d = !1);
303
315
  continue;
304
316
  }
305
317
  if (f) {
306
- n += u, u === "/" && n.endsWith("*/") && (f = !1);
318
+ n += s, s === "/" && n.endsWith("*/") && (f = !1);
307
319
  continue;
308
320
  }
309
- if (p) {
310
- n += u, l ? l = !1 : u === "\\" ? l = !0 : u === "[" ? m = !0 : u === "]" && m ? m = !1 : u === "/" && !m && (p = !1, h = c.IdentifierOrLiteral);
321
+ if (m) {
322
+ n += s, u ? u = !1 : s === "\\" ? u = !0 : s === "[" ? h = !0 : s === "]" && h ? h = !1 : s === "/" && !h && (m = !1, g = a.IdentifierOrLiteral);
311
323
  continue;
312
324
  }
313
- if (s !== null) {
314
- if (n += u, l) l = !1;
315
- else if (u === "\\") l = !0;
316
- else if (u === s) s = null, h = c.IdentifierOrLiteral;
317
- else if (s === "`" && u === "{" && n.endsWith("${")) {
325
+ if (l !== null) {
326
+ if (n += s, u) u = !1;
327
+ else if (s === "\\") u = !0;
328
+ else if (s === l) l = null, g = a.IdentifierOrLiteral;
329
+ else if (l === "`" && s === "{" && n.endsWith("${")) {
318
330
  let e = 0;
319
331
  for (let t = n.length - 3; t >= 0 && n[t] === "\\"; t--) e++;
320
- e % 2 == 0 && (o.push(a), s = null, a++, h = c.Start);
332
+ e % 2 == 0 && (c.push(o), l = null, o++, g = a.Start);
321
333
  }
322
334
  continue;
323
335
  }
324
- if (u === "/" && !l) {
336
+ if (s === "/" && !u) {
325
337
  let e = this.peek();
326
338
  if (e === "/") {
327
- d = !0, n += u;
339
+ d = !0, n += s;
328
340
  continue;
329
341
  } else if (e === "*") {
330
- f = !0, n += u;
342
+ f = !0, n += s;
331
343
  continue;
332
- } else if (this.isRegexStart(h)) {
333
- p = !0, m = !1, n += u;
344
+ } else if (this.isRegexStart(g)) {
345
+ m = !0, h = !1, n += s;
334
346
  continue;
335
- } else h = c.Punctuator;
347
+ } else g = a.Punctuator;
336
348
  }
337
- if (u === "\"" || u === "'" || u === "`") {
338
- s = u, n += u;
349
+ if (s === "\"" || s === "'" || s === "`") {
350
+ l = s, n += s;
339
351
  continue;
340
352
  }
341
- if (u === "(") {
342
- r++, h = c.Start, n += u;
353
+ if (s === "(") {
354
+ r++, g = a.Start, n += s;
343
355
  continue;
344
356
  }
345
- if (u === ")") {
346
- r > 0 && r--, h = c.IdentifierOrLiteral, n += u;
357
+ if (s === ")") {
358
+ r > 0 && r--, g = a.IdentifierOrLiteral, n += s;
347
359
  continue;
348
360
  }
349
- if (u === "[") {
350
- i++, h = c.Start, n += u;
361
+ if (s === "[") {
362
+ i++, g = a.Start, n += s;
351
363
  continue;
352
364
  }
353
- if (u === "]") {
354
- i > 0 && i--, h = c.IdentifierOrLiteral, n += u;
365
+ if (s === "]") {
366
+ i > 0 && i--, g = a.IdentifierOrLiteral, n += s;
355
367
  continue;
356
368
  }
357
- if (u === "{") {
358
- if (r === 0 && i === 0 && a === 0 && o.length === 0) return this.blockDepth++, this.blockElementDepthStack.push(this.elementDepth), this.createToken(t, n.trim(), e);
359
- a++, h = c.Start, n += u;
369
+ if (s === "{") {
370
+ if (!/\b(as|catch)\s*$/.test(n) && r === 0 && i === 0 && o === 0 && c.length === 0) return this.blockDepth++, this.blockElementDepthStack.push(this.elementDepth), this.createToken(t, n.trim(), e);
371
+ o++, g = a.Start, n += s;
360
372
  continue;
361
373
  }
362
- if (u === "}") {
363
- a--, o.length > 0 && a === o[o.length - 1] && (o.pop(), s = "`"), h = c.IdentifierOrLiteral, n += u;
374
+ if (s === "}") {
375
+ o--, c.length > 0 && o === c[c.length - 1] && (c.pop(), l = "`"), g = a.IdentifierOrLiteral, n += s;
364
376
  continue;
365
377
  }
366
- if (u === "+" || u === "-") {
367
- if (this.peek() === u) {
378
+ if (s === "+" || s === "-") {
379
+ if (this.peek() === s) {
368
380
  let e = this.advance();
369
- n += u + e, h = h === c.IdentifierOrLiteral ? c.PostfixOp : c.Start;
381
+ n += s + e, g = g === a.IdentifierOrLiteral ? a.PostfixOp : a.Start;
370
382
  continue;
371
383
  }
372
- h = c.Punctuator, n += u;
384
+ g = a.Punctuator, n += s;
373
385
  continue;
374
386
  }
375
- if ("=(,:;!&|?*%<>~^".includes(u)) {
376
- h = c.Punctuator, n += u;
387
+ if ("=(,:;!&|?*%<>~^".includes(s)) {
388
+ g = a.Punctuator, n += s;
377
389
  continue;
378
390
  }
379
- if (/[a-zA-Z0-9_$]/.test(u)) {
380
- let e = u;
391
+ if (/[a-zA-Z0-9_$]/.test(s)) {
392
+ let e = s;
381
393
  for (; !this.isAtEnd() && /[a-zA-Z0-9_$]/.test(this.peek());) e += this.advance();
382
- h = g.has(e) ? c.Keyword : c.IdentifierOrLiteral, n += e;
394
+ g = p.has(e) ? a.Keyword : a.IdentifierOrLiteral, n += e;
383
395
  continue;
384
396
  }
385
- n += u;
397
+ n += s;
386
398
  }
387
- throw new u("Unterminated directive header, expected '{'", e.line, e.column, e.offset);
399
+ throw new s("Unterminated directive header, expected '{'", e.line, e.column, e.offset);
388
400
  }
389
401
  readTagNameToken(e) {
390
402
  if (this.isAtEnd()) throw this.createUnexpectedEOFError(e ? "after closing tag opener" : "after opening tag opener");
391
403
  let t = this.getLocation(), n = this.peek();
392
- if (!this.isIdentifierStart(n)) throw new u(`Expected tag name but found '${n || "EOF"}'`, t.line, t.column, t.offset);
393
- let r = this.readIdentifierValue(), i = !e && m(r);
404
+ if (!this.isIdentifierStart(n)) throw new s(`Expected tag name but found '${n || "EOF"}'`, t.line, t.column, t.offset);
405
+ let a = this.readIdentifierValue(), o = !e && d(a);
394
406
  return this.transitionTo({
395
- kind: s.BeforeAttributeName,
396
- tagName: r,
407
+ kind: i.BeforeAttributeName,
408
+ tagName: a,
397
409
  isClosingTag: e,
398
- entersRawText: i
399
- }), this.createToken(o.Identifier, r, t);
410
+ entersRawText: o
411
+ }), this.createToken(r.Identifier, a, t);
400
412
  }
401
413
  readBeforeAttributeNameToken() {
402
414
  let t = this.state;
403
- if (t.kind !== s.BeforeAttributeName) throw Error(`Expected BeforeAttributeName state but found '${t.kind}'.`);
415
+ if (t.kind !== i.BeforeAttributeName) throw Error(`Expected BeforeAttributeName state but found '${t.kind}'.`);
404
416
  if (this.skipWhitespace(), this.isAtEnd()) throw this.createUnexpectedEOFError(`inside tag <${t.tagName}>`);
405
417
  let n = this.getLocation();
406
418
  if (t.isClosingTag) {
407
- if (this.peek() !== ">") throw new u(`Unexpected character '${this.peek()}' inside closing tag </${t.tagName}>`, n.line, n.column, n.offset);
408
- return this.advance(), this.elementDepth > 0 && this.elementDepth--, this.transitionTo({ kind: s.Data }), this.createToken(o.TagClose, ">", n);
419
+ if (this.peek() !== ">") throw new s(`Unexpected character '${this.peek()}' inside closing tag </${t.tagName}>`, n.line, n.column, n.offset);
420
+ return this.advance(), this.elementDepth > 0 && this.elementDepth--, this.transitionTo({ kind: i.Data }), this.createToken(r.TagClose, ">", n);
409
421
  }
410
422
  if (this.peek() === "/") {
411
423
  let e = 1;
412
424
  for (; this.peek(e) === " " || this.peek(e) === " " || this.peek(e) === "\n" || this.peek(e) === "\r";) e++;
413
425
  if (this.peek(e) === ">") {
414
426
  for (let t = 0; t <= e; t++) this.advance();
415
- return this.transitionTo({ kind: s.Data }), this.createToken(o.TagSelfClose, "/>", n);
427
+ return this.transitionTo({ kind: i.Data }), this.createToken(r.TagSelfClose, "/>", n);
416
428
  }
417
429
  }
418
- if (this.peek() === ">") return this.advance(), e.has(t.tagName.toLowerCase()) || this.elementDepth++, t.entersRawText && m(t.tagName) ? this.transitionTo({
419
- kind: s.RawText,
430
+ if (this.peek() === ">") return this.advance(), e.has(t.tagName.toLowerCase()) || this.elementDepth++, t.entersRawText && d(t.tagName) ? this.transitionTo({
431
+ kind: i.RawText,
420
432
  tagName: t.tagName
421
- }) : this.transitionTo({ kind: s.Data }), this.createToken(o.TagClose, ">", n);
422
- if (!this.isIdentifierStart(this.peek())) throw new u(`Unexpected character '${this.peek()}' inside tag <${t.tagName}>`, n.line, n.column, n.offset);
433
+ }) : this.transitionTo({ kind: i.Data }), this.createToken(r.TagClose, ">", n);
434
+ if (!this.isIdentifierStart(this.peek())) throw new s(`Unexpected character '${this.peek()}' inside tag <${t.tagName}>`, n.line, n.column, n.offset);
423
435
  return this.transitionTo({
424
- kind: s.AttributeName,
436
+ kind: i.AttributeName,
425
437
  tagName: t.tagName,
426
438
  attributeName: null
427
439
  }), this.readAttributeNameToken();
428
440
  }
429
441
  readAttributeNameToken() {
430
442
  let e = this.state;
431
- if (e.kind !== s.AttributeName) throw Error(`Expected AttributeName state but found '${e.kind}'.`);
443
+ if (e.kind !== i.AttributeName) throw Error(`Expected AttributeName state but found '${e.kind}'.`);
432
444
  let t = this.getLocation();
433
- if (!this.isIdentifierStart(this.peek())) throw new u(`Expected attribute name but found '${this.peek() || "EOF"}'`, t.line, t.column, t.offset);
434
- let n = this.readIdentifierValue(), r = e.tagName;
445
+ if (!this.isIdentifierStart(this.peek())) throw new s(`Expected attribute name but found '${this.peek() || "EOF"}'`, t.line, t.column, t.offset);
446
+ let n = this.readIdentifierValue(), a = e.tagName;
435
447
  return this.transitionTo({
436
- kind: s.AfterAttributeName,
437
- tagName: r,
448
+ kind: i.AfterAttributeName,
449
+ tagName: a,
438
450
  attributeName: n
439
- }), this.createToken(o.Identifier, n, t);
451
+ }), this.createToken(r.Identifier, n, t);
440
452
  }
441
453
  readAfterAttributeNameToken() {
442
454
  let e = this.state;
443
- if (e.kind !== s.AfterAttributeName) throw Error(`Expected AfterAttributeName state but found '${e.kind}'.`);
455
+ if (e.kind !== i.AfterAttributeName) throw Error(`Expected AfterAttributeName state but found '${e.kind}'.`);
444
456
  let t = e.tagName, n = e.attributeName;
445
457
  if (this.skipWhitespace(), this.isAtEnd()) throw this.createUnexpectedEOFError(`after attribute '${n}' in <${t}>`);
446
458
  if (this.peek() === "=") {
447
459
  let e = this.getLocation();
448
460
  return this.advance(), this.transitionTo({
449
- kind: s.BeforeAttributeValue,
461
+ kind: i.BeforeAttributeValue,
450
462
  tagName: t,
451
463
  attributeName: n
452
- }), this.createToken(o.Equals, "=", e);
464
+ }), this.createToken(r.Equals, "=", e);
453
465
  }
454
466
  return this.transitionTo({
455
- kind: s.BeforeAttributeName,
467
+ kind: i.BeforeAttributeName,
456
468
  tagName: t,
457
469
  isClosingTag: !1,
458
- entersRawText: m(t)
470
+ entersRawText: d(t)
459
471
  }), this.readBeforeAttributeNameToken();
460
472
  }
461
473
  readBeforeAttributeValueToken() {
462
474
  let e = this.state;
463
- if (e.kind !== s.BeforeAttributeValue) throw Error(`Expected BeforeAttributeValue state but found '${e.kind}'.`);
475
+ if (e.kind !== i.BeforeAttributeValue) throw Error(`Expected BeforeAttributeValue state but found '${e.kind}'.`);
464
476
  let t = e.tagName, n = e.attributeName;
465
477
  if (this.skipWhitespace(), this.isAtEnd()) throw this.createUnexpectedEOFError(`before value for attribute '${n}' in <${t}>`);
466
- let r = this.getLocation(), i = this.peek();
467
- if (i === "\"" || i === "'") return this.advance(), this.transitionTo({
468
- kind: s.AttributeValueQuoted,
478
+ let r = this.getLocation(), a = this.peek();
479
+ if (a === "\"" || a === "'") return this.advance(), this.transitionTo({
480
+ kind: i.AttributeValueQuoted,
469
481
  tagName: t,
470
482
  attributeName: n,
471
- quote: i
472
- }), this.readQuotedStringToken(r, i, t);
473
- if (i === "{") return this.advance(), this.transitionTo({
474
- kind: s.AttributeValueInterpolation,
483
+ quote: a
484
+ }), this.readQuotedStringToken(r, a, t);
485
+ if (a === "{") return this.advance(), this.transitionTo({
486
+ kind: i.AttributeValueInterpolation,
475
487
  tagName: t,
476
488
  attributeName: n
477
489
  }), this.readInterpolationToken(r, "attribute", t);
478
- throw new u(`Expected quoted string or interpolation for attribute '${n}' in <${t}>`, r.line, r.column, r.offset);
490
+ throw new s(`Expected quoted string or interpolation for attribute '${n}' in <${t}>`, r.line, r.column, r.offset);
479
491
  }
480
492
  readCommentToken(e) {
481
493
  let t = "";
482
494
  for (; !this.isAtEnd();) {
483
- if (this.startsWith("-->")) return this.consumePattern("-->"), this.transitionTo({ kind: s.Data }), this.createToken(o.Comment, t, e);
495
+ if (this.startsWith("-->")) return this.consumePattern("-->"), this.transitionTo({ kind: i.Data }), this.createToken(r.Comment, t, e);
484
496
  t += this.advance();
485
497
  }
486
- throw new u("Unterminated XML comment", e.line, e.column, e.offset);
498
+ throw new s("Unterminated XML comment", e.line, e.column, e.offset);
487
499
  }
488
500
  isRegexStart(e) {
489
- return e === c.Start || e === c.Punctuator || e === c.Keyword;
501
+ return e === a.Start || e === a.Punctuator || e === a.Keyword;
490
502
  }
491
503
  readInterpolationToken(e, t, n) {
492
- let r = 1, i = null, a = !1, l = !1, d = !1, f = !1, p = !1, h = c.Start, _ = [], v = "";
504
+ let o = 1, c = null, l = !1, u = !1, f = !1, m = !1, h = !1, g = a.Start, _ = [], v = "";
493
505
  for (; !this.isAtEnd();) {
494
- let u = this.advance();
495
- if (l) {
496
- v += u, u === "\n" && (l = !1);
506
+ let s = this.advance();
507
+ if (u) {
508
+ v += s, s === "\n" && (u = !1);
497
509
  continue;
498
510
  }
499
- if (d) {
500
- v += u, u === "/" && v.endsWith("*/") && (d = !1);
511
+ if (f) {
512
+ v += s, s === "/" && v.endsWith("*/") && (f = !1);
501
513
  continue;
502
514
  }
503
- if (f) {
504
- v += u, a ? a = !1 : u === "\\" ? a = !0 : u === "[" ? p = !0 : u === "]" && p ? p = !1 : u === "/" && !p && (f = !1, h = c.IdentifierOrLiteral);
515
+ if (m) {
516
+ v += s, l ? l = !1 : s === "\\" ? l = !0 : s === "[" ? h = !0 : s === "]" && h ? h = !1 : s === "/" && !h && (m = !1, g = a.IdentifierOrLiteral);
505
517
  continue;
506
518
  }
507
- if (i !== null) {
508
- if (v += u, a) a = !1;
509
- else if (u === "\\") a = !0;
510
- else if (u === i) i = null, h = c.IdentifierOrLiteral;
511
- else if (i === "`" && u === "{" && v.endsWith("${")) {
519
+ if (c !== null) {
520
+ if (v += s, l) l = !1;
521
+ else if (s === "\\") l = !0;
522
+ else if (s === c) c = null, g = a.IdentifierOrLiteral;
523
+ else if (c === "`" && s === "{" && v.endsWith("${")) {
512
524
  let e = 0;
513
525
  for (let t = v.length - 3; t >= 0 && v[t] === "\\"; t--) e++;
514
- e % 2 == 0 && (_.push(r), i = null, r++, h = c.Start);
526
+ e % 2 == 0 && (_.push(o), c = null, o++, g = a.Start);
515
527
  }
516
528
  continue;
517
529
  }
518
- if (u === "/" && !a) {
530
+ if (s === "/" && !l) {
519
531
  let e = this.peek();
520
532
  if (e === "/") {
521
- l = !0, v += u;
533
+ u = !0, v += s;
522
534
  continue;
523
535
  } else if (e === "*") {
524
- d = !0, v += u;
536
+ f = !0, v += s;
525
537
  continue;
526
- } else if (this.isRegexStart(h)) {
527
- f = !0, p = !1, v += u;
538
+ } else if (this.isRegexStart(g)) {
539
+ m = !0, h = !1, v += s;
528
540
  continue;
529
- } else h = c.Punctuator;
541
+ } else g = a.Punctuator;
530
542
  }
531
- if (u === "\"" || u === "'" || u === "`") {
532
- inQuote: i = u;
533
- v += u;
543
+ if (s === "\"" || s === "'" || s === "`") {
544
+ inQuote: c = s;
545
+ v += s;
534
546
  continue;
535
547
  }
536
- if (u === "(") {
537
- h = c.Start, v += u;
548
+ if (s === "(") {
549
+ g = a.Start, v += s;
538
550
  continue;
539
551
  }
540
- if (u === ")") {
541
- h = c.IdentifierOrLiteral, v += u;
552
+ if (s === ")") {
553
+ g = a.IdentifierOrLiteral, v += s;
542
554
  continue;
543
555
  }
544
- if (u === "[") {
545
- h = c.Start, v += u;
556
+ if (s === "[") {
557
+ g = a.Start, v += s;
546
558
  continue;
547
559
  }
548
- if (u === "]") {
549
- h = c.IdentifierOrLiteral, v += u;
560
+ if (s === "]") {
561
+ g = a.IdentifierOrLiteral, v += s;
550
562
  continue;
551
563
  }
552
- if (u === "{") {
553
- r++, h = c.Start, v += u;
564
+ if (s === "{") {
565
+ o++, g = a.Start, v += s;
554
566
  continue;
555
567
  }
556
- if (u === "}") {
557
- if (r--, _.length > 0 && r === _[_.length - 1] && (_.pop(), i = "`"), h = c.IdentifierOrLiteral, r === 0) return t === "attribute" && n !== null ? this.transitionTo({
558
- kind: s.BeforeAttributeName,
568
+ if (s === "}") {
569
+ if (o--, _.length > 0 && o === _[_.length - 1] && (_.pop(), c = "`"), g = a.IdentifierOrLiteral, o === 0) return t === "attribute" && n !== null ? this.transitionTo({
570
+ kind: i.BeforeAttributeName,
559
571
  tagName: n,
560
572
  isClosingTag: !1,
561
- entersRawText: m(n)
562
- }) : this.transitionTo({ kind: s.Data }), this.createToken(o.Interpolation, v, e);
563
- v += u;
573
+ entersRawText: d(n)
574
+ }) : this.transitionTo({ kind: i.Data }), this.createToken(r.Interpolation, v, e);
575
+ v += s;
564
576
  continue;
565
577
  }
566
- if (u === "+" || u === "-") {
567
- if (this.peek() === u) {
578
+ if (s === "+" || s === "-") {
579
+ if (this.peek() === s) {
568
580
  let e = this.advance();
569
- v += u + e, h = h === c.IdentifierOrLiteral ? c.PostfixOp : c.Start;
581
+ v += s + e, g = g === a.IdentifierOrLiteral ? a.PostfixOp : a.Start;
570
582
  continue;
571
583
  }
572
- h = c.Punctuator, v += u;
584
+ g = a.Punctuator, v += s;
573
585
  continue;
574
586
  }
575
- if ("=(,:;!&|?*%<>~^".includes(u)) {
576
- h = c.Punctuator, v += u;
587
+ if ("=(,:;!&|?*%<>~^".includes(s)) {
588
+ g = a.Punctuator, v += s;
577
589
  continue;
578
590
  }
579
- if (/[a-zA-Z0-9_$]/.test(u)) {
580
- let e = u;
591
+ if (/[a-zA-Z0-9_$]/.test(s)) {
592
+ let e = s;
581
593
  for (; !this.isAtEnd() && /[a-zA-Z0-9_$]/.test(this.peek());) e += this.advance();
582
- h = g.has(e) ? c.Keyword : c.IdentifierOrLiteral, v += e;
594
+ g = p.has(e) ? a.Keyword : a.IdentifierOrLiteral, v += e;
583
595
  continue;
584
596
  }
585
- v += u;
597
+ v += s;
586
598
  }
587
- throw new u("Unterminated interpolation expression, expected closing brace '}'", e.line, e.column, e.offset);
599
+ throw new s("Unterminated interpolation expression, expected closing brace '}'", e.line, e.column, e.offset);
588
600
  }
589
601
  readRawTextToken() {
590
602
  let e = this.state;
591
- if (e.kind !== s.RawText) throw Error(`Expected RawText state but found '${e.kind}'.`);
603
+ if (e.kind !== i.RawText) throw Error(`Expected RawText state but found '${e.kind}'.`);
592
604
  let t = this.getLocation(), n = `</${e.tagName}`;
593
- if (this.isAtEnd()) return this.transitionTo({ kind: s.EOF }), this.getOrCreateEOFToken(t);
594
- if (this.isRawTextClosingTagAhead(n)) return this.consumePattern("</"), this.transitionTo({ kind: s.EndTagOpen }), this.createToken(o.TagOpenSlash, "</", t);
595
- let r = "";
596
- for (; !this.isAtEnd() && !this.isRawTextClosingTagAhead(n);) r += this.advance();
597
- return r.length > 0 ? this.createToken(o.Text, r, t) : (this.transitionTo({ kind: s.EOF }), this.getOrCreateEOFToken(t));
605
+ if (this.isAtEnd()) return this.transitionTo({ kind: i.EOF }), this.getOrCreateEOFToken(t);
606
+ if (this.isRawTextClosingTagAhead(n)) return this.consumePattern("</"), this.transitionTo({ kind: i.EndTagOpen }), this.createToken(r.TagOpenSlash, "</", t);
607
+ let a = "";
608
+ for (; !this.isAtEnd() && !this.isRawTextClosingTagAhead(n);) a += this.advance();
609
+ return a.length > 0 ? this.createToken(r.Text, a, t) : (this.transitionTo({ kind: i.EOF }), this.getOrCreateEOFToken(t));
598
610
  }
599
611
  readTextToken() {
600
612
  let e = this.getLocation(), t = "", n = () => this.blockDepth > 0 && this.blockElementDepthStack.length > 0 && this.elementDepth === this.blockElementDepthStack[this.blockElementDepthStack.length - 1];
@@ -603,29 +615,29 @@ var h = /* @__PURE__ */ new Set([
603
615
  if (e === "<" || e === "{" || e === "@" || e === "}" && n()) break;
604
616
  t += this.advance();
605
617
  }
606
- return this.createToken(o.Text, t, e);
618
+ return this.createToken(r.Text, t, e);
607
619
  }
608
620
  readQuotedStringToken(e, t, n) {
609
- let r = "", i = !1;
621
+ let a = "", o = !1;
610
622
  for (; !this.isAtEnd();) {
611
- let a = this.peek();
612
- if (i) {
613
- i = !1, r += this.advance();
623
+ let s = this.peek();
624
+ if (o) {
625
+ o = !1, a += this.advance();
614
626
  continue;
615
627
  }
616
- if (a === "\\") {
617
- i = !0, this.advance();
628
+ if (s === "\\") {
629
+ o = !0, this.advance();
618
630
  continue;
619
631
  }
620
- if (a === t) return this.advance(), this.transitionTo({
621
- kind: s.BeforeAttributeName,
632
+ if (s === t) return this.advance(), this.transitionTo({
633
+ kind: i.BeforeAttributeName,
622
634
  tagName: n,
623
635
  isClosingTag: !1,
624
- entersRawText: m(n)
625
- }), this.createToken(o.StringLiteral, r, e);
626
- r += this.advance();
636
+ entersRawText: d(n)
637
+ }), this.createToken(r.StringLiteral, a, e);
638
+ a += this.advance();
627
639
  }
628
- throw new u(`Unterminated string literal, expected closing quote ${t}`, e.line, e.column, e.offset);
640
+ throw new s(`Unterminated string literal, expected closing quote ${t}`, e.line, e.column, e.offset);
629
641
  }
630
642
  readIdentifierValue() {
631
643
  let e = "";
@@ -634,7 +646,7 @@ var h = /* @__PURE__ */ new Set([
634
646
  }
635
647
  getOrCreateEOFToken(e = this.getLocation()) {
636
648
  return this.eofToken === null && (this.eofToken = {
637
- type: o.EOF,
649
+ type: r.EOF,
638
650
  value: "",
639
651
  loc: {
640
652
  start: e,
@@ -654,10 +666,10 @@ var h = /* @__PURE__ */ new Set([
654
666
  }
655
667
  createUnexpectedEOFError(e) {
656
668
  let t = this.getLocation();
657
- return new u(`Unexpected end of input ${e}`, t.line, t.column, t.offset);
669
+ return new s(`Unexpected end of input ${e}`, t.line, t.column, t.offset);
658
670
  }
659
671
  transitionTo(e) {
660
- if (!p[this.state.kind].some((t) => t.to === e.kind)) throw Error(`Invalid lexer transition from '${this.state.kind}' to '${e.kind}'.`);
672
+ if (!u[this.state.kind].some((t) => t.to === e.kind)) throw Error(`Invalid lexer transition from '${this.state.kind}' to '${e.kind}'.`);
661
673
  this.state = e;
662
674
  }
663
675
  getLocation() {
@@ -707,7 +719,7 @@ var h = /* @__PURE__ */ new Set([
707
719
  let n = this.peek(t);
708
720
  return n === "" || n === ">" || n === " " || n === " " || n === "\r" || n === "\n";
709
721
  }
710
- }, v = class {
722
+ }, h = class {
711
723
  tokens;
712
724
  current = 0;
713
725
  constructor(e) {
@@ -718,7 +730,7 @@ var h = /* @__PURE__ */ new Set([
718
730
  let e = this.tokens[this.current];
719
731
  return this.current++, e;
720
732
  }
721
- }, y = {
733
+ }, g = {
722
734
  amp: "&",
723
735
  lt: "<",
724
736
  gt: ">",
@@ -861,7 +873,7 @@ var h = /* @__PURE__ */ new Set([
861
873
  psi: "ψ",
862
874
  omega: "ω"
863
875
  };
864
- function b(e) {
876
+ function _(e) {
865
877
  return !e || !e.includes("&") ? e : e.replace(/&(?:#(\d+)|#x([0-9a-fA-F]+)|([a-zA-Z0-9]+));/g, (e, t, n, r) => {
866
878
  if (t) {
867
879
  let e = parseInt(t, 10);
@@ -879,14 +891,32 @@ function b(e) {
879
891
  return "�";
880
892
  }
881
893
  }
882
- return r && y[r] || e;
894
+ return r && g[r] || e;
883
895
  });
884
896
  }
885
- var x = class {
897
+ function v(e) {
898
+ let t = e.trim();
899
+ if (!t.startsWith("(") || !t.endsWith(")")) return !1;
900
+ let n = 0, r = null, i = !1;
901
+ for (let e = 0; e < t.length - 1; e++) {
902
+ let a = t[e];
903
+ if (r !== null) {
904
+ i ? i = !1 : a === "\\" ? i = !0 : a === r && (r = null);
905
+ continue;
906
+ }
907
+ if (a === "\"" || a === "'" || a === "`") {
908
+ r = a;
909
+ continue;
910
+ }
911
+ if (a === "(" ? n++ : a === ")" && n--, n === 0) return !1;
912
+ }
913
+ return n === 1;
914
+ }
915
+ var y = class {
886
916
  tokenSource;
887
917
  lookahead = [];
888
918
  constructor(e) {
889
- this.tokenSource = Array.isArray(e) ? new v(e) : e;
919
+ this.tokenSource = Array.isArray(e) ? new h(e) : e;
890
920
  }
891
921
  parse() {
892
922
  return this.lookahead.length = 0, this.parseProgram();
@@ -896,7 +926,7 @@ var x = class {
896
926
  for (; !this.isAtEnd();) t.push(this.parseChild());
897
927
  let n = this.peek().loc.end;
898
928
  return {
899
- type: l.Program,
929
+ type: o.Program,
900
930
  body: t,
901
931
  loc: {
902
932
  start: e,
@@ -906,36 +936,37 @@ var x = class {
906
936
  }
907
937
  parseChild() {
908
938
  let e = this.peek();
909
- if (e.type === o.Comment) return this.advance(), {
910
- type: l.Comment,
939
+ if (e.type === r.Comment) return this.advance(), {
940
+ type: o.Comment,
911
941
  content: e.value,
912
942
  loc: e.loc
913
943
  };
914
- if (e.type === o.Interpolation) return this.advance(), {
915
- type: l.Interpolation,
944
+ if (e.type === r.Interpolation) return this.advance(), {
945
+ type: o.Interpolation,
916
946
  expression: e.value,
917
947
  loc: e.loc
918
948
  };
919
- if (e.type === o.TagOpen) return this.parseElement();
920
- if (e.type === o.DirectiveIf) return this.parseIfDirective();
921
- if (e.type === o.DirectiveFor) return this.parseForDirective();
922
- if (e.type === o.DirectiveSwitch) return this.parseSwitchDirective();
923
- if (e.type === o.Text) return this.advance(), {
924
- type: l.Text,
925
- content: b(e.value),
949
+ if (e.type === r.TagOpen) return this.parseElement();
950
+ if (e.type === r.DirectiveIf) return this.parseIfDirective();
951
+ if (e.type === r.DirectiveFor) return this.parseForDirective();
952
+ if (e.type === r.DirectiveSwitch) return this.parseSwitchDirective();
953
+ if (e.type === r.DirectiveAsync) return this.parseAsyncDirective();
954
+ if (e.type === r.Text) return this.advance(), {
955
+ type: o.Text,
956
+ content: _(e.value),
926
957
  loc: e.loc
927
958
  };
928
- throw e.type === o.TagOpenSlash ? new d(`Unexpected closing tag '</${this.peek(1).value}>' without opening tag`, e.loc.start.line, e.loc.start.column, e.loc.start.offset) : new d(`Unexpected token '${e.value}' of type '${e.type}'`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
959
+ throw e.type === r.TagOpenSlash ? new c(`Unexpected closing tag '</${this.peek(1).value}>' without opening tag`, e.loc.start.line, e.loc.start.column, e.loc.start.offset) : new c(`Unexpected token '${e.value}' of type '${e.type}'`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
929
960
  }
930
961
  parseElement() {
931
- let t = this.consume(o.TagOpen, "Expected opening tag bracket").loc.start, n = this.consume(o.Identifier, "Expected tag name after opening bracket").value, r = [];
932
- for (; !this.check(o.TagClose) && !this.check(o.TagSelfClose) && !this.isAtEnd();) r.push(this.parseAttribute());
933
- if (this.check(o.TagSelfClose)) {
962
+ let t = this.consume(r.TagOpen, "Expected opening tag bracket").loc.start, n = this.consume(r.Identifier, "Expected tag name after opening bracket").value, i = [];
963
+ for (; !this.check(r.TagClose) && !this.check(r.TagSelfClose) && !this.isAtEnd();) i.push(this.parseAttribute());
964
+ if (this.check(r.TagSelfClose)) {
934
965
  let e = this.advance();
935
966
  return {
936
- type: l.Element,
967
+ type: o.Element,
937
968
  tagName: n,
938
- attributes: r,
969
+ attributes: i,
939
970
  children: [],
940
971
  isSelfClosing: !0,
941
972
  loc: {
@@ -944,66 +975,66 @@ var x = class {
944
975
  }
945
976
  };
946
977
  }
947
- let i = this.consume(o.TagClose, "Expected closing bracket after attributes");
978
+ let a = this.consume(r.TagClose, "Expected closing bracket after attributes");
948
979
  if (e.has(n.toLowerCase())) return {
949
- type: l.Element,
980
+ type: o.Element,
950
981
  tagName: n,
951
- attributes: r,
982
+ attributes: i,
952
983
  children: [],
953
984
  isSelfClosing: !0,
954
985
  loc: {
955
986
  start: t,
956
- end: i.loc.end
987
+ end: a.loc.end
957
988
  }
958
989
  };
959
- let a = n.toLowerCase() === "script" || n.toLowerCase() === "style", s = [];
960
- for (; !this.check(o.TagOpenSlash) && !this.isAtEnd();) if (a && this.check(o.Text)) {
990
+ let s = n.toLowerCase() === "script" || n.toLowerCase() === "style", l = [];
991
+ for (; !this.check(r.TagOpenSlash) && !this.isAtEnd();) if (s && this.check(r.Text)) {
961
992
  let e = this.advance();
962
- s.push({
963
- type: l.Text,
993
+ l.push({
994
+ type: o.Text,
964
995
  content: e.value,
965
996
  loc: e.loc
966
997
  });
967
- } else s.push(this.parseChild());
968
- if (this.isAtEnd()) throw new d(`Unclosed element '<${n}>', expected closing tag '</${n}>'`, t.line, t.column, t.offset);
969
- this.consume(o.TagOpenSlash, `Expected closing tag '</${n}>'`);
970
- let c = this.consume(o.Identifier, "Expected closing tag name");
971
- if (c.value !== n && c.value.toLowerCase() !== n.toLowerCase()) throw new d(`Mismatched closing tag. Expected '</${n}>' but got '</${c.value}>'`, c.loc.start.line, c.loc.start.column, c.loc.start.offset);
972
- let u = this.consume(o.TagClose, "Expected '>' after closing tag name");
998
+ } else l.push(this.parseChild());
999
+ if (this.isAtEnd()) throw new c(`Unclosed element '<${n}>', expected closing tag '</${n}>'`, t.line, t.column, t.offset);
1000
+ this.consume(r.TagOpenSlash, `Expected closing tag '</${n}>'`);
1001
+ let u = this.consume(r.Identifier, "Expected closing tag name");
1002
+ if (u.value !== n && u.value.toLowerCase() !== n.toLowerCase()) throw new c(`Mismatched closing tag. Expected '</${n}>' but got '</${u.value}>'`, u.loc.start.line, u.loc.start.column, u.loc.start.offset);
1003
+ let d = this.consume(r.TagClose, "Expected '>' after closing tag name");
973
1004
  return {
974
- type: l.Element,
1005
+ type: o.Element,
975
1006
  tagName: n,
976
- attributes: r,
977
- children: s,
1007
+ attributes: i,
1008
+ children: l,
978
1009
  isSelfClosing: !1,
979
1010
  loc: {
980
1011
  start: t,
981
- end: u.loc.end
1012
+ end: d.loc.end
982
1013
  }
983
1014
  };
984
1015
  }
985
1016
  parseAttribute() {
986
- let e = this.consume(o.Identifier, "Expected attribute name"), t = e.loc.start, n = e.value;
987
- if (this.matchToken(o.Equals)) {
1017
+ let e = this.consume(r.Identifier, "Expected attribute name"), t = e.loc.start, n = e.value;
1018
+ if (this.matchToken(r.Equals)) {
988
1019
  let e = this.peek();
989
- if (e.type === o.StringLiteral) return this.advance(), {
990
- type: l.Attribute,
1020
+ if (e.type === r.StringLiteral) return this.advance(), {
1021
+ type: o.Attribute,
991
1022
  name: n,
992
- value: b(e.value),
1023
+ value: _(e.value),
993
1024
  loc: {
994
1025
  start: t,
995
1026
  end: e.loc.end
996
1027
  }
997
1028
  };
998
- if (e.type === o.Interpolation) {
1029
+ if (e.type === r.Interpolation) {
999
1030
  this.advance();
1000
1031
  let r = {
1001
- type: l.Interpolation,
1032
+ type: o.Interpolation,
1002
1033
  expression: e.value,
1003
1034
  loc: e.loc
1004
1035
  };
1005
1036
  return {
1006
- type: l.Attribute,
1037
+ type: o.Attribute,
1007
1038
  name: n,
1008
1039
  value: r,
1009
1040
  loc: {
@@ -1012,10 +1043,10 @@ var x = class {
1012
1043
  }
1013
1044
  };
1014
1045
  }
1015
- throw new d(`Expected string literal or interpolation after '=' for attribute '${n}'`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1046
+ throw new c(`Expected string literal or interpolation after '=' for attribute '${n}'`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1016
1047
  }
1017
1048
  return {
1018
- type: l.Attribute,
1049
+ type: o.Attribute,
1019
1050
  name: n,
1020
1051
  value: null,
1021
1052
  loc: {
@@ -1025,125 +1056,172 @@ var x = class {
1025
1056
  };
1026
1057
  }
1027
1058
  parseIfDirective() {
1028
- let e = this.consume(o.DirectiveIf, "Expected @if directive"), t = e.loc.start, n = e.value, r = [];
1029
- for (; !this.check(o.BlockClose) && !this.isAtEnd();) r.push(this.parseChild());
1030
- let i = this.consume(o.BlockClose, "Expected closing brace } for @if block"), a = null, s = i.loc.end;
1031
- if (this.skipWhitespaceTokens(), this.check(o.DirectiveElseIf)) a = this.parseElseIfChain(), s = a.loc.end;
1032
- else if (this.check(o.DirectiveElse)) {
1059
+ let e = this.consume(r.DirectiveIf, "Expected @if directive"), t = e.loc.start, n = e.value, i = [];
1060
+ for (; !this.check(r.BlockClose) && !this.isAtEnd();) i.push(this.parseChild());
1061
+ let a = this.consume(r.BlockClose, "Expected closing brace } for @if block"), s = null, c = a.loc.end;
1062
+ if (this.skipWhitespaceTokens(), this.check(r.DirectiveElseIf)) s = this.parseElseIfChain(), c = s.loc.end;
1063
+ else if (this.check(r.DirectiveElse)) {
1033
1064
  this.advance();
1034
1065
  let e = [];
1035
- for (; !this.check(o.BlockClose) && !this.isAtEnd();) e.push(this.parseChild());
1036
- let t = this.consume(o.BlockClose, "Expected closing brace } for @else block");
1037
- a = e, s = t.loc.end;
1066
+ for (; !this.check(r.BlockClose) && !this.isAtEnd();) e.push(this.parseChild());
1067
+ let t = this.consume(r.BlockClose, "Expected closing brace } for @else block");
1068
+ s = e, c = t.loc.end;
1038
1069
  }
1039
1070
  return {
1040
- type: l.If,
1071
+ type: o.If,
1041
1072
  test: n,
1042
- consequent: r,
1043
- alternate: a,
1073
+ consequent: i,
1074
+ alternate: s,
1044
1075
  loc: {
1045
1076
  start: t,
1046
- end: s
1077
+ end: c
1047
1078
  }
1048
1079
  };
1049
1080
  }
1050
1081
  parseElseIfChain() {
1051
- let e = this.consume(o.DirectiveElseIf, "Expected @else if directive"), t = e.loc.start, n = e.value, r = [];
1052
- for (; !this.check(o.BlockClose) && !this.isAtEnd();) r.push(this.parseChild());
1053
- let i = this.consume(o.BlockClose, "Expected closing brace } for @else if block"), a = null, s = i.loc.end;
1054
- if (this.skipWhitespaceTokens(), this.check(o.DirectiveElseIf)) a = this.parseElseIfChain(), s = a.loc.end;
1055
- else if (this.check(o.DirectiveElse)) {
1082
+ let e = this.consume(r.DirectiveElseIf, "Expected @else if directive"), t = e.loc.start, n = e.value, i = [];
1083
+ for (; !this.check(r.BlockClose) && !this.isAtEnd();) i.push(this.parseChild());
1084
+ let a = this.consume(r.BlockClose, "Expected closing brace } for @else if block"), s = null, c = a.loc.end;
1085
+ if (this.skipWhitespaceTokens(), this.check(r.DirectiveElseIf)) s = this.parseElseIfChain(), c = s.loc.end;
1086
+ else if (this.check(r.DirectiveElse)) {
1056
1087
  this.advance();
1057
1088
  let e = [];
1058
- for (; !this.check(o.BlockClose) && !this.isAtEnd();) e.push(this.parseChild());
1059
- let t = this.consume(o.BlockClose, "Expected closing brace } for @else block");
1060
- a = e, s = t.loc.end;
1089
+ for (; !this.check(r.BlockClose) && !this.isAtEnd();) e.push(this.parseChild());
1090
+ let t = this.consume(r.BlockClose, "Expected closing brace } for @else block");
1091
+ s = e, c = t.loc.end;
1061
1092
  }
1062
1093
  return {
1063
- type: l.If,
1094
+ type: o.If,
1064
1095
  test: n,
1065
- consequent: r,
1066
- alternate: a,
1096
+ consequent: i,
1097
+ alternate: s,
1067
1098
  loc: {
1068
1099
  start: t,
1069
- end: s
1100
+ end: c
1070
1101
  }
1071
1102
  };
1072
1103
  }
1073
1104
  parseForDirective() {
1074
- let e = this.consume(o.DirectiveFor, "Expected @for directive"), i = e.loc.start, a = e.value.trim();
1075
- function s(e) {
1076
- return r(e, (t, n) => {
1077
- if (n === "i" && e[t + 1] === "n") {
1078
- let n = t === 0 || !/[a-zA-Z0-9_$]/.test(e[t - 1]), r = t + 2 >= e.length || !/[a-zA-Z0-9_$]/.test(e[t + 2]);
1079
- if (n && r) {
1080
- let n = t - 1;
1081
- for (; n >= 0 && /\s/.test(e[n]);) n--;
1082
- if (n < 0 || e[n] !== ".") return !0;
1083
- }
1105
+ let e = this.consume(r.DirectiveFor, "Expected @for directive"), n = e.loc.start, i = e.value.trim();
1106
+ for (; v(i);) i = i.slice(1, -1).trim();
1107
+ let a = null, s = 0, l = 0, u = 0, d = null;
1108
+ try {
1109
+ let e = t.tokenizer(i, { ecmaVersion: "latest" });
1110
+ for (let n of e) if (n.type === t.tokTypes.parenL) s++;
1111
+ else if (n.type === t.tokTypes.parenR) s = Math.max(0, s - 1);
1112
+ else if (n.type === t.tokTypes.bracketL) l++;
1113
+ else if (n.type === t.tokTypes.bracketR) l = Math.max(0, l - 1);
1114
+ else if (n.type === t.tokTypes.braceL || n.type === t.tokTypes.dollarBraceL) u++;
1115
+ else if (n.type === t.tokTypes.braceR) u = Math.max(0, u - 1);
1116
+ else if (n.type === t.tokTypes._in && s === 0 && l === 0 && u === 0) {
1117
+ a = n;
1118
+ break;
1119
+ }
1120
+ } catch (e) {
1121
+ d = e;
1122
+ }
1123
+ if (!a) throw d ? new c(`Syntax error in @for header: ${d.message}`, e.loc.start.line, e.loc.start.column, e.loc.start.offset) : new c(`Invalid @for header syntax '${e.value.trim()}'. Expected format: 'item in list' or '(item, index) in list'`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1124
+ let f = i.slice(0, a.start).trim();
1125
+ if (f.length === 0) throw new c(`Invalid @for header syntax '${e.value.trim()}'. Expected format: 'item in list' or '(item, index) in list'`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1126
+ let p = "", m = null;
1127
+ if (v(f)) {
1128
+ let n = f.slice(1, -1).trim(), r = [], i = 0, a = 0, o = 0;
1129
+ try {
1130
+ let e = t.tokenizer(n, { ecmaVersion: "latest" });
1131
+ for (let n of e) n.type === t.tokTypes.parenL ? i++ : n.type === t.tokTypes.parenR ? i = Math.max(0, i - 1) : n.type === t.tokTypes.bracketL ? a++ : n.type === t.tokTypes.bracketR ? a = Math.max(0, a - 1) : n.type === t.tokTypes.braceL || n.type === t.tokTypes.dollarBraceL ? o++ : n.type === t.tokTypes.braceR ? o = Math.max(0, o - 1) : n.type === t.tokTypes.comma && i === 0 && a === 0 && o === 0 && r.push(n.start);
1132
+ } catch (t) {
1133
+ throw new c(`Invalid @for target bindings '${f}': ${t.message}`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1134
+ }
1135
+ if (r.length === 0) p = n;
1136
+ else if (r.length === 1) {
1137
+ let e = r[0];
1138
+ p = n.slice(0, e).trim(), m = n.slice(e + 1).trim();
1139
+ } else throw new c("Invalid @for target bindings. Expected at most 2 variables (item, index).", e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1140
+ } else {
1141
+ let n = 0, r = 0, i = 0, a = !1;
1142
+ try {
1143
+ let e = t.tokenizer(f, { ecmaVersion: "latest" });
1144
+ for (let o of e) if (o.type === t.tokTypes.parenL) n++;
1145
+ else if (o.type === t.tokTypes.parenR) n = Math.max(0, n - 1);
1146
+ else if (o.type === t.tokTypes.bracketL) r++;
1147
+ else if (o.type === t.tokTypes.bracketR) r = Math.max(0, r - 1);
1148
+ else if (o.type === t.tokTypes.braceL || o.type === t.tokTypes.dollarBraceL) i++;
1149
+ else if (o.type === t.tokTypes.braceR) i = Math.max(0, i - 1);
1150
+ else if (o.type === t.tokTypes.comma && n === 0 && r === 0 && i === 0) {
1151
+ a = !0;
1152
+ break;
1084
1153
  }
1085
- });
1154
+ } catch {}
1155
+ if (a) throw new c(`Invalid @for target bindings '${f}'. Multiple loop variables must be enclosed in parentheses: '(${f})'.`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1156
+ p = f;
1086
1157
  }
1087
- function c(e) {
1088
- return t(e, ",");
1089
- }
1090
- function u(e) {
1091
- let t = -1;
1092
- return r(e, (n) => {
1093
- if (e.slice(n, n + 3) === "key") {
1094
- let r = n === 0 || !/[a-zA-Z0-9_$]/.test(e[n - 1]), i = n + 3 >= e.length || !/[a-zA-Z0-9_$]/.test(e[n + 3]);
1095
- if (r && i) {
1096
- let r = n - 1;
1097
- for (; r >= 0 && /\s/.test(e[r]);) r--;
1098
- if ((r < 0 || e[r] !== "." && e[r] !== "?") && e.slice(n + 3).trim().length > 0) return t = n, !0;
1099
- }
1158
+ if (p.length === 0) throw new c("Expected item identifier in @for target bindings.", e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1159
+ let h = null;
1160
+ try {
1161
+ let e = t.parse(`let ${p} = 0;`, { ecmaVersion: "latest" });
1162
+ if (!e || !Array.isArray(e.body) || e.body.length !== 1 || e.body[0].type !== "VariableDeclaration" || !Array.isArray(e.body[0].declarations) || e.body[0].declarations.length !== 1) throw Error();
1163
+ h = e.body[0].declarations[0].id;
1164
+ } catch {
1165
+ throw new c(`Invalid @for item binding '${p}'. Expected a valid variable identifier or destructuring pattern.`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1166
+ }
1167
+ if (m !== null) {
1168
+ if (m.length === 0) throw new c("Unexpected trailing comma in @for target bindings. Expected index identifier.", e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1169
+ try {
1170
+ let e = t.parseExpressionAt(m, 0, { ecmaVersion: "latest" });
1171
+ if (e.type !== "Identifier" || m.slice(e.end).trim().length > 0) throw Error();
1172
+ } catch {
1173
+ throw new c(`Invalid @for index binding '${m}'. Array index must be a valid identifier.`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1174
+ }
1175
+ }
1176
+ let g = a.end, _ = i.slice(g), y = g + (_.length - _.trimStart().length), b = "", x = null;
1177
+ try {
1178
+ x = t.parseExpressionAt(i, y, { ecmaVersion: "latest" }), b = i.slice(y, x.end).trim();
1179
+ } catch {
1180
+ throw new c(`Invalid @for iterable expression in '${e.value.trim()}'`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1181
+ }
1182
+ let S = null, C = i.slice(x.end).trim();
1183
+ if (C.length > 0) {
1184
+ let n = null;
1185
+ try {
1186
+ n = t.tokenizer(C, { ecmaVersion: "latest" }).getToken();
1187
+ } catch {}
1188
+ if (n && n.type === t.tokTypes.name && n.value === "key") {
1189
+ let r = n.end, i = C.slice(r).trim();
1190
+ try {
1191
+ let n = t.parseExpressionAt(i, 0, { ecmaVersion: "latest" });
1192
+ S = i.slice(0, n.end).trim();
1193
+ let r = i.slice(n.end).trim();
1194
+ if (r.length > 0) throw new c(`Unexpected token '${r}' after key expression in @for directive.`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1195
+ } catch (t) {
1196
+ throw t instanceof c ? t : new c(`Invalid key expression in @for directive: '${i}'`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1100
1197
  }
1101
- }), t === -1 ? null : {
1102
- index: t,
1103
- keyExpr: e.slice(t + 3).trim()
1104
- };
1198
+ } else throw new c(`Unexpected token '${C}' in @for header. Expected 'key <expression>' or block opening '{'.`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1105
1199
  }
1106
- for (; n(a);) a = a.slice(1, -1).trim();
1107
- let f = s(a);
1108
- if (f === -1 && n(a)) {
1109
- let e = a.slice(1, -1).trim(), t = s(e);
1110
- t !== -1 && (a = e, f = t);
1111
- }
1112
- if (f === -1) throw new d(`Invalid @for header syntax '${a}'. Expected format: 'item in list' or '(item, index) in list'`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1113
- let p = a.slice(0, f).trim(), m = a.slice(f + 2).trim(), h = null, g = u(m);
1114
- g && (h = g.keyExpr, m = m.slice(0, g.index).trim());
1115
- let _ = m, v = p, y = null;
1116
- for (; n(v);) v = v.slice(1, -1).trim();
1117
- let b = c(v);
1118
- if (b !== -1) {
1119
- let e = v.slice(0, b).trim();
1120
- for (y = v.slice(b + 1).trim() || null, v = e; n(v);) v = v.slice(1, -1).trim();
1121
- }
1122
- if (!v || !_) throw new d(`Invalid @for header syntax '${e.value.trim()}'. Expected format: 'item in list' or '(item, index) in list'`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1123
- let x = [];
1124
- for (; !this.check(o.BlockClose) && !this.isAtEnd();) x.push(this.parseChild());
1125
- let S = this.consume(o.BlockClose, "Expected closing brace } for @for block");
1200
+ let w = [];
1201
+ for (; !this.check(r.BlockClose) && !this.isAtEnd();) w.push(this.parseChild());
1202
+ let T = this.consume(r.BlockClose, "Expected closing brace } for @for block");
1126
1203
  return {
1127
- type: l.For,
1128
- item: v,
1129
- index: y,
1130
- iterable: _,
1131
- key: h,
1132
- body: x,
1204
+ type: o.For,
1205
+ item: p,
1206
+ pattern: h,
1207
+ index: m,
1208
+ iterable: b,
1209
+ key: S,
1210
+ body: w,
1133
1211
  loc: {
1134
- start: i,
1135
- end: S.loc.end
1212
+ start: n,
1213
+ end: T.loc.end
1136
1214
  }
1137
1215
  };
1138
1216
  }
1139
1217
  parseSwitchDirective() {
1140
- let e = this.consume(o.DirectiveSwitch, "Expected @switch directive"), t = e.loc.start, n = e.value, r = [];
1141
- for (this.skipWhitespaceTokens(); !this.check(o.BlockClose) && !this.isAtEnd();) {
1142
- if (this.check(o.DirectiveCase)) {
1218
+ let e = this.consume(r.DirectiveSwitch, "Expected @switch directive"), t = e.loc.start, n = e.value, i = [];
1219
+ for (this.skipWhitespaceTokens(); !this.check(r.BlockClose) && !this.isAtEnd();) {
1220
+ if (this.check(r.DirectiveCase)) {
1143
1221
  let e = this.advance(), t = [];
1144
- for (; !this.check(o.BlockClose) && !this.isAtEnd();) t.push(this.parseChild());
1145
- let n = this.consume(o.BlockClose, "Expected closing brace } for @case block");
1146
- r.push({
1222
+ for (; !this.check(r.BlockClose) && !this.isAtEnd();) t.push(this.parseChild());
1223
+ let n = this.consume(r.BlockClose, "Expected closing brace } for @case block");
1224
+ i.push({
1147
1225
  expression: e.value,
1148
1226
  body: t,
1149
1227
  loc: {
@@ -1151,11 +1229,11 @@ var x = class {
1151
1229
  end: n.loc.end
1152
1230
  }
1153
1231
  });
1154
- } else if (this.check(o.DirectiveDefault)) {
1232
+ } else if (this.check(r.DirectiveDefault)) {
1155
1233
  let e = this.advance(), t = [];
1156
- for (; !this.check(o.BlockClose) && !this.isAtEnd();) t.push(this.parseChild());
1157
- let n = this.consume(o.BlockClose, "Expected closing brace } for @default block");
1158
- r.push({
1234
+ for (; !this.check(r.BlockClose) && !this.isAtEnd();) t.push(this.parseChild());
1235
+ let n = this.consume(r.BlockClose, "Expected closing brace } for @default block");
1236
+ i.push({
1159
1237
  expression: null,
1160
1238
  body: t,
1161
1239
  loc: {
@@ -1163,32 +1241,102 @@ var x = class {
1163
1241
  end: n.loc.end
1164
1242
  }
1165
1243
  });
1166
- } else throw new d(`Unexpected token '${this.peek().value}' inside @switch block. Expected @case or @default.`, this.peek().loc.start.line, this.peek().loc.start.column, this.peek().loc.start.offset);
1244
+ } else throw new c(`Unexpected token '${this.peek().value}' inside @switch block. Expected @case or @default.`, this.peek().loc.start.line, this.peek().loc.start.column, this.peek().loc.start.offset);
1167
1245
  this.skipWhitespaceTokens();
1168
1246
  }
1169
- let i = this.consume(o.BlockClose, "Expected closing brace } for @switch block");
1247
+ let a = this.consume(r.BlockClose, "Expected closing brace } for @switch block");
1170
1248
  return {
1171
- type: l.Switch,
1249
+ type: o.Switch,
1172
1250
  discriminant: n,
1173
- cases: r,
1251
+ cases: i,
1174
1252
  loc: {
1175
1253
  start: t,
1176
- end: i.loc.end
1254
+ end: a.loc.end
1255
+ }
1256
+ };
1257
+ }
1258
+ parseAsyncDirective() {
1259
+ let e = this.consume(r.DirectiveAsync, "Expected @async directive"), n = e.loc.start, i = e.value.trim();
1260
+ for (; v(i);) i = i.slice(1, -1).trim();
1261
+ let a = null;
1262
+ try {
1263
+ a = t.parseExpressionAt(i, 0, { ecmaVersion: "latest" });
1264
+ } catch {
1265
+ throw new c(`Invalid @async header syntax '${e.value.trim()}'. Expected format: '@async <promise> as <alias>'`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1266
+ }
1267
+ let s = i.slice(0, a.end).trim(), l = i.slice(a.end).trim(), u = "data", d = null;
1268
+ if (l.length > 0) {
1269
+ let n = null;
1270
+ try {
1271
+ n = t.tokenizer(l, { ecmaVersion: "latest" }).getToken();
1272
+ } catch {}
1273
+ if (n && n.type === t.tokTypes.name && n.value === "as") {
1274
+ let e = l.slice(n.end).trim();
1275
+ for (; v(e);) e = e.slice(1, -1).trim();
1276
+ u = e;
1277
+ try {
1278
+ let n = t.parse(`let ${e} = 0;`, { ecmaVersion: "latest" });
1279
+ n && Array.isArray(n.body) && n.body.length === 1 && n.body[0].type === "VariableDeclaration" && Array.isArray(n.body[0].declarations) && n.body[0].declarations.length === 1 && (d = n.body[0].declarations[0].id);
1280
+ } catch {}
1281
+ } else throw new c(`Unexpected token '${l}' in @async header. Expected 'as <alias>'.`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1282
+ }
1283
+ if (!s || !u) throw new c(`Invalid @async header syntax '${e.value.trim()}'. Expected format: '@async <promise> as <alias>'`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1284
+ let f = [];
1285
+ for (; !this.check(r.BlockClose) && !this.isAtEnd();) f.push(this.parseChild());
1286
+ let p = this.consume(r.BlockClose, "Expected closing brace } for @async block"), m = null, h = null;
1287
+ for (; !this.isAtEnd();) if (this.skipWhitespaceTokens(), this.check(r.DirectiveFallback)) {
1288
+ if (m !== null) {
1289
+ let e = this.peek();
1290
+ throw new c("Duplicate @fallback directive for @async block", e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1291
+ }
1292
+ this.consume(r.DirectiveFallback, "Expected @fallback directive");
1293
+ let e = [];
1294
+ for (; !this.check(r.BlockClose) && !this.isAtEnd();) e.push(this.parseChild());
1295
+ p = this.consume(r.BlockClose, "Expected closing brace } for @fallback block"), m = e;
1296
+ } else if (this.check(r.DirectiveCatch)) {
1297
+ if (h !== null) {
1298
+ let e = this.peek();
1299
+ throw new c("Duplicate @catch directive for @async block", e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1300
+ }
1301
+ let e = this.consume(r.DirectiveCatch, "Expected @catch directive"), t = e.value.trim() || "error";
1302
+ for (; v(t);) t = t.slice(1, -1).trim();
1303
+ let n = [];
1304
+ for (; !this.check(r.BlockClose) && !this.isAtEnd();) n.push(this.parseChild());
1305
+ p = this.consume(r.BlockClose, "Expected closing brace } for @catch block"), h = {
1306
+ errorVar: t,
1307
+ body: n,
1308
+ loc: {
1309
+ start: e.loc.start,
1310
+ end: p.loc.end
1311
+ }
1312
+ };
1313
+ } else break;
1314
+ return {
1315
+ type: o.Async,
1316
+ promise: s,
1317
+ alias: u,
1318
+ aliasAst: d,
1319
+ body: f,
1320
+ fallback: m,
1321
+ catchBranch: h,
1322
+ loc: {
1323
+ start: n,
1324
+ end: p.loc.end
1177
1325
  }
1178
1326
  };
1179
1327
  }
1180
1328
  skipWhitespaceTokens() {
1181
- for (; this.check(o.Text) && this.peek().value.trim().length === 0;) this.advance();
1329
+ for (; this.check(r.Text) && this.peek().value.trim().length === 0;) this.advance();
1182
1330
  }
1183
1331
  isAtEnd() {
1184
- return this.peek().type === o.EOF;
1332
+ return this.peek().type === r.EOF;
1185
1333
  }
1186
1334
  peek(e = 0) {
1187
1335
  return this.ensureLookahead(e), this.lookahead[Math.min(e, this.lookahead.length - 1)];
1188
1336
  }
1189
1337
  advance() {
1190
1338
  let e = this.peek();
1191
- return e.type !== o.EOF && this.lookahead.shift(), e;
1339
+ return e.type !== r.EOF && this.lookahead.shift(), e;
1192
1340
  }
1193
1341
  check(e) {
1194
1342
  return this.peek().type === e;
@@ -1199,18 +1347,18 @@ var x = class {
1199
1347
  consume(e, t) {
1200
1348
  if (this.check(e)) return this.advance();
1201
1349
  let n = this.peek();
1202
- throw new d(t, n.loc.start.line, n.loc.start.column, n.loc.start.offset);
1350
+ throw new c(t, n.loc.start.line, n.loc.start.column, n.loc.start.offset);
1203
1351
  }
1204
1352
  ensureLookahead(e) {
1205
1353
  for (; this.lookahead.length <= e;) {
1206
1354
  let e = this.tokenSource.nextToken();
1207
- if (this.lookahead.push(e), e.type === o.EOF) break;
1355
+ if (this.lookahead.push(e), e.type === r.EOF) break;
1208
1356
  }
1209
1357
  }
1210
1358
  };
1211
1359
  //#endregion
1212
1360
  //#region src/transformer.ts
1213
- function S(e, t) {
1361
+ function b(e, t) {
1214
1362
  function n(e, t) {
1215
1363
  let n = [];
1216
1364
  for (let i of e) {
@@ -1229,16 +1377,16 @@ function S(e, t) {
1229
1377
  a = e;
1230
1378
  }
1231
1379
  }
1232
- let o = t[a.type];
1233
- if (o) {
1234
- let e = o(a, i);
1380
+ let s = t[a.type];
1381
+ if (s) {
1382
+ let e = s(a, i);
1235
1383
  if (e !== void 0) {
1236
1384
  if (e === null || Array.isArray(e)) return e;
1237
1385
  a = e;
1238
1386
  }
1239
1387
  }
1240
1388
  switch (a.type) {
1241
- case l.Program: {
1389
+ case o.Program: {
1242
1390
  let e = n(a.body, a);
1243
1391
  a = {
1244
1392
  ...a,
@@ -1246,7 +1394,7 @@ function S(e, t) {
1246
1394
  };
1247
1395
  break;
1248
1396
  }
1249
- case l.Element: {
1397
+ case o.Element: {
1250
1398
  let e = a.attributes;
1251
1399
  if (t.Attribute && Array.isArray(a.attributes)) {
1252
1400
  let n = [];
@@ -1264,7 +1412,7 @@ function S(e, t) {
1264
1412
  };
1265
1413
  break;
1266
1414
  }
1267
- case l.If: {
1415
+ case o.If: {
1268
1416
  let e = n(a.consequent, a), t = a.alternate;
1269
1417
  Array.isArray(a.alternate) ? t = n(a.alternate, a) : a.alternate !== null && typeof a.alternate == "object" && (t = r(a.alternate, a)), a = {
1270
1418
  ...a,
@@ -1273,7 +1421,7 @@ function S(e, t) {
1273
1421
  };
1274
1422
  break;
1275
1423
  }
1276
- case l.For: {
1424
+ case o.For: {
1277
1425
  let e = n(a.body, a);
1278
1426
  a = {
1279
1427
  ...a,
@@ -1281,7 +1429,7 @@ function S(e, t) {
1281
1429
  };
1282
1430
  break;
1283
1431
  }
1284
- case l.Switch:
1432
+ case o.Switch:
1285
1433
  if (Array.isArray(a.cases)) {
1286
1434
  let e = a.cases.map((e) => ({
1287
1435
  ...e,
@@ -1293,6 +1441,19 @@ function S(e, t) {
1293
1441
  };
1294
1442
  }
1295
1443
  break;
1444
+ case o.Async: {
1445
+ let e = n(a.body, a), t = a.fallback ? n(a.fallback, a) : null, r = a.catchBranch;
1446
+ a.catchBranch && (r = {
1447
+ ...a.catchBranch,
1448
+ body: n(a.catchBranch.body, a)
1449
+ }), a = {
1450
+ ...a,
1451
+ body: e,
1452
+ fallback: t,
1453
+ catchBranch: r
1454
+ };
1455
+ break;
1456
+ }
1296
1457
  }
1297
1458
  if (t.leave) {
1298
1459
  let e = t.leave(a, i);
@@ -1302,22 +1463,21 @@ function S(e, t) {
1302
1463
  }
1303
1464
  return r(e, null);
1304
1465
  }
1305
- var C = class {
1466
+ var x = class {
1306
1467
  rawAst;
1307
- switchCounter = 0;
1308
1468
  constructor(e) {
1309
1469
  this.rawAst = e;
1310
1470
  }
1311
1471
  transform() {
1312
- return S(S(S(this.rawAst, { Text: (e) => {
1472
+ return b(b(this.rawAst, { Text: (e) => {
1313
1473
  if (typeof e.content == "string" && this.isWhitespaceOnly(e.content)) return null;
1314
- } }), { Switch: (e) => this.transformSwitchToIfChain(e) }), {
1474
+ } }), {
1315
1475
  Interpolation: (e) => this.transformInterpolation(e),
1316
1476
  Element: (e) => {
1317
1477
  if (e.tagName === "script") return this.transformScriptElement(e);
1318
1478
  if (e.attributes.some((e) => e.value !== null && typeof e.value != "string")) return {
1319
1479
  ...e,
1320
- attributes: e.attributes.map((e) => e.type === l.Attribute && e.value !== null && typeof e.value != "string" && e.value.type === l.Interpolation ? {
1480
+ attributes: e.attributes.map((e) => e.type === o.Attribute && e.value !== null && typeof e.value != "string" && e.value.type === o.Interpolation ? {
1321
1481
  ...e,
1322
1482
  value: this.transformInterpolation(e.value)
1323
1483
  } : e)
@@ -1326,141 +1486,81 @@ var C = class {
1326
1486
  If: (e) => {
1327
1487
  if (typeof e.test == "string" && e.test.trim().length > 0) return {
1328
1488
  ...e,
1329
- test: i.parseExpressionAt(e.test, 0, { ecmaVersion: "latest" })
1489
+ test: t.parseExpressionAt(e.test, 0, { ecmaVersion: "latest" })
1330
1490
  };
1331
1491
  },
1332
1492
  For: (e) => ({
1333
1493
  ...e,
1334
- iterable: typeof e.iterable == "string" ? i.parseExpressionAt(e.iterable, 0, { ecmaVersion: "latest" }) : e.iterable,
1335
- key: typeof e.key == "string" && e.key.trim().length > 0 ? i.parseExpressionAt(e.key, 0, { ecmaVersion: "latest" }) : e.key ?? null
1336
- })
1337
- });
1338
- }
1339
- transformSwitchToIfChain(e) {
1340
- let t = typeof e.discriminant == "string" ? i.parseExpressionAt(e.discriminant, 0, { ecmaVersion: "latest" }) : e.discriminant, n = t.type === "Identifier" || t.type === "Literal" || t.type === "MemberExpression", r = `__drift_sw_${this.switchCounter++}`, a = !0, o = (s) => {
1341
- let c = e.cases[s];
1342
- if (!c) return null;
1343
- if (c.expression === null) {
1344
- let e = [...c.body], t = o(s + 1), n = null;
1345
- return (Array.isArray(t) || t !== null && t.type === l.If) && (n = t), s > 0 && n === null ? e : {
1346
- type: l.If,
1347
- test: {
1348
- type: "Literal",
1349
- value: !0,
1350
- raw: "true",
1351
- start: 0,
1352
- end: 0
1353
- },
1354
- consequent: e,
1355
- alternate: n,
1356
- loc: c.loc
1494
+ iterable: typeof e.iterable == "string" ? t.parseExpressionAt(e.iterable, 0, { ecmaVersion: "latest" }) : e.iterable,
1495
+ key: typeof e.key == "string" && e.key.trim().length > 0 ? t.parseExpressionAt(e.key, 0, { ecmaVersion: "latest" }) : e.key ?? null
1496
+ }),
1497
+ Switch: (e) => {
1498
+ let n = typeof e.discriminant == "string" && e.discriminant.trim().length > 0 ? t.parseExpressionAt(e.discriminant, 0, { ecmaVersion: "latest" }) : e.discriminant, r = e.cases.map((e) => {
1499
+ let n = e.expression !== null && typeof e.expression == "string" && e.expression.trim().length > 0 ? t.parseExpressionAt(e.expression, 0, { ecmaVersion: "latest" }) : e.expression;
1500
+ return {
1501
+ ...e,
1502
+ expression: n
1503
+ };
1504
+ });
1505
+ return {
1506
+ ...e,
1507
+ discriminant: n,
1508
+ cases: r
1357
1509
  };
1358
- }
1359
- let u = typeof c.expression == "string" && c.expression.trim().length > 0 ? i.parseExpressionAt(c.expression, 0, { ecmaVersion: "latest" }) : c.expression, d;
1360
- n ? d = structuredClone(t) : a ? (a = !1, d = {
1361
- type: "AssignmentExpression",
1362
- operator: "=",
1363
- left: {
1364
- type: "Identifier",
1365
- name: r,
1366
- start: 0,
1367
- end: 0
1368
- },
1369
- right: structuredClone(t),
1370
- start: 0,
1371
- end: 0
1372
- }) : d = {
1373
- type: "Identifier",
1374
- name: r,
1375
- start: 0,
1376
- end: 0
1377
- };
1378
- let f = {
1379
- type: "BinaryExpression",
1380
- operator: "===",
1381
- left: d,
1382
- right: u,
1383
- start: 0,
1384
- end: 0
1385
- }, p = [...c.body], m = o(s + 1), h = null;
1386
- return (Array.isArray(m) || m !== null && m.type === l.If) && (h = m), {
1387
- type: l.If,
1388
- test: f,
1389
- consequent: p,
1390
- alternate: h,
1391
- extraDeps: structuredClone(t),
1392
- loc: c.loc
1393
- };
1394
- }, s = o(0);
1395
- return s ? Array.isArray(s) ? s.length === 0 ? {
1396
- type: l.Comment,
1397
- content: "empty switch",
1398
- loc: e.loc
1399
- } : {
1400
- type: l.If,
1401
- test: {
1402
- type: "Literal",
1403
- value: !0,
1404
- raw: "true",
1405
- start: 0,
1406
- end: 0
1407
1510
  },
1408
- consequent: s,
1409
- alternate: null,
1410
- loc: e.loc
1411
- } : s : {
1412
- type: l.Comment,
1413
- content: "empty switch",
1414
- loc: e.loc
1415
- };
1511
+ Async: (e) => ({
1512
+ ...e,
1513
+ promise: typeof e.promise == "string" && e.promise.trim().length > 0 ? t.parseExpressionAt(e.promise, 0, { ecmaVersion: "latest" }) : e.promise
1514
+ })
1515
+ });
1416
1516
  }
1417
1517
  transformInterpolation(e) {
1418
1518
  if (typeof e.expression != "string") return e;
1419
- let t;
1519
+ let n;
1420
1520
  try {
1421
- t = i.parseExpressionAt(e.expression, 0, {
1521
+ n = t.parseExpressionAt(e.expression, 0, {
1422
1522
  ecmaVersion: "latest",
1423
1523
  allowAwaitOutsideFunction: !0
1424
1524
  });
1425
1525
  } catch (t) {
1426
- throw new d(`Failed to parse JS expression in interpolation: ${t instanceof Error ? t.message : String(t)}`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1526
+ throw new c(`Failed to parse JS expression in interpolation: ${t instanceof Error ? t.message : String(t)}`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1427
1527
  }
1428
1528
  return {
1429
1529
  ...e,
1430
- expression: t
1530
+ expression: n
1431
1531
  };
1432
1532
  }
1433
1533
  transformScriptElement(e) {
1434
- let t = e.children.map((e) => {
1435
- if (e.type === l.Text && typeof e.content == "string") {
1436
- let t;
1534
+ let n = e.children.map((e) => {
1535
+ if (e.type === o.Text && typeof e.content == "string") {
1536
+ let n;
1437
1537
  try {
1438
- let n = i.parse(e.content, {
1538
+ let r = t.parse(e.content, {
1439
1539
  ecmaVersion: "latest",
1440
1540
  sourceType: "module",
1441
1541
  allowAwaitOutsideFunction: !0,
1442
1542
  allowReturnOutsideFunction: !0
1443
1543
  });
1444
- t = n.body.length === 1 && n.body[0] !== void 0 ? n.body[0] : n.body;
1544
+ n = r.body.length === 1 && r.body[0] !== void 0 ? r.body[0] : r.body;
1445
1545
  } catch (t) {
1446
- throw new d(`Failed to parse script tag JS content: ${t instanceof Error ? t.message : String(t)}`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1546
+ throw new c(`Failed to parse script tag JS content: ${t instanceof Error ? t.message : String(t)}`, e.loc.start.line, e.loc.start.column, e.loc.start.offset);
1447
1547
  }
1448
1548
  return {
1449
1549
  ...e,
1450
- content: t
1550
+ content: n
1451
1551
  };
1452
1552
  }
1453
1553
  return e;
1454
1554
  });
1455
1555
  return {
1456
1556
  ...e,
1457
- children: t
1557
+ children: n
1458
1558
  };
1459
1559
  }
1460
1560
  isWhitespaceOnly(e) {
1461
1561
  return e.length > 0 && /^\s*$/.test(e);
1462
1562
  }
1463
- }, w = class {
1563
+ }, S = class {
1464
1564
  ast;
1465
1565
  bytecode = [];
1466
1566
  constants = [];
@@ -1478,7 +1578,7 @@ var C = class {
1478
1578
  generate() {
1479
1579
  if (this.bytecode = [], this.constants = [], this.nextRegisterId = 0, this.declaredVars = /* @__PURE__ */ new Set(), this.imports = [], this.bindingPositions = /* @__PURE__ */ new Map(), this.derivedBindings = [], this.pendingDerived = [], this.effectBindings = [], this.pendingEffects = [], this.collectDeclaredVars(this.ast.body), this.processDerivedBindings(), this.processEffectBindings(), this.ast.body.length === 0) {
1480
1580
  let e = this.allocRegister();
1481
- return this.emit(f.CREATE_FRAGMENT, e), {
1581
+ return this.emit(l.CREATE_FRAGMENT, e), {
1482
1582
  bytecode: this.bytecode,
1483
1583
  constants: this.constants,
1484
1584
  reactiveBindings: this.buildReactiveBindings(),
@@ -1488,12 +1588,12 @@ var C = class {
1488
1588
  imports: this.imports
1489
1589
  };
1490
1590
  }
1491
- if (this.ast.body.length === 1 && this.ast.body[0]?.type === l.Element) {
1591
+ if (this.ast.body.length === 1 && this.ast.body[0]?.type === o.Element) {
1492
1592
  let e = this.allocRegister();
1493
1593
  this.compileElement(this.ast.body[0], e);
1494
1594
  } else {
1495
1595
  let e = this.allocRegister();
1496
- this.emit(f.CREATE_FRAGMENT, e);
1596
+ this.emit(l.CREATE_FRAGMENT, e);
1497
1597
  for (let t of this.ast.body) this.compileNode(t, e);
1498
1598
  }
1499
1599
  return {
@@ -1508,24 +1608,30 @@ var C = class {
1508
1608
  }
1509
1609
  compileNode(e, t) {
1510
1610
  switch (e.type) {
1511
- case l.Element:
1611
+ case o.Element:
1512
1612
  e.tagName === "script" ? this.compileScriptElement(e) : this.compileElementNode(e, t);
1513
1613
  break;
1514
- case l.Text:
1614
+ case o.Text:
1515
1615
  this.compileTextNode(e, t);
1516
1616
  break;
1517
- case l.Interpolation:
1617
+ case o.Interpolation:
1518
1618
  this.compileInterpolationNode(e, t);
1519
1619
  break;
1520
- case l.Comment:
1620
+ case o.Comment:
1521
1621
  this.compileCommentNode(e, t);
1522
1622
  break;
1523
- case l.If:
1623
+ case o.If:
1524
1624
  this.compileIfNode(e, t);
1525
1625
  break;
1526
- case l.For:
1626
+ case o.Switch:
1627
+ this.compileSwitchNode(e, t);
1628
+ break;
1629
+ case o.For:
1527
1630
  this.compileForNode(e, t);
1528
1631
  break;
1632
+ case o.Async:
1633
+ this.compileAsyncNode(e, t);
1634
+ break;
1529
1635
  }
1530
1636
  }
1531
1637
  filterRuntimeScriptAst(e) {
@@ -1548,11 +1654,11 @@ var C = class {
1548
1654
  return e;
1549
1655
  }
1550
1656
  compileScriptElement(e) {
1551
- for (let t of e.children) if (t.type === l.Text && typeof t.content == "object" && t.content !== null) {
1657
+ for (let t of e.children) if (t.type === o.Text && typeof t.content == "object" && t.content !== null) {
1552
1658
  let e = this.filterRuntimeScriptAst(t.content);
1553
1659
  if (e !== null && (!Array.isArray(e) || e.length > 0)) {
1554
- let t = this.addConstant(e);
1555
- this.emit(f.EXEC_SCRIPT, t);
1660
+ let t = this.addScriptConstant(e);
1661
+ this.emit(l.EXEC_SCRIPT, t);
1556
1662
  }
1557
1663
  }
1558
1664
  }
@@ -1565,66 +1671,66 @@ var C = class {
1565
1671
  let n = this.isComponentTag(e.tagName), r = this.addConstant(e.tagName);
1566
1672
  if (n) {
1567
1673
  let n = { __drift_props__: !0 };
1568
- for (let t of e.attributes) if (t.type === l.Attribute) {
1674
+ for (let t of e.attributes) if (t.type === o.Attribute) {
1569
1675
  if (t.value === null) n[t.name] = !0;
1570
1676
  else if (typeof t.value == "string") n[t.name] = t.value;
1571
- else if (t.value.type === l.Interpolation) {
1677
+ else if (t.value.type === o.Interpolation) {
1572
1678
  let e = t.value.expression;
1573
1679
  if (e && typeof e == "object" && e.type) {
1574
- let r = A(e);
1575
- n[t.name] = { __drift_fn__: `(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, _get) => (${r})` };
1680
+ let r = k(e);
1681
+ n[t.name] = { __drift_fn__: `(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, getScopeValue) => (${r})` };
1576
1682
  } else n[t.name] = e;
1577
1683
  }
1578
1684
  }
1579
1685
  let i = null;
1580
1686
  if (e.children && e.children.length > 0) {
1581
- let t = e.children.filter((e) => !(e.type === l.Text && typeof e.content == "string" && e.content.trim() === ""));
1687
+ let t = e.children.filter((e) => !(e.type === o.Text && typeof e.content == "string" && e.content.trim() === ""));
1582
1688
  t.length > 0 && (i = this.compileNodesToSubModule(t), n.__drift_children__ = this.addConstant(i));
1583
1689
  }
1584
- let a = Object.keys(n).length > 1 ? this.addConstant(n) : 255, o = this.bytecode.length;
1585
- this.emit(f.MOUNT_COMPONENT, t, r, a), this.emit(f.RETURN);
1586
- for (let t of e.attributes) t.type === l.Attribute && t.value !== null && typeof t.value != "string" && t.value.type === l.Interpolation && this.recordBindingPositions(t.value.expression, o);
1587
- if (i) for (let e of i.reactiveBindings) this.declaredVars.has(e.variable) && (this.bindingPositions.has(e.variable) || this.bindingPositions.set(e.variable, []), this.bindingPositions.get(e.variable).push(o));
1690
+ let a = Object.keys(n).length > 1 ? this.addConstant(n) : 255, s = this.bytecode.length;
1691
+ this.emit(l.MOUNT_COMPONENT, t, r, a), this.emit(l.RETURN);
1692
+ for (let t of e.attributes) t.type === o.Attribute && t.value !== null && typeof t.value != "string" && t.value.type === o.Interpolation && this.recordBindingPositions(t.value.expression, s);
1693
+ if (i) for (let e of i.reactiveBindings) this.declaredVars.has(e.variable) && (this.bindingPositions.has(e.variable) || this.bindingPositions.set(e.variable, []), this.bindingPositions.get(e.variable).push(s));
1588
1694
  } else {
1589
- this.emit(f.CREATE_ELEMENT, t, r);
1695
+ this.emit(l.CREATE_ELEMENT, t, r);
1590
1696
  for (let n of e.attributes) this.compileAttributeNode(n, t);
1591
1697
  for (let n of e.children) this.compileNode(n, t);
1592
1698
  }
1593
1699
  }
1594
1700
  compileElementNode(e, t) {
1595
1701
  let n = this.allocRegister();
1596
- this.compileElement(e, n), this.emit(f.APPEND_CHILD, t, n);
1702
+ this.compileElement(e, n), this.emit(l.APPEND_CHILD, t, n);
1597
1703
  }
1598
1704
  compileAttributeNode(e, t) {
1599
1705
  let n = this.addConstant(e.name);
1600
1706
  if (e.value === null) {
1601
1707
  let e = this.addConstant(!0);
1602
- this.emit(f.SET_ATTR, t, n, e, 0);
1708
+ this.emit(l.SET_ATTR, t, n, e, 0);
1603
1709
  } else if (typeof e.value == "string") {
1604
1710
  let r = this.addConstant(e.value);
1605
- this.emit(f.SET_ATTR, t, n, r, 0);
1606
- } else if (e.value.type === l.Interpolation) {
1607
- let r = this.addConstant(e.value.expression), i = this.bytecode.length;
1608
- this.emit(f.SET_ATTR, t, n, r, 1), this.emit(f.RETURN), this.recordBindingPositions(e.value.expression, i);
1711
+ this.emit(l.SET_ATTR, t, n, r, 0);
1712
+ } else if (e.value.type === o.Interpolation) {
1713
+ let r = this.addExpressionConstant(e.value.expression), i = this.bytecode.length;
1714
+ this.emit(l.SET_ATTR, t, n, r, 1), this.emit(l.RETURN), this.recordBindingPositions(e.value.expression, i);
1609
1715
  }
1610
1716
  }
1611
1717
  compileTextNode(e, t) {
1612
1718
  let n = this.allocRegister(), r = this.addConstant(e.content);
1613
- this.emit(f.CREATE_TEXT, n, r), this.emit(f.APPEND_CHILD, t, n);
1719
+ this.emit(l.CREATE_TEXT, n, r), this.emit(l.APPEND_CHILD, t, n);
1614
1720
  }
1615
1721
  compileInterpolationNode(e, t) {
1616
- let n = this.allocRegister(), r = this.addConstant(e.expression), i = this.bytecode.length;
1617
- this.emit(f.INTERPOLATE_TEXT, n, r), this.emit(f.RETURN), this.emit(f.APPEND_CHILD, t, n), this.recordBindingPositions(e.expression, i);
1722
+ let n = this.allocRegister(), r = this.addExpressionConstant(e.expression), i = this.bytecode.length;
1723
+ this.emit(l.INTERPOLATE_TEXT, n, r), this.emit(l.RETURN), this.emit(l.APPEND_CHILD, t, n), this.recordBindingPositions(e.expression, i);
1618
1724
  }
1619
1725
  compileCommentNode(e, t) {
1620
1726
  let n = this.allocRegister(), r = this.addConstant(e.content);
1621
- this.emit(f.CREATE_COMMENT, n, r), this.emit(f.APPEND_CHILD, t, n);
1727
+ this.emit(l.CREATE_COMMENT, n, r), this.emit(l.APPEND_CHILD, t, n);
1622
1728
  }
1623
1729
  compileNodesToSubModule(e) {
1624
1730
  let t = this.bytecode, n = this.constants, r = this.nextRegisterId, i = this.bindingPositions;
1625
1731
  this.bytecode = [], this.constants = [], this.nextRegisterId = 0, this.bindingPositions = /* @__PURE__ */ new Map();
1626
1732
  let a = this.allocRegister();
1627
- this.emit(f.CREATE_FRAGMENT, a);
1733
+ this.emit(l.CREATE_FRAGMENT, a);
1628
1734
  for (let t of e) this.compileNode(t, a);
1629
1735
  let o = {
1630
1736
  bytecode: this.bytecode,
@@ -1652,16 +1758,69 @@ var C = class {
1652
1758
  n.constants && !n.constants.some((e) => Array.isArray(e)) && n.constants.push(t);
1653
1759
  }
1654
1760
  if (a) for (let e of this.collectDepsFromSubModule(a)) o.add(e);
1655
- let s = this.addConstant(Array.from(o)), c = this.addConstant(e.test);
1656
- this.emit(f.REACTIVE_IF, t, c, r, i, s);
1761
+ let s = this.addConstant(Array.from(o)), c = this.addExpressionConstant(e.test);
1762
+ this.emit(l.REACTIVE_IF, t, c, r, i, s);
1763
+ }
1764
+ compileSwitchNode(e, t) {
1765
+ let n = this.addExpressionConstant(e.discriminant), r = [], i = 255, a = [];
1766
+ for (let t of e.cases) if (t.expression === null) {
1767
+ let e = this.compileNodesToSubModule(t.body);
1768
+ a.push(e), i = this.addConstant(e);
1769
+ } else {
1770
+ let e = this.addExpressionConstant(t.expression), n = this.compileNodesToSubModule(t.body);
1771
+ a.push(n);
1772
+ let i = this.addConstant(n);
1773
+ r.push({
1774
+ testIdx: e,
1775
+ modIdx: i
1776
+ });
1777
+ }
1778
+ let o = this.addConstant(r), s = /* @__PURE__ */ new Set();
1779
+ for (let t of this.extractIdentifiers(e.discriminant)) this.declaredVars.has(t) && s.add(t);
1780
+ for (let t of e.cases) if (t.expression !== null) for (let e of this.extractIdentifiers(t.expression)) this.declaredVars.has(e) && s.add(e);
1781
+ for (let e of a) for (let t of this.collectDepsFromSubModule(e)) s.add(t);
1782
+ let c = this.addConstant(Array.from(s));
1783
+ this.emit(l.REACTIVE_SWITCH, t, n, o, i, c);
1657
1784
  }
1658
1785
  compileForNode(e, t) {
1659
- let n = this.compileNodesToSubModule(e.body), r = this.addConstant(n), i = this.addConstant(e.iterable), a = this.addConstant(e.item), o = e.index === null ? 255 : this.addConstant(e.index), s = e.key ? this.addConstant(e.key) : 255, c = this.collectDepsFromSubModule(n, e.iterable), l = this.addConstant(c);
1660
- this.emit(f.REACTIVE_FOR, t, i, a, o, s, r, l);
1786
+ let n = /* @__PURE__ */ new Set();
1787
+ if (e.pattern) for (let t of w(e.pattern)) n.add(t);
1788
+ else e.item && n.add(e.item);
1789
+ e.index !== null && n.add(e.index);
1790
+ let r = [];
1791
+ for (let e of n) this.declaredVars.has(e) || (this.declaredVars.add(e), r.push(e));
1792
+ let i = this.compileNodesToSubModule(e.body);
1793
+ for (let e of r) this.declaredVars.delete(e);
1794
+ let a = this.addConstant(i), o = this.addExpressionConstant(e.iterable), s = this.addConstant(e.item), c = e.index === null ? 255 : this.addConstant(e.index), u = e.key ? this.addExpressionConstant(e.key) : 255, d = 255;
1795
+ e.pattern && (d = this.addConstant(O(e.pattern, e.index)));
1796
+ let f = /* @__PURE__ */ new Set();
1797
+ for (let t of this.extractIdentifiers(e.iterable)) this.declaredVars.has(t) && f.add(t);
1798
+ if (e.key) for (let t of this.extractIdentifiers(e.key)) this.declaredVars.has(t) && f.add(t);
1799
+ let p = this.addConstant([...f]), m = /* @__PURE__ */ new Set();
1800
+ for (let e of i.reactiveBindings) !f.has(e.variable) && !n.has(e.variable) && m.add(e.variable);
1801
+ let h = this.addConstant([...m]), g = [...f, ...m], _ = this.addConstant(g);
1802
+ this.emit(l.REACTIVE_FOR, t, o, s, c, u, a, _, p, h, d);
1803
+ }
1804
+ compileAsyncNode(e, t) {
1805
+ let n = this.compileNodesToSubModule(e.body), r = this.addConstant(n), i = e.fallback ? this.compileNodesToSubModule(e.fallback) : null, a = i ? this.addConstant(i) : 255, o = 255;
1806
+ if (e.catchBranch) {
1807
+ let t = this.compileNodesToSubModule(e.catchBranch.body);
1808
+ o = this.addConstant({
1809
+ errorVar: e.catchBranch.errorVar,
1810
+ module: t
1811
+ });
1812
+ }
1813
+ let s = this.addExpressionConstant(e.promise), c = this.addConstant(e.alias), u = 255;
1814
+ e.aliasAst && (u = this.addConstant(O(e.aliasAst, null)));
1815
+ let d = /* @__PURE__ */ new Set();
1816
+ for (let t of this.collectDepsFromSubModule(n, e.promise)) d.add(t);
1817
+ if (i) for (let e of this.collectDepsFromSubModule(i)) d.add(e);
1818
+ let f = this.addConstant(Array.from(d));
1819
+ this.emit(l.REACTIVE_ASYNC, t, s, c, r, a, o, f, u);
1661
1820
  }
1662
1821
  collectDeclaredVars(e) {
1663
- for (let t of e) if (t.type === l.Element && t.tagName === "script") {
1664
- for (let e of t.children) if (e.type === l.Text && typeof e.content == "object" && e.content !== null) {
1822
+ for (let t of e) if (t.type === o.Element && t.tagName === "script") {
1823
+ for (let e of t.children) if (e.type === o.Text && typeof e.content == "object" && e.content !== null) {
1665
1824
  let t = e.content;
1666
1825
  if (Array.isArray(t)) for (let e of t) this.extractVarNames(e);
1667
1826
  else this.extractVarNames(t);
@@ -1671,7 +1830,7 @@ var C = class {
1671
1830
  extractVarNames(e) {
1672
1831
  if (!(!e || typeof e != "object")) {
1673
1832
  if (e.type === "VariableDeclaration") for (let t of e.declarations) {
1674
- for (let e of E(t.id)) this.declaredVars.add(e);
1833
+ for (let e of w(t.id)) this.declaredVars.add(e);
1675
1834
  t.id?.type === "Identifier" && t.init?.type === "CallExpression" && t.init.callee?.type === "Identifier" && t.init.callee.name === "derive" && t.init.arguments && t.init.arguments.length > 0 && this.pendingDerived.push({
1676
1835
  name: t.id.name,
1677
1836
  arg: t.init.arguments[0]
@@ -1706,8 +1865,8 @@ var C = class {
1706
1865
  for (let e of this.pendingDerived) {
1707
1866
  let { name: t, arg: n } = e, r = n, i = !1;
1708
1867
  (n.type === "ArrowFunctionExpression" || n.type === "FunctionExpression") && (n.body?.type === "BlockStatement" && (i = !0), r = n.body);
1709
- let a = [...this.extractIdentifiers(r)].filter((e) => e !== t && this.declaredVars.has(e)), o = A(r), s;
1710
- s = i ? { __drift_fn__: `(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, _get) => ${o}` } : { __drift_fn__: `(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, _get) => (${o})` };
1868
+ let a = [...this.extractIdentifiers(r)].filter((e) => e !== t && this.declaredVars.has(e)), o = k(r), s;
1869
+ s = i ? { __drift_fn__: `(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, getScopeValue) => ${o}` } : { __drift_fn__: `(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, getScopeValue) => (${o})` };
1711
1870
  let c = this.addConstant(s);
1712
1871
  this.derivedBindings.push({
1713
1872
  name: t,
@@ -1720,8 +1879,8 @@ var C = class {
1720
1879
  for (let e of this.pendingEffects) {
1721
1880
  let { arg: t } = e, n = t, r = !1, i = !!t?.async;
1722
1881
  (t.type === "ArrowFunctionExpression" || t.type === "FunctionExpression") && (t.body?.type === "BlockStatement" && (r = !0), n = t.body);
1723
- let a = [...this.extractIdentifiers(n)].filter((e) => this.declaredVars.has(e)), o = A(n), s = i ? "async " : "", c;
1724
- c = r ? { __drift_fn__: `${s}(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, _get) => ${o}` } : { __drift_fn__: `${s}(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, _get) => (${o})` };
1882
+ let a = [...this.extractIdentifiers(n)].filter((e) => this.declaredVars.has(e)), o = k(n), s = i ? "async " : "", c;
1883
+ c = r ? { __drift_fn__: `${s}(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, getScopeValue) => ${o}` } : { __drift_fn__: `${s}(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, getScopeValue) => (${o})` };
1725
1884
  let l = this.addConstant(c);
1726
1885
  this.effectBindings.push({
1727
1886
  deps: a,
@@ -1733,7 +1892,7 @@ var C = class {
1733
1892
  let t = /* @__PURE__ */ new Set();
1734
1893
  if (!e || typeof e != "object") return t;
1735
1894
  try {
1736
- a.ancestor(e, { Identifier(e, n) {
1895
+ n.ancestor(e, { Identifier(e, n) {
1737
1896
  let r = n[n.length - 2];
1738
1897
  r && (r.type === "MemberExpression" && r.property === e && !r.computed || (r.type === "Property" || r.type === "MethodDefinition" || r.type === "PropertyDefinition") && r.key === e && !r.computed) || t.add(e.name);
1739
1898
  } });
@@ -1756,8 +1915,20 @@ var C = class {
1756
1915
  allocRegister() {
1757
1916
  return this.nextRegisterId++;
1758
1917
  }
1918
+ addExpressionConstant(e) {
1919
+ let t = k(e), n = [];
1920
+ for (let t of this.extractIdentifiers(e)) this.declaredVars.has(t) && n.push(t);
1921
+ let r = {
1922
+ __drift_fn__: `(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, getScopeValue) => (${t})`,
1923
+ deps: n
1924
+ };
1925
+ return this.addConstant(r);
1926
+ }
1927
+ addScriptConstant(e) {
1928
+ let t = { __drift_fn__: `(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, getScopeValue) => { ${k(e)}; }` };
1929
+ return this.addConstant(t);
1930
+ }
1759
1931
  addConstant(e) {
1760
- e && typeof e == "object" && e.type && typeof e.type == "string" ? e = { __drift_fn__: `(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, _get) => (${A(e)})` } : Array.isArray(e) && e.length > 0 && typeof e[0] == "object" && e[0]?.type && (e = { __drift_fn__: `(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, _get) => { ${A(e)}; }` });
1761
1932
  let t = this.constants.findIndex((t) => this.isConstantEqual(t, e));
1762
1933
  return t === -1 ? (this.constants.push(e), this.constants.length - 1) : t;
1763
1934
  }
@@ -1768,10 +1939,10 @@ var C = class {
1768
1939
  this.bytecode.push(e, ...t);
1769
1940
  }
1770
1941
  };
1771
- function T(e) {
1772
- return !e || typeof e != "object" ? null : e.type === "Identifier" ? e.name : e.type === "MemberExpression" ? T(e.object) : null;
1942
+ function C(e) {
1943
+ return !e || typeof e != "object" ? null : e.type === "Identifier" ? e.name : e.type === "MemberExpression" ? C(e.object) : null;
1773
1944
  }
1774
- function E(e) {
1945
+ function w(e) {
1775
1946
  let t = [];
1776
1947
  function n(e) {
1777
1948
  if (!(!e || typeof e != "object")) if (e.type === "Identifier") t.push(e.name);
@@ -1781,71 +1952,78 @@ function E(e) {
1781
1952
  }
1782
1953
  return n(e), t;
1783
1954
  }
1784
- function D(e, t) {
1955
+ function T(e, t) {
1785
1956
  if (!e) return "";
1786
1957
  switch (e.type) {
1787
1958
  case "Identifier": return e.name;
1788
- case "AssignmentPattern": return `${D(e.left, t)} = ${A(e.right, t)}`;
1789
- case "RestElement": return `...${D(e.argument, t)}`;
1790
- case "ArrayPattern": return `[${(e.elements || []).map((e) => e ? D(e, t) : "").join(", ")}]`;
1959
+ case "AssignmentPattern": return `${T(e.left, t)} = ${k(e.right, t)}`;
1960
+ case "RestElement": return `...${T(e.argument, t)}`;
1961
+ case "ArrayPattern": return `[${(e.elements || []).map((e) => e ? T(e, t) : "").join(", ")}]`;
1791
1962
  case "ObjectPattern": return `{ ${(e.properties || []).map((e) => {
1792
1963
  if (e.type === "Property") {
1793
- let n = e.key?.name || (typeof e.key?.value == "string" ? JSON.stringify(e.key.value) : e.key?.value ? String(e.key.value) : A(e.key, t));
1964
+ let n = e.key?.name || (typeof e.key?.value == "string" ? JSON.stringify(e.key.value) : e.key?.value ? String(e.key.value) : k(e.key, t));
1794
1965
  if (e.value?.type === "AssignmentPattern") {
1795
- let r = e.value.left?.name || D(e.value.left, t), i = A(e.value.right, t);
1966
+ let r = e.value.left?.name || T(e.value.left, t), i = k(e.value.right, t);
1796
1967
  return n === r || e.shorthand ? `${n} = ${i}` : `${n}: ${r} = ${i}`;
1797
1968
  } else if (e.value?.type === "Identifier" && e.value.name === n) return n;
1798
- else return `${n}: ${D(e.value, t)}`;
1799
- } else if (e.type === "RestElement") return `...${D(e.argument, t)}`;
1969
+ else return `${n}: ${T(e.value, t)}`;
1970
+ } else if (e.type === "RestElement") return `...${T(e.argument, t)}`;
1800
1971
  return "";
1801
1972
  }).filter(Boolean).join(", ")} }`;
1802
- default: return e.name || A(e, t);
1973
+ default: return e.name || k(e, t);
1803
1974
  }
1804
1975
  }
1805
- function O(e, t, n) {
1976
+ function E(e, t, n) {
1806
1977
  return n && n.has(e) ? `(${e} = ${t})` : `(typeof setScopeValue === 'function' && scope ? setScopeValue(scope, ${JSON.stringify(e)}, ${t}) : ((scope || {})[${JSON.stringify(e)}] = ${t}))`;
1807
1978
  }
1808
- function k(e, t, n, r = { count: 0 }) {
1979
+ function D(e, t, n, r = { count: 0 }) {
1809
1980
  let i = [];
1810
1981
  if (!e) return i;
1811
- if (e.type === "Identifier") return i.push(O(e.name, t, n)), i;
1982
+ if (e.type === "Identifier") return i.push(E(e.name, t, n)), i;
1812
1983
  if (e.type === "AssignmentPattern") {
1813
- let a = `((${t} !== undefined) ? ${t} : ${A(e.right, n)})`;
1814
- if (e.left?.type === "Identifier") i.push(O(e.left.name, a, n));
1984
+ let a = `((${t} !== undefined) ? ${t} : ${k(e.right, n)})`;
1985
+ if (e.left?.type === "Identifier") i.push(E(e.left.name, a, n));
1815
1986
  else {
1816
1987
  let t = `_t${r.count++}`;
1817
- i.push(`const ${t} = ${a}`), i.push(...k(e.left, t, n, r));
1988
+ i.push(`const ${t} = ${a}`), i.push(...D(e.left, t, n, r));
1818
1989
  }
1819
1990
  return i;
1820
1991
  }
1821
1992
  if (e.type === "ObjectPattern") {
1822
1993
  for (let a of e.properties || []) if (a.type === "Property") {
1823
- let e = a.computed ? A(a.key, n) : a.key?.name === void 0 ? typeof a.key?.value == "string" || typeof a.key?.value == "number" ? JSON.stringify(a.key.value) : A(a.key, n) : JSON.stringify(a.key.name);
1994
+ let e = a.computed ? k(a.key, n) : a.key?.name === void 0 ? typeof a.key?.value == "string" || typeof a.key?.value == "number" ? JSON.stringify(a.key.value) : k(a.key, n) : JSON.stringify(a.key.name);
1824
1995
  if (a.value?.type === "Identifier") {
1825
1996
  let r = `(${t} ? ${t}[${e}] : undefined)`;
1826
- i.push(O(a.value.name, r, n));
1997
+ i.push(E(a.value.name, r, n));
1827
1998
  } else if (a.value?.type === "AssignmentPattern") {
1828
- let o = `((${t} && ${t}[${e}] !== undefined) ? ${t}[${e}] : ${A(a.value.right, n)})`;
1829
- if (a.value.left?.type === "Identifier") i.push(O(a.value.left.name, o, n));
1999
+ let o = `((${t} && ${t}[${e}] !== undefined) ? ${t}[${e}] : ${k(a.value.right, n)})`;
2000
+ if (a.value.left?.type === "Identifier") i.push(E(a.value.left.name, o, n));
1830
2001
  else {
1831
2002
  let e = `_t${r.count++}`;
1832
- i.push(`const ${e} = ${o}`), i.push(...k(a.value.left, e, n, r));
2003
+ i.push(`const ${e} = ${o}`), i.push(...D(a.value.left, e, n, r));
1833
2004
  }
1834
2005
  } else if (a.value?.type === "ObjectPattern" || a.value?.type === "ArrayPattern") {
1835
2006
  let o = `_t${r.count++}`, s = `(${t} ? ${t}[${e}] : undefined)`;
1836
- i.push(`const ${o} = ${s}`), i.push(...k(a.value, o, n, r));
2007
+ i.push(`const ${o} = ${s}`), i.push(...D(a.value, o, n, r));
1837
2008
  } else {
1838
- let r = a.value?.name || A(a.value, n);
2009
+ let r = a.value?.name || k(a.value, n);
1839
2010
  if (r) {
1840
2011
  let a = `(${t} ? ${t}[${e}] : undefined)`;
1841
- i.push(O(r, a, n));
2012
+ i.push(E(r, a, n));
1842
2013
  }
1843
2014
  }
1844
2015
  } else if (a.type === "RestElement") {
1845
- let r = a.argument?.name || A(a.argument, n);
2016
+ let r = a.argument?.name || k(a.argument, n);
1846
2017
  if (r) {
1847
- let a = (e.properties || []).filter((e) => e.type === "Property").map((e) => e.key?.name || (typeof e.key?.value == "string" ? e.key.value : "")).filter(Boolean), o = `(() => { const _r = Object.assign({}, ${t}); ${JSON.stringify(a)}.forEach(k => delete _r[k]); return _r; })()`;
1848
- i.push(O(r, o, n));
2018
+ (e.properties || []).filter((e) => e.type === "Property").map((e) => {
2019
+ let t = e.key?.name === void 0 ? typeof e.key?.value == "string" ? e.key.value : null : e.key.name;
2020
+ return t == null ? null : JSON.stringify(t);
2021
+ }).filter(Boolean);
2022
+ let a = (e.properties || []).filter((e) => e.type === "Property").map((e) => {
2023
+ let t = e.key?.name === void 0 ? typeof e.key?.value == "string" ? e.key.value : null : e.key.name;
2024
+ return t == null ? null : String(t);
2025
+ }).filter(Boolean).join(", "), o = `(((${a.length > 0 ? `{ ${a}, ...${r} }` : `{ ...${r} }`}) => ${r})(${t} ?? {}))`;
2026
+ i.push(E(r, o, n));
1849
2027
  }
1850
2028
  }
1851
2029
  return i;
@@ -1859,23 +2037,23 @@ function k(e, t, n, r = { count: 0 }) {
1859
2037
  if (t) {
1860
2038
  if (t.type === "Identifier") {
1861
2039
  let r = `(${a} ? ${a}[${e}] : undefined)`;
1862
- i.push(O(t.name, r, n));
2040
+ i.push(E(t.name, r, n));
1863
2041
  } else if (t.type === "AssignmentPattern") {
1864
- let o = A(t.right, n), s = `((${a} && ${a}[${e}] !== undefined) ? ${a}[${e}] : ${o})`;
1865
- if (t.left?.type === "Identifier") i.push(O(t.left.name, s, n));
2042
+ let o = k(t.right, n), s = `((${a} && ${a}[${e}] !== undefined) ? ${a}[${e}] : ${o})`;
2043
+ if (t.left?.type === "Identifier") i.push(E(t.left.name, s, n));
1866
2044
  else {
1867
2045
  let e = `_t${r.count++}`;
1868
- i.push(`const ${e} = ${s}`), i.push(...k(t.left, e, n, r));
2046
+ i.push(`const ${e} = ${s}`), i.push(...D(t.left, e, n, r));
1869
2047
  }
1870
2048
  } else if (t.type === "RestElement") {
1871
- let r = t.argument?.name || A(t.argument, n);
2049
+ let r = t.argument?.name || k(t.argument, n);
1872
2050
  if (r) {
1873
2051
  let t = `((${a} && typeof ${a}.slice === 'function') ? ${a}.slice(${e}) : [])`;
1874
- i.push(O(r, t, n));
2052
+ i.push(E(r, t, n));
1875
2053
  }
1876
2054
  } else if (t.type === "ObjectPattern" || t.type === "ArrayPattern") {
1877
2055
  let o = `_t${r.count++}`, s = `(${a} ? ${a}[${e}] : undefined)`;
1878
- i.push(`const ${o} = ${s}`), i.push(...k(t, o, n, r));
2056
+ i.push(`const ${o} = ${s}`), i.push(...D(t, o, n, r));
1879
2057
  }
1880
2058
  }
1881
2059
  }
@@ -1883,41 +2061,33 @@ function k(e, t, n, r = { count: 0 }) {
1883
2061
  }
1884
2062
  return i;
1885
2063
  }
1886
- function A(e, t) {
2064
+ function O(e, t) {
2065
+ let n = [], r = D(e, "_item", void 0, { count: 0 });
2066
+ return n.push(...r), t && n.push(E(t, "_index", void 0)), { __drift_fn__: `(scope, declaredVars, setScopeValue, inScopeChain, resolveIterable, getScopeValue) => (_item, _index) => { ${n.join("; ")}; }` };
2067
+ }
2068
+ function k(e, t) {
1887
2069
  if (e == null) return "undefined";
1888
2070
  if (typeof e != "object") return typeof e == "string" ? JSON.stringify(e) : String(e);
1889
- if (Array.isArray(e)) return e.map((e) => A(e, t)).join("; ");
2071
+ if (Array.isArray(e)) return e.map((e) => k(e, t)).join("; ");
1890
2072
  switch (e.type) {
1891
- case "Identifier": return t && t.has(e.name) ? e.name : `(typeof _get === 'function' ? _get(scope, ${JSON.stringify(e.name)}) : (typeof inScopeChain === 'function' && inScopeChain(scope, ${JSON.stringify(e.name)}) ? scope[${JSON.stringify(e.name)}] : (typeof globalThis !== 'undefined' && globalThis && (${JSON.stringify(e.name)} in globalThis) ? globalThis[${JSON.stringify(e.name)}] : (scope || {})[${JSON.stringify(e.name)}])))`;
2073
+ case "Identifier": return t && t.has(e.name) ? e.name : e.name === "undefined" ? "undefined" : `getScopeValue(scope, ${JSON.stringify(e.name)})`;
1892
2074
  case "Literal": return typeof e.raw == "string" ? e.raw : typeof e.value == "string" ? JSON.stringify(e.value) : String(e.value);
1893
2075
  case "BinaryExpression":
1894
- case "LogicalExpression": return `(${A(e.left, t)} ${e.operator} ${A(e.right, t)})`;
1895
- case "UnaryExpression": return `(${e.operator} ${A(e.argument, t)})`;
1896
- case "AwaitExpression": return `(await ${A(e.argument, t)})`;
1897
- case "YieldExpression": return e.delegate ? `(yield* ${e.argument ? A(e.argument, t) : ""})` : `(yield ${e.argument ? A(e.argument, t) : ""})`;
1898
- case "ConditionalExpression": return `(${A(e.test, t)} ? ${A(e.consequent, t)} : ${A(e.alternate, t)})`;
2076
+ case "LogicalExpression": return `(${k(e.left, t)} ${e.operator} ${k(e.right, t)})`;
2077
+ case "UnaryExpression": return `(${e.operator} ${k(e.argument, t)})`;
2078
+ case "AwaitExpression": return `(await ${k(e.argument, t)})`;
2079
+ case "YieldExpression": return e.delegate ? `(yield* ${e.argument ? k(e.argument, t) : ""})` : `(yield ${e.argument ? k(e.argument, t) : ""})`;
2080
+ case "ConditionalExpression": return `(${k(e.test, t)} ? ${k(e.consequent, t)} : ${k(e.alternate, t)})`;
1899
2081
  case "MemberExpression": {
1900
2082
  let n = e.optional ? "?." : ".";
1901
- return e.computed ? e.optional ? `(${A(e.object, t)}?.[${A(e.property, t)}])` : `(${A(e.object, t)}[${A(e.property, t)}])` : `(${A(e.object, t)}${n}${e.property.name})`;
2083
+ return e.computed ? e.optional ? `(${k(e.object, t)}?.[${k(e.property, t)}])` : `(${k(e.object, t)}[${k(e.property, t)}])` : `(${k(e.object, t)}${n}${e.property.name})`;
1902
2084
  }
1903
2085
  case "CallExpression": {
1904
- let n = A(e.callee, t), r = e.arguments ? e.arguments.map((e) => A(e, t)).join(", ") : "", i = `(${n}${e.optional ? "?.(" : "("}${r}))`;
1905
- if (e.callee?.type === "MemberExpression" && e.callee.property?.type === "Identifier") {
1906
- let n = T(e.callee.object), r = e.callee.property.name;
1907
- if (n && [
1908
- "push",
1909
- "pop",
1910
- "shift",
1911
- "unshift",
1912
- "splice",
1913
- "sort",
1914
- "reverse"
1915
- ].includes(r) && (!t || !t.has(n))) return `(() => { const _res = ${i}; if (typeof setScopeValue === 'function' && scope && typeof inScopeChain === 'function' && inScopeChain(scope, ${JSON.stringify(n)})) setScopeValue(scope, ${JSON.stringify(n)}, scope[${JSON.stringify(n)}]); return _res; })()`;
1916
- }
1917
- return i;
2086
+ let n = k(e.callee, t), r = e.arguments ? e.arguments.map((e) => k(e, t)).join(", ") : "";
2087
+ return `(${n}${e.optional ? "?.(" : "("}${r}))`;
1918
2088
  }
1919
2089
  case "AssignmentExpression": {
1920
- let n = A(e.right, t);
2090
+ let n = k(e.right, t);
1921
2091
  if (e.left?.type === "Identifier") {
1922
2092
  let r = e.left.name;
1923
2093
  if (t && t.has(r)) return `(${r} ${e.operator} ${n})`;
@@ -1928,10 +2098,10 @@ function A(e, t) {
1928
2098
  }
1929
2099
  }
1930
2100
  if (e.left?.type === "MemberExpression") {
1931
- let r = T(e.left), i = `(${A(e.left, t)} ${e.operator} ${n})`;
2101
+ let r = C(e.left), i = `(${k(e.left, t)} ${e.operator} ${n})`;
1932
2102
  if (r && (!t || !t.has(r))) return `(() => { const _res = ${i}; if (typeof setScopeValue === 'function' && scope && typeof inScopeChain === 'function' && inScopeChain(scope, ${JSON.stringify(r)})) setScopeValue(scope, ${JSON.stringify(r)}, scope[${JSON.stringify(r)}]); return _res; })()`;
1933
2103
  }
1934
- return e.left?.type === "ObjectPattern" || e.left?.type === "ArrayPattern" ? `((_val) => { ${k(e.left, "_val", t).join("; ")}; return _val; })(${n})` : `(${A(e.left, t)} ${e.operator} ${n})`;
2104
+ return e.left?.type === "ObjectPattern" || e.left?.type === "ArrayPattern" ? `((_val) => { ${D(e.left, "_val", t).join("; ")}; return _val; })(${n})` : `(${k(e.left, t)} ${e.operator} ${n})`;
1935
2105
  }
1936
2106
  case "UpdateExpression":
1937
2107
  if (e.argument?.type === "Identifier") {
@@ -1941,76 +2111,76 @@ function A(e, t) {
1941
2111
  return e.prefix ? `(typeof setScopeValue === 'function' ? (setScopeValue(scope, ${JSON.stringify(n)}, (Number(scope[${JSON.stringify(n)}]) || 0) ${r} 1), scope[${JSON.stringify(n)}]) : ((scope || {})[${JSON.stringify(n)}] = (Number((scope || {})[${JSON.stringify(n)}]) || 0) ${r} 1))` : `(() => { const _v = Number(scope[${JSON.stringify(n)}]) || 0; if (typeof setScopeValue === 'function') setScopeValue(scope, ${JSON.stringify(n)}, _v ${r} 1); else (scope || {})[${JSON.stringify(n)}] = _v ${r} 1; return _v; })()`;
1942
2112
  }
1943
2113
  if (e.argument?.type === "MemberExpression") {
1944
- let n = T(e.argument), r = e.prefix ? `(${e.operator}${A(e.argument, t)})` : `(${A(e.argument, t)}${e.operator})`;
2114
+ let n = C(e.argument), r = e.prefix ? `(${e.operator}${k(e.argument, t)})` : `(${k(e.argument, t)}${e.operator})`;
1945
2115
  if (n && (!t || !t.has(n))) return `(() => { const _res = ${r}; if (typeof setScopeValue === 'function' && scope && typeof inScopeChain === 'function' && inScopeChain(scope, ${JSON.stringify(n)})) setScopeValue(scope, ${JSON.stringify(n)}, scope[${JSON.stringify(n)}]); return _res; })()`;
1946
2116
  }
1947
- return e.prefix ? `(${e.operator}${A(e.argument, t)})` : `(${A(e.argument, t)}${e.operator})`;
1948
- case "SequenceExpression": return `(${e.expressions ? e.expressions.map((e) => A(e, t)).join(", ") : ""})`;
1949
- case "ArrayExpression": return `[${e.elements ? e.elements.map((e) => e?.type === "SpreadElement" ? "..." + A(e.argument, t) : A(e, t)).join(", ") : ""}]`;
2117
+ return e.prefix ? `(${e.operator}${k(e.argument, t)})` : `(${k(e.argument, t)}${e.operator})`;
2118
+ case "SequenceExpression": return `(${e.expressions ? e.expressions.map((e) => k(e, t)).join(", ") : ""})`;
2119
+ case "ArrayExpression": return `[${e.elements ? e.elements.map((e) => e?.type === "SpreadElement" ? "..." + k(e.argument, t) : k(e, t)).join(", ") : ""}]`;
1950
2120
  case "ObjectExpression": return `{${e.properties ? e.properties.map((e) => {
1951
- if (e.type === "SpreadElement") return "..." + A(e.argument, t);
1952
- let n = e.computed ? `[${A(e.key, t)}]` : e.key?.name || (typeof e.key?.value == "string" ? JSON.stringify(e.key.value) : String(e.key?.value ?? ""));
2121
+ if (e.type === "SpreadElement") return "..." + k(e.argument, t);
2122
+ let n = e.computed ? `[${k(e.key, t)}]` : e.key?.name || (typeof e.key?.value == "string" ? JSON.stringify(e.key.value) : String(e.key?.value ?? ""));
1953
2123
  if (e.kind === "get" || e.kind === "set") {
1954
2124
  let r = e.value, i = new Set(t), a = [];
1955
2125
  if (r.params) for (let e of r.params) {
1956
- for (let t of E(e)) i.add(t);
1957
- a.push(D(e, i));
2126
+ for (let t of w(e)) i.add(t);
2127
+ a.push(T(e, i));
1958
2128
  }
1959
- let o = r.body?.type === "BlockStatement" ? `{ ${r.body.body.map((e) => A(e, i)).filter(Boolean).join("; ")}; }` : `{ ${A(r.body, i)}; }`;
2129
+ let o = r.body?.type === "BlockStatement" ? `{ ${r.body.body.map((e) => k(e, i)).filter(Boolean).join("; ")}; }` : `{ ${k(r.body, i)}; }`;
1960
2130
  return `${e.kind} ${n}(${a.join(", ")}) ${o}`;
1961
2131
  }
1962
2132
  if (e.method) {
1963
2133
  let r = e.value, i = new Set(t), a = [];
1964
2134
  if (r.params) for (let e of r.params) {
1965
- for (let t of E(e)) i.add(t);
1966
- a.push(D(e, i));
2135
+ for (let t of w(e)) i.add(t);
2136
+ a.push(T(e, i));
1967
2137
  }
1968
- let o = r.async ? "async " : "", s = r.generator ? "*" : "", c = r.body?.type === "BlockStatement" ? `{ ${r.body.body.map((e) => A(e, i)).filter(Boolean).join("; ")}; }` : `{ ${A(r.body, i)}; }`;
2138
+ let o = r.async ? "async " : "", s = r.generator ? "*" : "", c = r.body?.type === "BlockStatement" ? `{ ${r.body.body.map((e) => k(e, i)).filter(Boolean).join("; ")}; }` : `{ ${k(r.body, i)}; }`;
1969
2139
  return `${o}${s}${n}(${a.join(", ")}) ${c}`;
1970
2140
  }
1971
- return `${n}: ${A(e.value, t)}`;
2141
+ return `${n}: ${k(e.value, t)}`;
1972
2142
  }).join(", ") : ""}}`;
1973
2143
  case "RestElement":
1974
- case "SpreadElement": return `...${A(e.argument, t)}`;
1975
- case "AssignmentPattern": return `${A(e.left, t)} = ${A(e.right, t)}`;
1976
- case "TemplateLiteral": return `\`${e.quasis ? e.quasis.map((n, r) => (n.value?.raw ?? "") + (e.expressions && e.expressions[r] ? "${" + A(e.expressions[r], t) + "}" : "")).join("") : ""}\``;
1977
- case "TaggedTemplateExpression": return `${A(e.tag, t)}${A(e.quasi, t)}`;
2144
+ case "SpreadElement": return `...${k(e.argument, t)}`;
2145
+ case "AssignmentPattern": return `${k(e.left, t)} = ${k(e.right, t)}`;
2146
+ case "TemplateLiteral": return `\`${e.quasis ? e.quasis.map((n, r) => (n.value?.raw ?? "") + (e.expressions && e.expressions[r] ? "${" + k(e.expressions[r], t) + "}" : "")).join("") : ""}\``;
2147
+ case "TaggedTemplateExpression": return `${k(e.tag, t)}${k(e.quasi, t)}`;
1978
2148
  case "ThisExpression": return "this";
1979
2149
  case "Super": return "super";
1980
2150
  case "BlockStatement": {
1981
2151
  let n = new Set(t);
1982
- if (Array.isArray(e.body)) for (let t of e.body) if (t.type === "VariableDeclaration" && Array.isArray(t.declarations)) for (let e of t.declarations) for (let t of E(e.id)) n.add(t);
2152
+ if (Array.isArray(e.body)) for (let t of e.body) if (t.type === "VariableDeclaration" && Array.isArray(t.declarations)) for (let e of t.declarations) for (let t of w(e.id)) n.add(t);
1983
2153
  else t.type === "FunctionDeclaration" && t.id?.name && n.add(t.id.name);
1984
- return `{ ${(e.body ? e.body.map((e) => A(e, n)).filter(Boolean) : []).join("; ")}; }`;
2154
+ return `{ ${(e.body ? e.body.map((e) => k(e, n)).filter(Boolean) : []).join("; ")}; }`;
1985
2155
  }
1986
- case "ExpressionStatement": return A(e.expression, t);
1987
- case "ReturnStatement": return `return ${e.argument ? A(e.argument, t) : ""}`;
1988
- case "ThrowStatement": return `throw ${e.argument ? A(e.argument, t) : ""}`;
2156
+ case "ExpressionStatement": return k(e.expression, t);
2157
+ case "ReturnStatement": return `return ${e.argument ? k(e.argument, t) : ""}`;
2158
+ case "ThrowStatement": return `throw ${e.argument ? k(e.argument, t) : ""}`;
1989
2159
  case "BreakStatement": return e.label ? `break ${e.label.name}` : "break";
1990
2160
  case "ContinueStatement": return e.label ? `continue ${e.label.name}` : "continue";
1991
- case "LabeledStatement": return `${e.label.name}: ${A(e.body, t)}`;
1992
- case "TryStatement": return `try ${A(e.block, t)} ${e.handler ? A(e.handler, t) : ""} ${e.finalizer ? `finally ${A(e.finalizer, t)}` : ""}`.trim();
2161
+ case "LabeledStatement": return `${e.label.name}: ${k(e.body, t)}`;
2162
+ case "TryStatement": return `try ${k(e.block, t)} ${e.handler ? k(e.handler, t) : ""} ${e.finalizer ? `finally ${k(e.finalizer, t)}` : ""}`.trim();
1993
2163
  case "CatchClause": {
1994
2164
  let n = new Set(t), r = "";
1995
2165
  if (e.param) {
1996
- for (let t of E(e.param)) n.add(t);
1997
- r = D(e.param, n);
2166
+ for (let t of w(e.param)) n.add(t);
2167
+ r = T(e.param, n);
1998
2168
  }
1999
- let i = A(e.body, n);
2169
+ let i = k(e.body, n);
2000
2170
  return r ? `catch (${r}) ${i}` : `catch ${i}`;
2001
2171
  }
2002
- case "SwitchStatement": return `switch (${A(e.discriminant, t)}) { ${e.cases ? e.cases.map((e) => A(e, t)).join(" ") : ""} }`;
2172
+ case "SwitchStatement": return `switch (${k(e.discriminant, t)}) { ${e.cases ? e.cases.map((e) => k(e, t)).join(" ") : ""} }`;
2003
2173
  case "SwitchCase": {
2004
- let n = e.test ? `case ${A(e.test, t)}:` : "default:", r = e.consequent ? e.consequent.map((e) => A(e, t)).filter(Boolean).join("; ") : "";
2174
+ let n = e.test ? `case ${k(e.test, t)}:` : "default:", r = e.consequent ? e.consequent.map((e) => k(e, t)).filter(Boolean).join("; ") : "";
2005
2175
  return `${n} ${r ? r + ";" : ""}`;
2006
2176
  }
2007
- case "IfStatement": return `if (${A(e.test, t)}) ${e.consequent.type === "BlockStatement" ? A(e.consequent, t) : `{ ${A(e.consequent, t)}; }`}${e.alternate ? ` else ${e.alternate.type === "BlockStatement" || e.alternate.type === "IfStatement" ? A(e.alternate, t) : `{ ${A(e.alternate, t)}; }`}` : ""}`;
2177
+ case "IfStatement": return `if (${k(e.test, t)}) ${e.consequent.type === "BlockStatement" ? k(e.consequent, t) : `{ ${k(e.consequent, t)}; }`}${e.alternate ? ` else ${e.alternate.type === "BlockStatement" || e.alternate.type === "IfStatement" ? k(e.alternate, t) : `{ ${k(e.alternate, t)}; }`}` : ""}`;
2008
2178
  case "ForStatement": {
2009
2179
  let n = new Set(t);
2010
- if (e.init?.type === "VariableDeclaration" && e.init.declarations) for (let t of e.init.declarations) for (let e of E(t.id)) n.add(e);
2180
+ if (e.init?.type === "VariableDeclaration" && e.init.declarations) for (let t of e.init.declarations) for (let e of w(t.id)) n.add(e);
2011
2181
  let r = "";
2012
- e.init?.type === "VariableDeclaration" && e.init.declarations ? r = (e.init.kind || "let") + " " + e.init.declarations.map((e) => `${e.id?.type === "Identifier" ? e.id.name : A(e.id, n)}${e.init ? ` = ${A(e.init, n)}` : ""}`).join(", ") : e.init && (r = A(e.init, n));
2013
- let i = e.test ? A(e.test, n) : "", a = e.update ? A(e.update, n) : "", o = e.body?.type === "BlockStatement" ? A(e.body, n) : `{ ${e.body ? A(e.body, n) : ""}; }`;
2182
+ e.init?.type === "VariableDeclaration" && e.init.declarations ? r = (e.init.kind || "let") + " " + e.init.declarations.map((e) => `${e.id?.type === "Identifier" ? e.id.name : k(e.id, n)}${e.init ? ` = ${k(e.init, n)}` : ""}`).join(", ") : e.init && (r = k(e.init, n));
2183
+ let i = e.test ? k(e.test, n) : "", a = e.update ? k(e.update, n) : "", o = e.body?.type === "BlockStatement" ? k(e.body, n) : `{ ${e.body ? k(e.body, n) : ""}; }`;
2014
2184
  return `for (${r}; ${i}; ${a}) ${o}`;
2015
2185
  }
2016
2186
  case "ForOfStatement": {
@@ -2018,11 +2188,11 @@ function A(e, t) {
2018
2188
  if (e.left?.type === "VariableDeclaration") {
2019
2189
  let t = e.left.kind || "let", i = e.left.declarations?.[0];
2020
2190
  if (i) {
2021
- for (let e of E(i.id)) n.add(e);
2022
- r = `${t} ${i.id?.type === "Identifier" ? i.id.name : A(i.id, n)}`;
2191
+ for (let e of w(i.id)) n.add(e);
2192
+ r = `${t} ${i.id?.type === "Identifier" ? i.id.name : k(i.id, n)}`;
2023
2193
  }
2024
- } else e.left?.type === "Identifier" ? (n.add(e.left.name), r = e.left.name) : e.left && (r = A(e.left, n));
2025
- let i = `(typeof resolveIterable === 'function' ? resolveIterable : (x) => x || [])(${A(e.right, t)})`, a = e.body?.type === "BlockStatement" ? A(e.body, n) : `{ ${e.body ? A(e.body, n) : ""}; }`;
2194
+ } else e.left?.type === "Identifier" ? (n.add(e.left.name), r = e.left.name) : e.left && (r = k(e.left, n));
2195
+ let i = `(typeof resolveIterable === 'function' ? resolveIterable : (x) => x || [])(${k(e.right, t)})`, a = e.body?.type === "BlockStatement" ? k(e.body, n) : `{ ${e.body ? k(e.body, n) : ""}; }`;
2026
2196
  return `for ${e.await ? "await " : ""}(${r} of ${i}) ${a}`;
2027
2197
  }
2028
2198
  case "ForInStatement": {
@@ -2030,41 +2200,41 @@ function A(e, t) {
2030
2200
  if (e.left?.type === "VariableDeclaration") {
2031
2201
  let t = e.left.kind || "let", i = e.left.declarations?.[0];
2032
2202
  if (i) {
2033
- for (let e of E(i.id)) n.add(e);
2034
- r = `${t} ${i.id?.type === "Identifier" ? i.id.name : A(i.id, n)}`;
2203
+ for (let e of w(i.id)) n.add(e);
2204
+ r = `${t} ${i.id?.type === "Identifier" ? i.id.name : k(i.id, n)}`;
2035
2205
  }
2036
- } else e.left?.type === "Identifier" ? (n.add(e.left.name), r = e.left.name) : e.left && (r = A(e.left, n));
2037
- let i = A(e.right, t), a = e.body?.type === "BlockStatement" ? A(e.body, n) : `{ ${e.body ? A(e.body, n) : ""}; }`;
2206
+ } else e.left?.type === "Identifier" ? (n.add(e.left.name), r = e.left.name) : e.left && (r = k(e.left, n));
2207
+ let i = k(e.right, t), a = e.body?.type === "BlockStatement" ? k(e.body, n) : `{ ${e.body ? k(e.body, n) : ""}; }`;
2038
2208
  return `for (${r} in ${i}) ${a}`;
2039
2209
  }
2040
- case "WhileStatement": return `while (${A(e.test, t)}) ${e.body?.type === "BlockStatement" ? A(e.body, t) : `{ ${e.body ? A(e.body, t) : ""}; }`}`;
2210
+ case "WhileStatement": return `while (${k(e.test, t)}) ${e.body?.type === "BlockStatement" ? k(e.body, t) : `{ ${e.body ? k(e.body, t) : ""}; }`}`;
2041
2211
  case "DoWhileStatement": {
2042
- let n = A(e.test, t);
2043
- return `do ${e.body?.type === "BlockStatement" ? A(e.body, t) : `{ ${e.body ? A(e.body, t) : ""}; }`} while (${n})`;
2212
+ let n = k(e.test, t);
2213
+ return `do ${e.body?.type === "BlockStatement" ? k(e.body, t) : `{ ${e.body ? k(e.body, t) : ""}; }`} while (${n})`;
2044
2214
  }
2045
- case "ArrayPattern": return `[ ${e.elements ? e.elements.map((e) => e ? e.type === "RestElement" ? "..." + A(e.argument, t) : e.type === "AssignmentPattern" ? `${A(e.left, t)} = ${A(e.right, t)}` : e.name || A(e, t) : "").join(", ") : ""} ]`;
2215
+ case "ArrayPattern": return `[ ${e.elements ? e.elements.map((e) => e ? e.type === "RestElement" ? "..." + k(e.argument, t) : e.type === "AssignmentPattern" ? `${k(e.left, t)} = ${k(e.right, t)}` : e.name || k(e, t) : "").join(", ") : ""} ]`;
2046
2216
  case "ObjectPattern": return `{ ${e.properties ? e.properties.map((e) => {
2047
2217
  if (e.type === "Property") {
2048
- let n = e.key?.name || A(e.key, t), r = e.value?.name || A(e.value, t);
2218
+ let n = e.key?.name || k(e.key, t), r = e.value?.name || k(e.value, t);
2049
2219
  return n === r ? n : `${n}: ${r}`;
2050
2220
  }
2051
- return e.type === "RestElement" ? `...${A(e.argument, t)}` : "";
2221
+ return e.type === "RestElement" ? `...${k(e.argument, t)}` : "";
2052
2222
  }).filter(Boolean).join(", ") : ""} }`;
2053
2223
  case "VariableDeclaration": {
2054
2224
  if (t) {
2055
2225
  let n = [];
2056
2226
  if (e.declarations) for (let r of e.declarations) {
2057
- let e = r.id?.type === "Identifier" ? r.id.name : A(r.id, t), i = r.init ? A(r.init, t) : void 0;
2227
+ let e = r.id?.type === "Identifier" ? r.id.name : k(r.id, t), i = r.init ? k(r.init, t) : void 0;
2058
2228
  i === void 0 ? n.push(`${e}`) : n.push(`${e} = ${i}`);
2059
2229
  }
2060
2230
  return `${e.kind || "let"} ${n.join(", ")}`;
2061
2231
  }
2062
2232
  let n = [];
2063
2233
  if (e.declarations) for (let r of e.declarations) if (r.id?.type === "ObjectPattern" || r.id?.type === "ArrayPattern") {
2064
- let e = r.init ? A(r.init, t) : "undefined", i = k(r.id, "_init", t);
2234
+ let e = r.init ? k(r.init, t) : "undefined", i = D(r.id, "_init", t);
2065
2235
  n.push(`((_init) => { ${i.join("; ")}; return _init; })(${e})`);
2066
2236
  } else {
2067
- let e = r.id?.name || A(r.id, t), i = r.init ? A(r.init, t) : "undefined";
2237
+ let e = r.id?.name || k(r.id, t), i = r.init ? k(r.init, t) : "undefined";
2068
2238
  n.push(`((scope || {})[${JSON.stringify(e)}] = ${i})`);
2069
2239
  }
2070
2240
  return `(${n.filter(Boolean).join(", ") || "undefined"})`;
@@ -2074,12 +2244,12 @@ function A(e, t) {
2074
2244
  n && r.add(n);
2075
2245
  let i = [];
2076
2246
  if (e.params) for (let t of e.params) {
2077
- for (let e of E(t)) r.add(e);
2078
- i.push(D(t, r));
2247
+ for (let e of w(t)) r.add(e);
2248
+ i.push(T(t, r));
2079
2249
  }
2080
- if (e.body?.type === "BlockStatement" && Array.isArray(e.body.body)) for (let t of e.body.body) if (t.type === "VariableDeclaration" && Array.isArray(t.declarations)) for (let e of t.declarations) for (let t of E(e.id)) r.add(t);
2250
+ if (e.body?.type === "BlockStatement" && Array.isArray(e.body.body)) for (let t of e.body.body) if (t.type === "VariableDeclaration" && Array.isArray(t.declarations)) for (let e of t.declarations) for (let t of w(e.id)) r.add(t);
2081
2251
  else t.type === "FunctionDeclaration" && t.id?.name && r.add(t.id.name);
2082
- let a = i.join(", "), o = e.body?.type === "BlockStatement" ? `{ ${e.body.body.map((e) => A(e, r)).filter(Boolean).join("; ")}; }` : `{ ${A(e.body, r)}; }`, s = `${e.async ? "async " : ""}function${e.generator ? "*" : ""} ${n || ""}(${a}) ${o}`;
2252
+ let a = i.join(", "), o = e.body?.type === "BlockStatement" ? `{ ${e.body.body.map((e) => k(e, r)).filter(Boolean).join("; ")}; }` : `{ ${k(e.body, r)}; }`, s = `${e.async ? "async " : ""}function${e.generator ? "*" : ""} ${n || ""}(${a}) ${o}`;
2083
2253
  return n ? `((scope || {})[${JSON.stringify(n)}] = ${s})` : s;
2084
2254
  }
2085
2255
  case "ArrowFunctionExpression":
@@ -2088,15 +2258,15 @@ function A(e, t) {
2088
2258
  e.id?.name && n.add(e.id.name);
2089
2259
  let r = [];
2090
2260
  if (e.params) for (let t of e.params) {
2091
- for (let e of E(t)) n.add(e);
2092
- r.push(D(t, n));
2261
+ for (let e of w(t)) n.add(e);
2262
+ r.push(T(t, n));
2093
2263
  }
2094
- if (e.body?.type === "BlockStatement" && Array.isArray(e.body.body)) for (let t of e.body.body) if (t.type === "VariableDeclaration" && Array.isArray(t.declarations)) for (let e of t.declarations) for (let t of E(e.id)) n.add(t);
2264
+ if (e.body?.type === "BlockStatement" && Array.isArray(e.body.body)) for (let t of e.body.body) if (t.type === "VariableDeclaration" && Array.isArray(t.declarations)) for (let e of t.declarations) for (let t of w(e.id)) n.add(t);
2095
2265
  else t.type === "FunctionDeclaration" && t.id?.name && n.add(t.id.name);
2096
2266
  let i = r.join(", "), a = e.async ? "async " : "";
2097
- if (e.type === "ArrowFunctionExpression") return e.body?.type === "BlockStatement" ? `(${a}(${i}) => ${`{ ${e.body.body.map((e) => A(e, n)).filter(Boolean).join("; ")}; }`})` : `(${a}(${i}) => ${A(e.body, n)})`;
2267
+ if (e.type === "ArrowFunctionExpression") return e.body?.type === "BlockStatement" ? `(${a}(${i}) => ${`{ ${e.body.body.map((e) => k(e, n)).filter(Boolean).join("; ")}; }`})` : `(${a}(${i}) => ${k(e.body, n)})`;
2098
2268
  {
2099
- let t = e.generator ? "*" : "", r = e.body?.type === "BlockStatement" ? `{ ${e.body.body.map((e) => A(e, n)).filter(Boolean).join("; ")}; }` : `{ ${A(e.body, n)}; }`;
2269
+ let t = e.generator ? "*" : "", r = e.body?.type === "BlockStatement" ? `{ ${e.body.body.map((e) => k(e, n)).filter(Boolean).join("; ")}; }` : `{ ${k(e.body, n)}; }`;
2100
2270
  return `(${a}function${t} ${e.id?.name || ""}(${i}) ${r})`;
2101
2271
  }
2102
2272
  }
@@ -2104,37 +2274,37 @@ function A(e, t) {
2104
2274
  case "ClassExpression": {
2105
2275
  let n = e.id?.name, r = new Set(t);
2106
2276
  n && r.add(n);
2107
- let i = e.superClass ? ` extends ${A(e.superClass, t)}` : "", a = A(e.body, r), o = `class ${n || ""}${i} ${a}`;
2277
+ let i = e.superClass ? ` extends ${k(e.superClass, t)}` : "", a = k(e.body, r), o = `class ${n || ""}${i} ${a}`;
2108
2278
  return e.type === "ClassDeclaration" && n ? `((scope || {})[${JSON.stringify(n)}] = ${o})` : `(${o})`;
2109
2279
  }
2110
- case "ClassBody": return `{ ${(e.body ? e.body.map((e) => A(e, t)).filter(Boolean) : []).join("; ")} }`;
2280
+ case "ClassBody": return `{ ${(e.body ? e.body.map((e) => k(e, t)).filter(Boolean) : []).join("; ")} }`;
2111
2281
  case "MethodDefinition": {
2112
- let n = e.static ? "static " : "", r = e.kind === "get" || e.kind === "set" ? `${e.kind} ` : "", i = e.value?.async ? "async " : "", a = e.value?.generator ? "*" : "", o = e.computed ? `[${A(e.key, t)}]` : e.key?.name || A(e.key, t), s = new Set(t), c = [];
2282
+ let n = e.static ? "static " : "", r = e.kind === "get" || e.kind === "set" ? `${e.kind} ` : "", i = e.value?.async ? "async " : "", a = e.value?.generator ? "*" : "", o = e.computed ? `[${k(e.key, t)}]` : e.key?.name || k(e.key, t), s = new Set(t), c = [];
2113
2283
  if (e.value?.params) for (let t of e.value.params) {
2114
- for (let e of E(t)) s.add(e);
2115
- c.push(D(t, s));
2284
+ for (let e of w(t)) s.add(e);
2285
+ c.push(T(t, s));
2116
2286
  }
2117
- if (e.value?.body?.type === "BlockStatement" && Array.isArray(e.value.body.body)) for (let t of e.value.body.body) if (t.type === "VariableDeclaration" && Array.isArray(t.declarations)) for (let e of t.declarations) for (let t of E(e.id)) s.add(t);
2287
+ if (e.value?.body?.type === "BlockStatement" && Array.isArray(e.value.body.body)) for (let t of e.value.body.body) if (t.type === "VariableDeclaration" && Array.isArray(t.declarations)) for (let e of t.declarations) for (let t of w(e.id)) s.add(t);
2118
2288
  else t.type === "FunctionDeclaration" && t.id?.name && s.add(t.id.name);
2119
- return `${n}${i}${a}${r}${o}(${c.join(", ")}) ${e.value?.body ? A(e.value.body, s) : "{}"}`;
2289
+ return `${n}${i}${a}${r}${o}(${c.join(", ")}) ${e.value?.body ? k(e.value.body, s) : "{}"}`;
2120
2290
  }
2121
- case "PropertyDefinition": return `${e.static ? "static " : ""}${e.computed ? `[${A(e.key, t)}]` : e.key?.name || A(e.key, t)}${e.value ? ` = ${A(e.value, t)}` : ""}`;
2122
- case "NewExpression": return `new (${A(e.callee, t)})(${e.arguments ? e.arguments.map((e) => A(e, t)).join(", ") : ""})`;
2291
+ case "PropertyDefinition": return `${e.static ? "static " : ""}${e.computed ? `[${k(e.key, t)}]` : e.key?.name || k(e.key, t)}${e.value ? ` = ${k(e.value, t)}` : ""}`;
2292
+ case "NewExpression": return `new (${k(e.callee, t)})(${e.arguments ? e.arguments.map((e) => k(e, t)).join(", ") : ""})`;
2123
2293
  case "EmptyStatement":
2124
2294
  case "ImportDeclaration":
2125
2295
  case "ImportSpecifier":
2126
2296
  case "ImportDefaultSpecifier":
2127
2297
  case "ImportNamespaceSpecifier": return "";
2128
2298
  case "ParenthesizedExpression":
2129
- case "ChainExpression": return A(e.expression, t);
2299
+ case "ChainExpression": return k(e.expression, t);
2130
2300
  default: return "";
2131
2301
  }
2132
2302
  }
2133
2303
  //#endregion
2134
2304
  //#region src/index.ts
2135
- function j(e, t = !1) {
2136
- let n = new C(new x(new _(e)).parse()).transform(), r = new w(n).generate();
2305
+ function A(e, t = !1) {
2306
+ let n = new x(new y(new m(e)).parse()).transform(), r = new S(n).generate();
2137
2307
  return t && (console.log("--- Transformed AST ---"), console.log(JSON.stringify(n, null, 2)), console.log("--- Compiled Module ---"), console.log(JSON.stringify(r, null, 2))), r;
2138
2308
  }
2139
2309
  //#endregion
2140
- export { l as ASTNodeType, w as DriftGenerator, _ as DriftLexer, u as DriftLexerError, x as DriftParser, d as DriftParserError, C as DriftTransformer, c as ExprTokenKind, s as LexerStateKind, f as Opcode, o as TokenType, A as astToJS, j as compile, E as extractBindingNames, S as traverseTemplateAST };
2310
+ export { o as ASTNodeType, S as DriftGenerator, m as DriftLexer, s as DriftLexerError, y as DriftParser, c as DriftParserError, x as DriftTransformer, a as ExprTokenKind, i as LexerStateKind, l as Opcode, r as TokenType, k as astToJS, A as compile, w as extractBindingNames, b as traverseTemplateAST };