ripple 0.2.47 → 0.2.49

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.
@@ -7,736 +7,809 @@ import { regex_newline_characters } from '../../../utils/patterns.js';
7
7
  const parser = acorn.Parser.extend(tsPlugin({ allowSatisfies: true }), RipplePlugin());
8
8
 
9
9
  function convert_from_jsx(node) {
10
- if (node.type === 'JSXIdentifier') {
11
- node.type = 'Identifier';
12
- } else if (node.type === 'JSXMemberExpression') {
13
- node.type = 'MemberExpression';
14
- node.object = convert_from_jsx(node.object);
15
- node.property = convert_from_jsx(node.property);
16
- }
17
- return node;
10
+ if (node.type === 'JSXIdentifier') {
11
+ node.type = 'Identifier';
12
+ } else if (node.type === 'JSXMemberExpression') {
13
+ node.type = 'MemberExpression';
14
+ node.object = convert_from_jsx(node.object);
15
+ node.property = convert_from_jsx(node.property);
16
+ }
17
+ return node;
18
18
  }
19
19
 
20
20
  function RipplePlugin(config) {
21
- return (Parser) => {
22
- const original = acorn.Parser.prototype;
23
- const tt = Parser.tokTypes || acorn.tokTypes;
24
- const tc = Parser.tokContexts || acorn.tokContexts;
25
-
26
- class RippleParser extends Parser {
27
- #path = [];
28
-
29
- // Helper method to get the element name from a JSX identifier or member expression
30
- getElementName(node) {
31
- if (!node) return null;
32
- if (node.type === 'Identifier' || node.type === 'JSXIdentifier') {
33
- return node.name;
34
- } else if (node.type === 'MemberExpression' || node.type === 'JSXMemberExpression') {
35
- // For components like <Foo.Bar>, return "Foo.Bar"
36
- return this.getElementName(node.object) + '.' + this.getElementName(node.property);
37
- }
38
- return null;
39
- }
40
-
41
- // Override getTokenFromCode to handle @ as an identifier prefix
42
- getTokenFromCode(code) {
43
- if (code === 64) { // '@' character
44
- // Look ahead to see if this is followed by a valid identifier character
45
- if (this.pos + 1 < this.input.length) {
46
- const nextChar = this.input.charCodeAt(this.pos + 1);
47
- // Check if the next character can start an identifier
48
- if ((nextChar >= 65 && nextChar <= 90) || // A-Z
49
- (nextChar >= 97 && nextChar <= 122) || // a-z
50
- nextChar === 95 || nextChar === 36) { // _ or $
51
-
52
- // Check if we're in an expression context
53
- // In JSX expressions, inside parentheses, assignments, etc.
54
- // we want to treat @ as an identifier prefix rather than decorator
55
- const currentType = this.type;
56
- const inExpression = this.exprAllowed ||
57
- currentType === tt.braceL || // Inside { }
58
- currentType === tt.parenL || // Inside ( )
59
- currentType === tt.eq || // After =
60
- currentType === tt.comma || // After ,
61
- currentType === tt.colon || // After :
62
- currentType === tt.question || // After ?
63
- currentType === tt.logicalOR || // After ||
64
- currentType === tt.logicalAND || // After &&
65
- currentType === tt.dot || // After . (for member expressions like obj.@prop)
66
- currentType === tt.questionDot; // After ?. (for optional chaining like obj?.@prop)
67
-
68
- if (inExpression) {
69
- return this.readAtIdentifier();
70
- }
71
- }
72
- }
73
- }
74
- return super.getTokenFromCode(code);
75
- }
76
-
77
- // Read an @ prefixed identifier
78
- readAtIdentifier() {
79
- const start = this.pos;
80
- this.pos++; // skip '@'
81
-
82
- // Read the identifier part manually
83
- let word = '';
84
- while (this.pos < this.input.length) {
85
- const ch = this.input.charCodeAt(this.pos);
86
- if ((ch >= 65 && ch <= 90) || // A-Z
87
- (ch >= 97 && ch <= 122) || // a-z
88
- (ch >= 48 && ch <= 57) || // 0-9
89
- ch === 95 || ch === 36) { // _ or $
90
- word += this.input[this.pos++];
91
- } else {
92
- break;
93
- }
94
- }
95
-
96
- if (word === '') {
97
- this.raise(start, 'Invalid @ identifier');
98
- }
99
-
100
- // Return the full identifier including @
101
- return this.finishToken(tt.name, '@' + word);
102
- }
103
-
104
- // Override parseIdent to mark @ identifiers as tracked
105
- parseIdent(liberal) {
106
- const node = super.parseIdent(liberal);
107
- if (node.name && node.name.startsWith('@')) {
108
- node.name = node.name.slice(1); // Remove the '@' for internal use
109
- node.tracked = true;
110
- node.start++;
111
- const prev_pos = this.pos;
112
- this.pos = node.start;
113
- node.loc.start = this.curPosition();
114
- this.pos = prev_pos;
115
- }
116
- return node;
117
- }
118
-
119
- parseExportDefaultDeclaration() {
120
- // Check if this is "export default component"
121
- if (this.value === 'component') {
122
- const node = this.startNode();
123
- node.type = 'Component';
124
- node.css = null;
125
- node.default = true;
126
- this.next();
127
- this.enterScope(0);
128
-
129
- node.id = this.type.label === 'name' ? this.parseIdent() : null;
130
-
131
- this.parseFunctionParams(node);
132
- this.eat(tt.braceL);
133
- node.body = [];
134
- this.#path.push(node);
135
-
136
- this.parseTemplateBody(node.body);
137
-
138
- this.#path.pop();
139
- this.exitScope();
140
-
141
- this.next();
142
- this.finishNode(node, 'Component');
143
- this.awaitPos = 0;
144
-
145
- return node;
146
- }
147
-
148
- return super.parseExportDefaultDeclaration();
149
- }
150
-
151
- shouldParseExportStatement() {
152
- if (super.shouldParseExportStatement()) {
153
- return true;
154
- }
155
- if (this.value === 'component') {
156
- return true;
157
- }
158
- return this.type.keyword === 'var';
159
- }
160
-
161
- jsx_parseExpressionContainer() {
162
- let node = this.startNode();
163
- this.next();
164
-
165
- node.expression =
166
- this.type === tt.braceR ? this.jsx_parseEmptyExpression() : this.parseExpression();
167
- this.expect(tt.braceR);
168
- return this.finishNode(node, 'JSXExpressionContainer');
169
- }
170
-
171
- jsx_parseTupleContainer() {
172
- var t = this.startNode();
173
- return (
174
- this.next(),
175
- (t.expression =
176
- this.type === tt.bracketR ? this.jsx_parseEmptyExpression() : this.parseExpression()),
177
- this.expect(tt.bracketR),
178
- this.finishNode(t, 'JSXExpressionContainer')
179
- );
180
- }
181
-
182
- jsx_parseAttribute() {
183
- let node = this.startNode();
184
- const lookahead = this.lookahead();
185
-
186
- if (lookahead.type?.label === ':') {
187
- let id = this.startNode();
188
- id.name = this.value;
189
- node.name = id;
190
- this.next();
191
- this.finishNode(id, 'Identifier');
192
-
193
- if (this.lookahead().value !== '=') {
194
- this.unexpected();
195
- }
196
- this.next();
197
- if (this.lookahead().type !== tt.braceL) {
198
- this.unexpected();
199
- }
200
- this.next();
201
- const value = this.jsx_parseAttributeValue();
202
- const expression = value.expression;
203
- node.get = null;
204
- node.set = null;
205
-
206
- if (expression.type == 'SequenceExpression') {
207
- node.get = expression.expressions[0];
208
- node.set = expression.expressions[1];
209
- if (expression.expressions.length > 2) {
210
- this.unexpected();
211
- }
212
- } else {
213
- node.get = expression;
214
- }
215
-
216
- return this.finishNode(node, 'AccessorAttribute');
217
- }
218
-
219
- if (this.eat(tt.braceL)) {
220
- if (this.value === 'ref') {
221
- this.next();
222
- if (this.type === tt.braceR) {
223
- this.raise(this.start, '"ref" is a Ripple keyword and must be used in the form {ref fn}');
224
- }
225
- node.argument = this.parseMaybeAssign();
226
- this.expect(tt.braceR);
227
- return this.finishNode(node, 'RefAttribute');
228
- } else if (this.type === tt.ellipsis) {
229
- this.expect(tt.ellipsis);
230
- node.argument = this.parseMaybeAssign();
231
- this.expect(tt.braceR);
232
- return this.finishNode(node, 'SpreadAttribute');
233
- } else if (this.lookahead().type === tt.ellipsis) {
234
- this.expect(tt.ellipsis);
235
- node.argument = this.parseMaybeAssign();
236
- this.expect(tt.braceR);
237
- return this.finishNode(node, 'SpreadAttribute');
238
- } else {
239
- const id = this.parseIdentNode();
240
- id.tracked = false;
241
- if (id.name.startsWith('@')) {
242
- id.tracked = true;
243
- id.name = id.name.slice(1);
244
- }
245
- this.finishNode(id, 'Identifier');
246
- node.name = id;
247
- node.value = id;
248
- this.next();
249
- this.expect(tt.braceR);
250
- return this.finishNode(node, 'Attribute');
251
- }
252
- }
253
- node.name = this.jsx_parseNamespacedName();
254
- node.value = this.eat(tt.eq) ? this.jsx_parseAttributeValue() : null;
255
- return this.finishNode(node, 'JSXAttribute');
256
- }
257
-
258
- jsx_parseAttributeValue() {
259
- const tok = this.acornTypeScript.tokTypes;
260
-
261
- switch (this.type) {
262
- case tt.braceL:
263
- var t = this.jsx_parseExpressionContainer();
264
- return (
265
- 'JSXEmptyExpression' === t.expression.type &&
266
- this.raise(t.start, 'attributes must only be assigned a non-empty expression'),
267
- t
268
- );
269
- case tok.jsxTagStart:
270
- case tt.string:
271
- return this.parseExprAtom();
272
- default:
273
- this.raise(this.start, 'value should be either an expression or a quoted text');
274
- }
275
- }
276
-
277
- parseTryStatement(node) {
278
- this.next();
279
- node.block = this.parseBlock();
280
- node.handler = null;
281
- if (this.type === tt._catch) {
282
- var clause = this.startNode();
283
- this.next();
284
- if (this.eat(tt.parenL)) {
285
- clause.param = this.parseCatchClauseParam();
286
- } else {
287
- if (this.options.ecmaVersion < 10) {
288
- this.unexpected();
289
- }
290
- clause.param = null;
291
- this.enterScope(0);
292
- }
293
- clause.body = this.parseBlock(false);
294
- this.exitScope();
295
- node.handler = this.finishNode(clause, 'CatchClause');
296
- }
297
- node.finalizer = this.eat(tt._finally) ? this.parseBlock() : null;
298
-
299
- if (this.value === 'async') {
300
- this.next();
301
- node.async = this.parseBlock();
302
- } else {
303
- node.async = null;
304
- }
305
-
306
- if (!node.handler && !node.finalizer && !node.async) {
307
- this.raise(node.start, 'Missing catch or finally clause');
308
- }
309
- return this.finishNode(node, 'TryStatement');
310
- }
311
- jsx_readToken() {
312
- let out = '',
313
- chunkStart = this.pos;
314
- const tok = this.acornTypeScript.tokTypes;
315
-
316
- for (;;) {
317
- if (this.pos >= this.input.length) this.raise(this.start, 'Unterminated JSX contents');
318
- let ch = this.input.charCodeAt(this.pos);
319
-
320
- switch (ch) {
321
- case 60: // '<'
322
- case 123: // '{'
323
- if (ch === 60 && this.exprAllowed) {
324
- ++this.pos;
325
- return this.finishToken(tok.jsxTagStart);
326
- }
327
- if (ch === 123 && this.exprAllowed) {
328
- return this.getTokenFromCode(ch);
329
- }
330
- throw new Error('TODO: Invalid syntax');
331
-
332
- case 47: // '/'
333
- // Check if this is a comment (// or /*)
334
- if (this.input.charCodeAt(this.pos + 1) === 47) {
335
- // '//'
336
- // Line comment - handle it properly
337
- const commentStart = this.pos;
338
- const startLoc = this.curPosition();
339
- this.pos += 2;
340
-
341
- let commentText = '';
342
- while (this.pos < this.input.length) {
343
- const nextCh = this.input.charCodeAt(this.pos);
344
- if (acorn.isNewLine(nextCh)) break;
345
- commentText += this.input[this.pos];
346
- this.pos++;
347
- }
348
-
349
- const commentEnd = this.pos;
350
- const endLoc = this.curPosition();
351
-
352
- // Call onComment if it exists
353
- if (this.options.onComment) {
354
- this.options.onComment(
355
- false,
356
- commentText,
357
- commentStart,
358
- commentEnd,
359
- startLoc,
360
- endLoc,
361
- );
362
- }
363
-
364
- // Continue processing from current position
365
- break;
366
- } else if (this.input.charCodeAt(this.pos + 1) === 42) {
367
- // '/*'
368
- // Block comment - handle it properly
369
- const commentStart = this.pos;
370
- const startLoc = this.curPosition();
371
- this.pos += 2;
372
-
373
- let commentText = '';
374
- while (this.pos < this.input.length - 1) {
375
- if (
376
- this.input.charCodeAt(this.pos) === 42 &&
377
- this.input.charCodeAt(this.pos + 1) === 47
378
- ) {
379
- this.pos += 2;
380
- break;
381
- }
382
- commentText += this.input[this.pos];
383
- this.pos++;
384
- }
385
-
386
- const commentEnd = this.pos;
387
- const endLoc = this.curPosition();
388
-
389
- // Call onComment if it exists
390
- if (this.options.onComment) {
391
- this.options.onComment(
392
- true,
393
- commentText,
394
- commentStart,
395
- commentEnd,
396
- startLoc,
397
- endLoc,
398
- );
399
- }
400
-
401
- // Continue processing from current position
402
- break;
403
- }
404
- // If not a comment, fall through to default case
405
- this.context.push(tc.b_stat);
406
- this.exprAllowed = true;
407
- return original.readToken.call(this, ch);
408
-
409
- case 38: // '&'
410
- out += this.input.slice(chunkStart, this.pos);
411
- out += this.jsx_readEntity();
412
- chunkStart = this.pos;
413
- break;
414
-
415
- case 62: // '>'
416
- case 125: {
417
- // '}'
418
- if (
419
- ch === 125 &&
420
- (this.#path.length === 0 || this.#path.at(-1)?.type === 'Component')
421
- ) {
422
- return original.readToken.call(this, ch);
423
- }
424
- this.raise(
425
- this.pos,
426
- 'Unexpected token `' +
427
- this.input[this.pos] +
428
- '`. Did you mean `' +
429
- (ch === 62 ? '&gt;' : '&rbrace;') +
430
- '` or ' +
431
- '`{"' +
432
- this.input[this.pos] +
433
- '"}' +
434
- '`?',
435
- );
436
- }
437
-
438
- default:
439
- if (acorn.isNewLine(ch)) {
440
- out += this.input.slice(chunkStart, this.pos);
441
- out += this.jsx_readNewLine(true);
442
- chunkStart = this.pos;
443
- } else if (ch === 32 || ch === 9) {
444
- ++this.pos;
445
- } else {
446
- this.context.push(tc.b_stat);
447
- this.exprAllowed = true;
448
- return original.readToken.call(this, ch);
449
- }
450
- }
451
- }
452
- }
453
-
454
- parseElement() {
455
- const tok = this.acornTypeScript.tokTypes;
456
- // Adjust the start so we capture the `<` as part of the element
457
- const prev_pos = this.pos;
458
- this.pos = this.start - 1;
459
- const position = this.curPosition();
460
- this.pos = prev_pos;
461
-
462
- const element = this.startNode();
463
- element.start = position.index;
464
- element.loc.start = position;
465
- element.type = 'Element';
466
- this.#path.push(element);
467
- element.children = [];
468
- const open = this.jsx_parseOpeningElementAt();
469
- for (const attr of open.attributes) {
470
- if (attr.type === 'JSXAttribute') {
471
- attr.type = 'Attribute';
472
- if (attr.name.type === 'JSXIdentifier') {
473
- attr.name.type = 'Identifier';
474
- }
475
- if (attr.value.type === 'JSXExpressionContainer') {
476
- attr.value = attr.value.expression;
477
- }
478
- }
479
- }
480
- if (open.name.type === 'JSXIdentifier') {
481
- open.name.type = 'Identifier';
482
- }
483
-
484
- element.id = convert_from_jsx(open.name);
485
- element.attributes = open.attributes;
486
- element.selfClosing = open.selfClosing;
487
- element.metadata = {};
488
-
489
- if (element.selfClosing) {
490
- this.#path.pop();
491
-
492
- if (this.type.label === '</>/<=/>=') {
493
- this.pos--;
494
- this.next();
495
- }
496
- } else {
497
- if (open.name.name === 'style') {
498
- // jsx_parseOpeningElementAt treats ID selectors (ie. #myid) or type selectors (ie. div) as identifier and read it
499
- // So backtrack to the end of the <style> tag to make sure everything is included
500
- const start = open.end;
501
- const input = this.input.slice(start);
502
- const end = input.indexOf('</style>');
503
- const content = input.slice(0, end);
504
-
505
- const component = this.#path.findLast((n) => n.type === 'Component');
506
- if (component.css !== null) {
507
- throw new Error('Components can only have one style tag');
508
- }
509
- component.css = parse_style(content);
510
-
511
- const newLines = content.match(regex_newline_characters)?.length;
512
- if (newLines) {
513
- this.curLine = open.loc.end.line + newLines;
514
- this.lineStart = start + content.lastIndexOf('\n') + 1;
515
- }
516
- this.pos = start + content.length + 1;
517
-
518
- this.type = tok.jsxTagStart;
519
- this.next();
520
- if (this.value === '/') {
521
- this.next();
522
- this.jsx_parseElementName();
523
- this.exprAllowed = true;
524
- this.#path.pop();
525
- this.next();
526
- }
527
- // This node is used for Prettier, we don't actually need
528
- // the node for Ripple's transform process
529
- element.children = [component.css];
530
- // Ensure we escape JSX <tag></tag> context
531
- const tokContexts = this.acornTypeScript.tokContexts;
532
- const curContext = this.curContext();
533
-
534
- if (curContext === tokContexts.tc_expr) {
535
- this.context.pop();
536
- }
537
-
538
- this.finishNode(element, 'Element');
539
- return element;
540
- } else {
541
- this.enterScope(0);
542
- this.parseTemplateBody(element.children);
543
- this.exitScope();
544
-
545
- // Check if this element was properly closed
546
- // If we reach here and this element is still in the path, it means it was never closed
547
- if (this.#path[this.#path.length - 1] === element) {
548
- const tagName = this.getElementName(element.id);
549
- this.raise(this.start, `Unclosed tag '<${tagName}>'. Expected '</${tagName}>' before end of component.`);
550
- }
551
- }
552
- // Ensure we escape JSX <tag></tag> context
553
- const tokContexts = this.acornTypeScript.tokContexts;
554
- const curContext = this.curContext();
555
-
556
- if (curContext === tokContexts.tc_expr) {
557
- this.context.pop();
558
- }
559
- }
560
-
561
- this.finishNode(element, 'Element');
562
- return element;
563
- }
564
-
565
- parseSubscript(base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained, forInit) {
566
- if (this.value === '<' && this.#path.findLast((n) => n.type === 'Component')) {
567
- // Check if this looks like JSX by looking ahead
568
- const ahead = this.lookahead();
569
- const curContext = this.curContext();
570
- if (
571
- curContext.token !== '(' &&
572
- (ahead.type.label === 'name' || ahead.value === '/' || ahead.value === '>')
573
- ) {
574
- // This is JSX, rewind to the end of the object expression
575
- // and let ASI handle the semicolon insertion naturally
576
- this.pos = base.end;
577
- this.type = tt.braceR;
578
- this.value = '}';
579
- this.start = base.end - 1;
580
- this.end = base.end;
581
- const position = this.curPosition();
582
- this.startLoc = position;
583
- this.endLoc = position;
584
- // Avoid triggering onComment handlers, as they will have
585
- // already been triggered when parsing the subscript before
586
- const onComment = this.options.onComment;
587
- this.options.onComment = () => {};
588
- this.next();
589
- this.options.onComment = onComment;
590
-
591
- return base;
592
- }
593
- }
594
- return super.parseSubscript(
595
- base,
596
- startPos,
597
- startLoc,
598
- noCalls,
599
- maybeAsyncArrow,
600
- optionalChained,
601
- forInit,
602
- );
603
- }
604
-
605
- parseTemplateBody(body) {
606
- var inside_func =
607
- this.context.some((n) => n.token === 'function') || this.scopeStack.length > 1;
608
-
609
- if (!inside_func) {
610
- if (this.type.label === 'return') {
611
- throw new Error('`return` statements are not allowed in components');
612
- }
613
- if (this.type.label === 'continue') {
614
- throw new Error('`continue` statements are not allowed in components');
615
- }
616
- if (this.type.label === 'break') {
617
- throw new Error('`break` statements are not allowed in components');
618
- }
619
- }
620
-
621
- if (this.type.label === '{') {
622
- const node = this.jsx_parseExpressionContainer();
623
- node.type = 'Text';
624
- body.push(node);
625
- } else if (this.type.label === '}') {
626
- return;
627
- } else if (this.type.label === 'jsxTagStart') {
628
- this.next();
629
- if (this.value === '/') {
630
- this.next();
631
- const closingTag = this.jsx_parseElementName();
632
- this.exprAllowed = true;
633
-
634
- // Validate that the closing tag matches the opening tag
635
- const currentElement = this.#path[this.#path.length - 1];
636
- if (!currentElement || currentElement.type !== 'Element') {
637
- this.raise(this.start, 'Unexpected closing tag');
638
- }
639
-
640
- const openingTagName = this.getElementName(currentElement.id);
641
- const closingTagName = this.getElementName(closingTag);
642
-
643
- if (openingTagName !== closingTagName) {
644
- this.raise(this.start, `Expected closing tag to match opening tag. Expected '</${openingTagName}>' but found '</${closingTagName}>'`);
645
- }
646
-
647
- this.#path.pop();
648
- this.next();
649
- return;
650
- }
651
- const node = this.parseElement();
652
- if (node !== null) {
653
- body.push(node);
654
- }
655
- } else {
656
- const node = this.parseStatement(null);
657
- body.push(node);
658
- }
659
- this.parseTemplateBody(body);
660
- }
661
-
662
- parseStatement(context, topLevel, exports) {
663
- const tok = this.acornTypeScript.tokContexts;
664
-
665
- if (
666
- context !== 'for' &&
667
- context !== 'if' &&
668
- this.context.at(-1) === tc.b_stat &&
669
- this.type === tt.braceL &&
670
- this.context.some((c) => c === tok.tc_expr)
671
- ) {
672
- this.next();
673
- const node = this.jsx_parseExpressionContainer();
674
- node.type = 'Text';
675
- this.next();
676
- this.context.pop();
677
- this.context.pop();
678
- return node;
679
- }
680
-
681
- if (this.value === 'component') {
682
- const node = this.startNode();
683
- node.type = 'Component';
684
- node.css = null;
685
- this.next();
686
- this.enterScope(0);
687
- node.id = this.parseIdent();
688
- this.parseFunctionParams(node);
689
- this.eat(tt.braceL);
690
- node.body = [];
691
- this.#path.push(node);
692
-
693
- this.parseTemplateBody(node.body);
694
-
695
- this.#path.pop();
696
- this.exitScope();
697
-
698
- this.next();
699
- this.finishNode(node, 'Component');
700
- this.awaitPos = 0;
701
-
702
- return node;
703
- }
704
-
705
- return super.parseStatement(context, topLevel, exports);
706
- }
707
-
708
- parseBlock(createNewLexicalScope, node, exitStrict) {
709
- const parent = this.#path.at(-1);
710
-
711
- if (parent?.type === 'Component' || parent?.type === 'Element') {
712
- if (createNewLexicalScope === void 0) createNewLexicalScope = true;
713
- if (node === void 0) node = this.startNode();
714
-
715
- node.body = [];
716
- this.expect(tt.braceL);
717
- if (createNewLexicalScope) {
718
- this.enterScope(0);
719
- }
720
- this.parseTemplateBody(node.body);
721
-
722
- if (exitStrict) {
723
- this.strict = false;
724
- }
725
- this.exprAllowed = true;
726
-
727
- this.next();
728
- if (createNewLexicalScope) {
729
- this.exitScope();
730
- }
731
- return this.finishNode(node, 'BlockStatement');
732
- }
733
-
734
- return super.parseBlock(createNewLexicalScope, node, exitStrict);
735
- }
736
- }
737
-
738
- return RippleParser;
739
- };
21
+ return (Parser) => {
22
+ const original = acorn.Parser.prototype;
23
+ const tt = Parser.tokTypes || acorn.tokTypes;
24
+ const tc = Parser.tokContexts || acorn.tokContexts;
25
+
26
+ class RippleParser extends Parser {
27
+ #path = [];
28
+ skip_decorator = false;
29
+
30
+ // Helper method to get the element name from a JSX identifier or member expression
31
+ getElementName(node) {
32
+ if (!node) return null;
33
+ if (node.type === 'Identifier' || node.type === 'JSXIdentifier') {
34
+ return node.name;
35
+ } else if (node.type === 'MemberExpression' || node.type === 'JSXMemberExpression') {
36
+ // For components like <Foo.Bar>, return "Foo.Bar"
37
+ return this.getElementName(node.object) + '.' + this.getElementName(node.property);
38
+ }
39
+ return null;
40
+ }
41
+
42
+ // Override getTokenFromCode to handle @ as an identifier prefix
43
+ getTokenFromCode(code) {
44
+ if (code === 64) {
45
+ // '@' character
46
+ // Look ahead to see if this is followed by a valid identifier character
47
+ if (this.pos + 1 < this.input.length) {
48
+ const nextChar = this.input.charCodeAt(this.pos + 1);
49
+ // Check if the next character can start an identifier
50
+ if (
51
+ (nextChar >= 65 && nextChar <= 90) || // A-Z
52
+ (nextChar >= 97 && nextChar <= 122) || // a-z
53
+ nextChar === 95 ||
54
+ nextChar === 36
55
+ ) {
56
+ // _ or $
57
+
58
+ // Check if we're in an expression context
59
+ // In JSX expressions, inside parentheses, assignments, etc.
60
+ // we want to treat @ as an identifier prefix rather than decorator
61
+ const currentType = this.type;
62
+ const inExpression =
63
+ this.exprAllowed ||
64
+ currentType === tt.braceL || // Inside { }
65
+ currentType === tt.parenL || // Inside ( )
66
+ currentType === tt.eq || // After =
67
+ currentType === tt.comma || // After ,
68
+ currentType === tt.colon || // After :
69
+ currentType === tt.question || // After ?
70
+ currentType === tt.logicalOR || // After ||
71
+ currentType === tt.logicalAND || // After &&
72
+ currentType === tt.dot || // After . (for member expressions like obj.@prop)
73
+ currentType === tt.questionDot; // After ?. (for optional chaining like obj?.@prop)
74
+
75
+ if (inExpression) {
76
+ return this.readAtIdentifier();
77
+ }
78
+ }
79
+ }
80
+ }
81
+ return super.getTokenFromCode(code);
82
+ }
83
+
84
+ // Read an @ prefixed identifier
85
+ readAtIdentifier() {
86
+ const start = this.pos;
87
+ this.pos++; // skip '@'
88
+
89
+ // Read the identifier part manually
90
+ let word = '';
91
+ while (this.pos < this.input.length) {
92
+ const ch = this.input.charCodeAt(this.pos);
93
+ if (
94
+ (ch >= 65 && ch <= 90) || // A-Z
95
+ (ch >= 97 && ch <= 122) || // a-z
96
+ (ch >= 48 && ch <= 57) || // 0-9
97
+ ch === 95 ||
98
+ ch === 36
99
+ ) {
100
+ // _ or $
101
+ word += this.input[this.pos++];
102
+ } else {
103
+ break;
104
+ }
105
+ }
106
+
107
+ if (word === '') {
108
+ this.raise(start, 'Invalid @ identifier');
109
+ }
110
+
111
+ // Return the full identifier including @
112
+ return this.finishToken(tt.name, '@' + word);
113
+ }
114
+
115
+ // Override parseIdent to mark @ identifiers as tracked
116
+ parseIdent(liberal) {
117
+ const node = super.parseIdent(liberal);
118
+ if (node.name && node.name.startsWith('@')) {
119
+ node.name = node.name.slice(1); // Remove the '@' for internal use
120
+ node.tracked = true;
121
+ node.start++;
122
+ const prev_pos = this.pos;
123
+ this.pos = node.start;
124
+ node.loc.start = this.curPosition();
125
+ this.pos = prev_pos;
126
+ }
127
+ return node;
128
+ }
129
+
130
+ parseExportDefaultDeclaration() {
131
+ // Check if this is "export default component"
132
+ if (this.value === 'component') {
133
+ const node = this.startNode();
134
+ node.type = 'Component';
135
+ node.css = null;
136
+ node.default = true;
137
+ this.next();
138
+ this.enterScope(0);
139
+
140
+ node.id = this.type.label === 'name' ? this.parseIdent() : null;
141
+
142
+ this.parseFunctionParams(node);
143
+ this.eat(tt.braceL);
144
+ node.body = [];
145
+ this.#path.push(node);
146
+
147
+ this.parseTemplateBody(node.body);
148
+
149
+ this.#path.pop();
150
+ this.exitScope();
151
+
152
+ this.next();
153
+ this.finishNode(node, 'Component');
154
+ this.awaitPos = 0;
155
+
156
+ return node;
157
+ }
158
+
159
+ return super.parseExportDefaultDeclaration();
160
+ }
161
+
162
+ shouldParseExportStatement() {
163
+ if (super.shouldParseExportStatement()) {
164
+ return true;
165
+ }
166
+ if (this.value === 'component') {
167
+ return true;
168
+ }
169
+ return this.type.keyword === 'var';
170
+ }
171
+
172
+ jsx_parseExpressionContainer() {
173
+ let node = this.startNode();
174
+ this.next();
175
+
176
+ node.expression =
177
+ this.type === tt.braceR ? this.jsx_parseEmptyExpression() : this.parseExpression();
178
+ this.expect(tt.braceR);
179
+ return this.finishNode(node, 'JSXExpressionContainer');
180
+ }
181
+
182
+ jsx_parseTupleContainer() {
183
+ var t = this.startNode();
184
+ return (
185
+ this.next(),
186
+ (t.expression =
187
+ this.type === tt.bracketR ? this.jsx_parseEmptyExpression() : this.parseExpression()),
188
+ this.expect(tt.bracketR),
189
+ this.finishNode(t, 'JSXExpressionContainer')
190
+ );
191
+ }
192
+
193
+ jsx_parseAttribute() {
194
+ let node = this.startNode();
195
+ const lookahead = this.lookahead();
196
+
197
+ if (lookahead.type?.label === ':') {
198
+ let id = this.startNode();
199
+ id.name = this.value;
200
+ node.name = id;
201
+ this.next();
202
+ this.finishNode(id, 'Identifier');
203
+
204
+ if (this.lookahead().value !== '=') {
205
+ this.unexpected();
206
+ }
207
+ this.next();
208
+ if (this.lookahead().type !== tt.braceL) {
209
+ this.unexpected();
210
+ }
211
+ this.next();
212
+ const value = this.jsx_parseAttributeValue();
213
+ const expression = value.expression;
214
+ node.get = null;
215
+ node.set = null;
216
+
217
+ if (expression.type == 'SequenceExpression') {
218
+ node.get = expression.expressions[0];
219
+ node.set = expression.expressions[1];
220
+ if (expression.expressions.length > 2) {
221
+ this.unexpected();
222
+ }
223
+ } else {
224
+ node.get = expression;
225
+ }
226
+
227
+ return this.finishNode(node, 'AccessorAttribute');
228
+ }
229
+
230
+ if (this.eat(tt.braceL)) {
231
+ if (this.value === 'ref') {
232
+ this.next();
233
+ if (this.type === tt.braceR) {
234
+ this.raise(
235
+ this.start,
236
+ '"ref" is a Ripple keyword and must be used in the form {ref fn}',
237
+ );
238
+ }
239
+ node.argument = this.parseMaybeAssign();
240
+ this.expect(tt.braceR);
241
+ return this.finishNode(node, 'RefAttribute');
242
+ } else if (this.type === tt.ellipsis) {
243
+ this.expect(tt.ellipsis);
244
+ node.argument = this.parseMaybeAssign();
245
+ this.expect(tt.braceR);
246
+ return this.finishNode(node, 'SpreadAttribute');
247
+ } else if (this.lookahead().type === tt.ellipsis) {
248
+ this.expect(tt.ellipsis);
249
+ node.argument = this.parseMaybeAssign();
250
+ this.expect(tt.braceR);
251
+ return this.finishNode(node, 'SpreadAttribute');
252
+ } else {
253
+ const id = this.parseIdentNode();
254
+ id.tracked = false;
255
+ if (id.name.startsWith('@')) {
256
+ id.tracked = true;
257
+ id.name = id.name.slice(1);
258
+ }
259
+ this.finishNode(id, 'Identifier');
260
+ node.name = id;
261
+ node.value = id;
262
+ this.next();
263
+ this.expect(tt.braceR);
264
+ return this.finishNode(node, 'Attribute');
265
+ }
266
+ }
267
+ node.name = this.jsx_parseNamespacedName();
268
+ node.value = this.eat(tt.eq) ? this.jsx_parseAttributeValue() : null;
269
+ return this.finishNode(node, 'JSXAttribute');
270
+ }
271
+
272
+ jsx_parseAttributeValue() {
273
+ const tok = this.acornTypeScript.tokTypes;
274
+
275
+ switch (this.type) {
276
+ case tt.braceL:
277
+ var t = this.jsx_parseExpressionContainer();
278
+ return (
279
+ 'JSXEmptyExpression' === t.expression.type &&
280
+ this.raise(t.start, 'attributes must only be assigned a non-empty expression'),
281
+ t
282
+ );
283
+ case tok.jsxTagStart:
284
+ case tt.string:
285
+ return this.parseExprAtom();
286
+ default:
287
+ this.raise(this.start, 'value should be either an expression or a quoted text');
288
+ }
289
+ }
290
+
291
+ parseTryStatement(node) {
292
+ this.next();
293
+ node.block = this.parseBlock();
294
+ node.handler = null;
295
+ if (this.type === tt._catch) {
296
+ var clause = this.startNode();
297
+ this.next();
298
+ if (this.eat(tt.parenL)) {
299
+ clause.param = this.parseCatchClauseParam();
300
+ } else {
301
+ if (this.options.ecmaVersion < 10) {
302
+ this.unexpected();
303
+ }
304
+ clause.param = null;
305
+ this.enterScope(0);
306
+ }
307
+ clause.body = this.parseBlock(false);
308
+ this.exitScope();
309
+ node.handler = this.finishNode(clause, 'CatchClause');
310
+ }
311
+ node.finalizer = this.eat(tt._finally) ? this.parseBlock() : null;
312
+
313
+ if (this.value === 'async') {
314
+ this.next();
315
+ node.async = this.parseBlock();
316
+ } else {
317
+ node.async = null;
318
+ }
319
+
320
+ if (!node.handler && !node.finalizer && !node.async) {
321
+ this.raise(node.start, 'Missing catch or finally clause');
322
+ }
323
+ return this.finishNode(node, 'TryStatement');
324
+ }
325
+ jsx_readToken() {
326
+ let out = '',
327
+ chunkStart = this.pos;
328
+ const tok = this.acornTypeScript.tokTypes;
329
+
330
+ for (;;) {
331
+ if (this.pos >= this.input.length) this.raise(this.start, 'Unterminated JSX contents');
332
+ let ch = this.input.charCodeAt(this.pos);
333
+
334
+ switch (ch) {
335
+ case 60: // '<'
336
+ case 123: // '{'
337
+ if (ch === 60 && this.exprAllowed) {
338
+ ++this.pos;
339
+ return this.finishToken(tok.jsxTagStart);
340
+ }
341
+ if (ch === 123 && this.exprAllowed) {
342
+ return this.getTokenFromCode(ch);
343
+ }
344
+ throw new Error('TODO: Invalid syntax');
345
+
346
+ case 47: // '/'
347
+ // Check if this is a comment (// or /*)
348
+ if (this.input.charCodeAt(this.pos + 1) === 47) {
349
+ // '//'
350
+ // Line comment - handle it properly
351
+ const commentStart = this.pos;
352
+ const startLoc = this.curPosition();
353
+ this.pos += 2;
354
+
355
+ let commentText = '';
356
+ while (this.pos < this.input.length) {
357
+ const nextCh = this.input.charCodeAt(this.pos);
358
+ if (acorn.isNewLine(nextCh)) break;
359
+ commentText += this.input[this.pos];
360
+ this.pos++;
361
+ }
362
+
363
+ const commentEnd = this.pos;
364
+ const endLoc = this.curPosition();
365
+
366
+ // Call onComment if it exists
367
+ if (this.options.onComment) {
368
+ this.options.onComment(
369
+ false,
370
+ commentText,
371
+ commentStart,
372
+ commentEnd,
373
+ startLoc,
374
+ endLoc,
375
+ );
376
+ }
377
+
378
+ // Continue processing from current position
379
+ break;
380
+ } else if (this.input.charCodeAt(this.pos + 1) === 42) {
381
+ // '/*'
382
+ // Block comment - handle it properly
383
+ const commentStart = this.pos;
384
+ const startLoc = this.curPosition();
385
+ this.pos += 2;
386
+
387
+ let commentText = '';
388
+ while (this.pos < this.input.length - 1) {
389
+ if (
390
+ this.input.charCodeAt(this.pos) === 42 &&
391
+ this.input.charCodeAt(this.pos + 1) === 47
392
+ ) {
393
+ this.pos += 2;
394
+ break;
395
+ }
396
+ commentText += this.input[this.pos];
397
+ this.pos++;
398
+ }
399
+
400
+ const commentEnd = this.pos;
401
+ const endLoc = this.curPosition();
402
+
403
+ // Call onComment if it exists
404
+ if (this.options.onComment) {
405
+ this.options.onComment(
406
+ true,
407
+ commentText,
408
+ commentStart,
409
+ commentEnd,
410
+ startLoc,
411
+ endLoc,
412
+ );
413
+ }
414
+
415
+ // Continue processing from current position
416
+ break;
417
+ }
418
+ // If not a comment, fall through to default case
419
+ this.context.push(tc.b_stat);
420
+ this.exprAllowed = true;
421
+ return original.readToken.call(this, ch);
422
+
423
+ case 38: // '&'
424
+ out += this.input.slice(chunkStart, this.pos);
425
+ out += this.jsx_readEntity();
426
+ chunkStart = this.pos;
427
+ break;
428
+
429
+ case 62: // '>'
430
+ case 125: {
431
+ // '}'
432
+ if (
433
+ ch === 125 &&
434
+ (this.#path.length === 0 || this.#path.at(-1)?.type === 'Component')
435
+ ) {
436
+ return original.readToken.call(this, ch);
437
+ }
438
+ this.raise(
439
+ this.pos,
440
+ 'Unexpected token `' +
441
+ this.input[this.pos] +
442
+ '`. Did you mean `' +
443
+ (ch === 62 ? '&gt;' : '&rbrace;') +
444
+ '` or ' +
445
+ '`{"' +
446
+ this.input[this.pos] +
447
+ '"}' +
448
+ '`?',
449
+ );
450
+ }
451
+
452
+ default:
453
+ if (acorn.isNewLine(ch)) {
454
+ out += this.input.slice(chunkStart, this.pos);
455
+ out += this.jsx_readNewLine(true);
456
+ chunkStart = this.pos;
457
+ } else if (ch === 32 || ch === 9) {
458
+ ++this.pos;
459
+ } else {
460
+ this.context.push(tc.b_stat);
461
+ this.exprAllowed = true;
462
+ return original.readToken.call(this, ch);
463
+ }
464
+ }
465
+ }
466
+ }
467
+
468
+ parseElement() {
469
+ const tok = this.acornTypeScript.tokTypes;
470
+ // Adjust the start so we capture the `<` as part of the element
471
+ const prev_pos = this.pos;
472
+ this.pos = this.start - 1;
473
+ const position = this.curPosition();
474
+ this.pos = prev_pos;
475
+
476
+ const element = this.startNode();
477
+ element.start = position.index;
478
+ element.loc.start = position;
479
+ element.type = 'Element';
480
+ this.#path.push(element);
481
+ element.children = [];
482
+ const open = this.jsx_parseOpeningElementAt();
483
+ for (const attr of open.attributes) {
484
+ if (attr.type === 'JSXAttribute') {
485
+ attr.type = 'Attribute';
486
+ if (attr.name.type === 'JSXIdentifier') {
487
+ attr.name.type = 'Identifier';
488
+ }
489
+ if (attr.value.type === 'JSXExpressionContainer') {
490
+ attr.value = attr.value.expression;
491
+ }
492
+ }
493
+ }
494
+ if (open.name.type === 'JSXIdentifier') {
495
+ open.name.type = 'Identifier';
496
+ }
497
+
498
+ element.id = convert_from_jsx(open.name);
499
+ element.attributes = open.attributes;
500
+ element.selfClosing = open.selfClosing;
501
+ element.metadata = {};
502
+
503
+ if (element.selfClosing) {
504
+ this.#path.pop();
505
+
506
+ if (this.type.label === '</>/<=/>=') {
507
+ this.pos--;
508
+ this.next();
509
+ }
510
+ } else {
511
+ if (open.name.name === 'style') {
512
+ // jsx_parseOpeningElementAt treats ID selectors (ie. #myid) or type selectors (ie. div) as identifier and read it
513
+ // So backtrack to the end of the <style> tag to make sure everything is included
514
+ const start = open.end;
515
+ const input = this.input.slice(start);
516
+ const end = input.indexOf('</style>');
517
+ const content = input.slice(0, end);
518
+
519
+ const component = this.#path.findLast((n) => n.type === 'Component');
520
+ if (component.css !== null) {
521
+ throw new Error('Components can only have one style tag');
522
+ }
523
+ component.css = parse_style(content);
524
+
525
+ const newLines = content.match(regex_newline_characters)?.length;
526
+ if (newLines) {
527
+ this.curLine = open.loc.end.line + newLines;
528
+ this.lineStart = start + content.lastIndexOf('\n') + 1;
529
+ }
530
+ this.pos = start + content.length + 1;
531
+
532
+ this.type = tok.jsxTagStart;
533
+ this.next();
534
+ if (this.value === '/') {
535
+ this.next();
536
+ this.jsx_parseElementName();
537
+ this.exprAllowed = true;
538
+ this.#path.pop();
539
+ this.next();
540
+ }
541
+ // This node is used for Prettier, we don't actually need
542
+ // the node for Ripple's transform process
543
+ element.children = [component.css];
544
+ // Ensure we escape JSX <tag></tag> context
545
+ const tokContexts = this.acornTypeScript.tokContexts;
546
+ const curContext = this.curContext();
547
+
548
+ if (curContext === tokContexts.tc_expr) {
549
+ this.context.pop();
550
+ }
551
+
552
+ this.finishNode(element, 'Element');
553
+ return element;
554
+ } else {
555
+ this.enterScope(0);
556
+ this.parseTemplateBody(element.children);
557
+ this.exitScope();
558
+
559
+ // Check if this element was properly closed
560
+ // If we reach here and this element is still in the path, it means it was never closed
561
+ if (this.#path[this.#path.length - 1] === element) {
562
+ const tagName = this.getElementName(element.id);
563
+ this.raise(
564
+ this.start,
565
+ `Unclosed tag '<${tagName}>'. Expected '</${tagName}>' before end of component.`,
566
+ );
567
+ }
568
+ }
569
+ // Ensure we escape JSX <tag></tag> context
570
+ const tokContexts = this.acornTypeScript.tokContexts;
571
+ const curContext = this.curContext();
572
+
573
+ if (curContext === tokContexts.tc_expr) {
574
+ this.context.pop();
575
+ }
576
+ }
577
+
578
+ this.finishNode(element, 'Element');
579
+ return element;
580
+ }
581
+
582
+ parseSubscript(base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained, forInit) {
583
+ const prev_char = this.input.at(this.pos - 2);
584
+
585
+ if (
586
+ this.value === '<' &&
587
+ (prev_char === ' ' || prev_char === '\t') &&
588
+ this.#path.findLast((n) => n.type === 'Component')
589
+ ) {
590
+ this.input.charCodeAt(this.pos);
591
+ // Check if this looks like JSX by looking ahead
592
+ const ahead = this.lookahead();
593
+ const curContext = this.curContext();
594
+ if (
595
+ curContext.token !== '(' &&
596
+ (ahead.type.label === 'name' || ahead.value === '/' || ahead.value === '>')
597
+ ) {
598
+ // This is JSX, rewind to the end of the object expression
599
+ // and let ASI handle the semicolon insertion naturally
600
+ this.pos = base.end;
601
+ this.type = tt.braceR;
602
+ this.value = '}';
603
+ this.start = base.end - 1;
604
+ this.end = base.end;
605
+ const position = this.curPosition();
606
+ this.startLoc = position;
607
+ this.endLoc = position;
608
+ // Avoid triggering onComment handlers, as they will have
609
+ // already been triggered when parsing the subscript before
610
+ const onComment = this.options.onComment;
611
+ this.options.onComment = () => {};
612
+ this.next();
613
+ this.options.onComment = onComment;
614
+
615
+ return base;
616
+ }
617
+ }
618
+ return super.parseSubscript(
619
+ base,
620
+ startPos,
621
+ startLoc,
622
+ noCalls,
623
+ maybeAsyncArrow,
624
+ optionalChained,
625
+ forInit,
626
+ );
627
+ }
628
+
629
+ parseTemplateBody(body) {
630
+ var inside_func =
631
+ this.context.some((n) => n.token === 'function') || this.scopeStack.length > 1;
632
+
633
+ if (!inside_func) {
634
+ if (this.type.label === 'return') {
635
+ throw new Error('`return` statements are not allowed in components');
636
+ }
637
+ if (this.type.label === 'continue') {
638
+ throw new Error('`continue` statements are not allowed in components');
639
+ }
640
+ if (this.type.label === 'break') {
641
+ throw new Error('`break` statements are not allowed in components');
642
+ }
643
+ }
644
+
645
+ if (this.type.label === '{') {
646
+ const node = this.jsx_parseExpressionContainer();
647
+ node.type = 'Text';
648
+ body.push(node);
649
+ } else if (this.type.label === '}') {
650
+ return;
651
+ } else if (this.type.label === 'jsxTagStart') {
652
+ this.next();
653
+ if (this.value === '/') {
654
+ this.next();
655
+ const closingTag = this.jsx_parseElementName();
656
+ this.exprAllowed = true;
657
+
658
+ // Validate that the closing tag matches the opening tag
659
+ const currentElement = this.#path[this.#path.length - 1];
660
+ if (!currentElement || currentElement.type !== 'Element') {
661
+ this.raise(this.start, 'Unexpected closing tag');
662
+ }
663
+
664
+ const openingTagName = this.getElementName(currentElement.id);
665
+ const closingTagName = this.getElementName(closingTag);
666
+
667
+ if (openingTagName !== closingTagName) {
668
+ this.raise(
669
+ this.start,
670
+ `Expected closing tag to match opening tag. Expected '</${openingTagName}>' but found '</${closingTagName}>'`,
671
+ );
672
+ }
673
+
674
+ this.#path.pop();
675
+ this.next();
676
+ return;
677
+ }
678
+ const node = this.parseElement();
679
+ if (node !== null) {
680
+ body.push(node);
681
+ }
682
+ } else {
683
+ const node = this.parseStatement(null);
684
+ body.push(node);
685
+ }
686
+ this.parseTemplateBody(body);
687
+ }
688
+
689
+ parseStatement(context, topLevel, exports) {
690
+ const tok = this.acornTypeScript.tokContexts;
691
+
692
+ if (
693
+ context !== 'for' &&
694
+ context !== 'if' &&
695
+ this.context.at(-1) === tc.b_stat &&
696
+ this.type === tt.braceL &&
697
+ this.context.some((c) => c === tok.tc_expr)
698
+ ) {
699
+ this.next();
700
+ const node = this.jsx_parseExpressionContainer();
701
+ node.type = 'Text';
702
+ this.next();
703
+ this.context.pop();
704
+ this.context.pop();
705
+ return node;
706
+ }
707
+
708
+ if (this.value === 'component') {
709
+ const node = this.startNode();
710
+ node.type = 'Component';
711
+ node.css = null;
712
+ this.next();
713
+ this.enterScope(0);
714
+ node.id = this.parseIdent();
715
+ this.parseFunctionParams(node);
716
+ this.eat(tt.braceL);
717
+ node.body = [];
718
+ this.#path.push(node);
719
+
720
+ this.parseTemplateBody(node.body);
721
+
722
+ this.#path.pop();
723
+ this.exitScope();
724
+
725
+ this.next();
726
+ this.finishNode(node, 'Component');
727
+ this.awaitPos = 0;
728
+
729
+ return node;
730
+ }
731
+
732
+ if (this.type.label === '@') {
733
+ // Try to parse as an expression statement first using tryParse
734
+ // This allows us to handle Ripple @ syntax like @count++ without
735
+ // interfering with legitimate decorator syntax
736
+ this.skip_decorator = true;
737
+ const expressionResult = this.tryParse(() => {
738
+ const node = this.startNode();
739
+ this.next();
740
+ // Force expression context to ensure @ is tokenized correctly
741
+ const oldExprAllowed = this.exprAllowed;
742
+ this.exprAllowed = true;
743
+ node.expression = this.parseExpression();
744
+
745
+ if (node.expression.type === 'UpdateExpression') {
746
+ let object = node.expression.argument;
747
+ while (object.type === 'MemberExpression') {
748
+ object = object.object;
749
+ }
750
+ if (object.type === 'Identifier') {
751
+ object.tracked = true;
752
+ }
753
+ } else if (node.expression.type === 'AssignmentExpression') {
754
+ let object = node.expression.left;
755
+ while (object.type === 'MemberExpression') {
756
+ object = object.object;
757
+ }
758
+ if (object.type === 'Identifier') {
759
+ object.tracked = true;
760
+ }
761
+ } else if (node.expression.type === 'Identifier') {
762
+ node.expression.tracked = true;
763
+ } else {
764
+ // TODO?
765
+ }
766
+
767
+ this.exprAllowed = oldExprAllowed;
768
+ return this.finishNode(node, 'ExpressionStatement');
769
+ });
770
+ this.skip_decorator = false;
771
+
772
+ // If parsing as expression statement succeeded, use that result
773
+ if (expressionResult.node) {
774
+ return expressionResult.node;
775
+ }
776
+ }
777
+
778
+ return super.parseStatement(context, topLevel, exports);
779
+ }
780
+
781
+ parseBlock(createNewLexicalScope, node, exitStrict) {
782
+ const parent = this.#path.at(-1);
783
+
784
+ if (parent?.type === 'Component' || parent?.type === 'Element') {
785
+ if (createNewLexicalScope === void 0) createNewLexicalScope = true;
786
+ if (node === void 0) node = this.startNode();
787
+
788
+ node.body = [];
789
+ this.expect(tt.braceL);
790
+ if (createNewLexicalScope) {
791
+ this.enterScope(0);
792
+ }
793
+ this.parseTemplateBody(node.body);
794
+
795
+ if (exitStrict) {
796
+ this.strict = false;
797
+ }
798
+ this.exprAllowed = true;
799
+
800
+ this.next();
801
+ if (createNewLexicalScope) {
802
+ this.exitScope();
803
+ }
804
+ return this.finishNode(node, 'BlockStatement');
805
+ }
806
+
807
+ return super.parseBlock(createNewLexicalScope, node, exitStrict);
808
+ }
809
+ }
810
+
811
+ return RippleParser;
812
+ };
740
813
  }
741
814
 
742
815
  /**
@@ -748,115 +821,115 @@ function RipplePlugin(config) {
748
821
  * @param {number} index
749
822
  */
750
823
  function get_comment_handlers(source, comments, index = 0) {
751
- return {
752
- onComment: (block, value, start, end, start_loc, end_loc) => {
753
- if (block && /\n/.test(value)) {
754
- let a = start;
755
- while (a > 0 && source[a - 1] !== '\n') a -= 1;
756
-
757
- let b = a;
758
- while (/[ \t]/.test(source[b])) b += 1;
759
-
760
- const indentation = source.slice(a, b);
761
- value = value.replace(new RegExp(`^${indentation}`, 'gm'), '');
762
- }
763
-
764
- comments.push({
765
- type: block ? 'Block' : 'Line',
766
- value,
767
- start,
768
- end,
769
- loc: {
770
- start: /** @type {import('acorn').Position} */ (start_loc),
771
- end: /** @type {import('acorn').Position} */ (end_loc),
772
- },
773
- });
774
- },
775
- add_comments: (ast) => {
776
- if (comments.length === 0) return;
777
-
778
- comments = comments
779
- .filter((comment) => comment.start >= index)
780
- .map(({ type, value, start, end }) => ({ type, value, start, end }));
781
-
782
- walk(ast, null, {
783
- _(node, { next, path }) {
784
- let comment;
785
-
786
- while (comments[0] && comments[0].start < node.start) {
787
- comment = /** @type {CommentWithLocation} */ (comments.shift());
788
- (node.leadingComments ||= []).push(comment);
789
- }
790
-
791
- next();
792
-
793
- if (comments[0]) {
794
- if (node.type === 'BlockStatement' && node.body.length === 0) {
795
- if (comments[0].start < node.end && comments[0].end < node.end) {
796
- comment = /** @type {CommentWithLocation} */ (comments.shift());
797
- (node.innerComments ||= []).push(comment);
798
- return;
799
- }
800
- }
801
- const parent = /** @type {any} */ (path.at(-1));
802
-
803
- if (parent === undefined || node.end !== parent.end) {
804
- const slice = source.slice(node.end, comments[0].start);
805
- const is_last_in_body =
806
- ((parent?.type === 'BlockStatement' || parent?.type === 'Program') &&
807
- parent.body.indexOf(node) === parent.body.length - 1) ||
808
- (parent?.type === 'ArrayExpression' &&
809
- parent.elements.indexOf(node) === parent.elements.length - 1) ||
810
- (parent?.type === 'ObjectExpression' &&
811
- parent.properties.indexOf(node) === parent.properties.length - 1);
812
-
813
- if (is_last_in_body) {
814
- // Special case: There can be multiple trailing comments after the last node in a block,
815
- // and they can be separated by newlines
816
- let end = node.end;
817
-
818
- while (comments.length) {
819
- const comment = comments[0];
820
- if (parent && comment.start >= parent.end) break;
821
-
822
- (node.trailingComments ||= []).push(comment);
823
- comments.shift();
824
- end = comment.end;
825
- }
826
- } else if (node.end <= comments[0].start && /^[,) \t]*$/.test(slice)) {
827
- node.trailingComments = [/** @type {CommentWithLocation} */ (comments.shift())];
828
- }
829
- }
830
- }
831
- },
832
- });
833
-
834
- // Special case: Trailing comments after the root node (which can only happen for expression tags or for Program nodes).
835
- // Adding them ensures that we can later detect the end of the expression tag correctly.
836
- if (comments.length > 0 && (comments[0].start >= ast.end || ast.type === 'Program')) {
837
- (ast.trailingComments ||= []).push(...comments.splice(0));
838
- }
839
- },
840
- };
824
+ return {
825
+ onComment: (block, value, start, end, start_loc, end_loc) => {
826
+ if (block && /\n/.test(value)) {
827
+ let a = start;
828
+ while (a > 0 && source[a - 1] !== '\n') a -= 1;
829
+
830
+ let b = a;
831
+ while (/[ \t]/.test(source[b])) b += 1;
832
+
833
+ const indentation = source.slice(a, b);
834
+ value = value.replace(new RegExp(`^${indentation}`, 'gm'), '');
835
+ }
836
+
837
+ comments.push({
838
+ type: block ? 'Block' : 'Line',
839
+ value,
840
+ start,
841
+ end,
842
+ loc: {
843
+ start: /** @type {import('acorn').Position} */ (start_loc),
844
+ end: /** @type {import('acorn').Position} */ (end_loc),
845
+ },
846
+ });
847
+ },
848
+ add_comments: (ast) => {
849
+ if (comments.length === 0) return;
850
+
851
+ comments = comments
852
+ .filter((comment) => comment.start >= index)
853
+ .map(({ type, value, start, end }) => ({ type, value, start, end }));
854
+
855
+ walk(ast, null, {
856
+ _(node, { next, path }) {
857
+ let comment;
858
+
859
+ while (comments[0] && comments[0].start < node.start) {
860
+ comment = /** @type {CommentWithLocation} */ (comments.shift());
861
+ (node.leadingComments ||= []).push(comment);
862
+ }
863
+
864
+ next();
865
+
866
+ if (comments[0]) {
867
+ if (node.type === 'BlockStatement' && node.body.length === 0) {
868
+ if (comments[0].start < node.end && comments[0].end < node.end) {
869
+ comment = /** @type {CommentWithLocation} */ (comments.shift());
870
+ (node.innerComments ||= []).push(comment);
871
+ return;
872
+ }
873
+ }
874
+ const parent = /** @type {any} */ (path.at(-1));
875
+
876
+ if (parent === undefined || node.end !== parent.end) {
877
+ const slice = source.slice(node.end, comments[0].start);
878
+ const is_last_in_body =
879
+ ((parent?.type === 'BlockStatement' || parent?.type === 'Program') &&
880
+ parent.body.indexOf(node) === parent.body.length - 1) ||
881
+ (parent?.type === 'ArrayExpression' &&
882
+ parent.elements.indexOf(node) === parent.elements.length - 1) ||
883
+ (parent?.type === 'ObjectExpression' &&
884
+ parent.properties.indexOf(node) === parent.properties.length - 1);
885
+
886
+ if (is_last_in_body) {
887
+ // Special case: There can be multiple trailing comments after the last node in a block,
888
+ // and they can be separated by newlines
889
+ let end = node.end;
890
+
891
+ while (comments.length) {
892
+ const comment = comments[0];
893
+ if (parent && comment.start >= parent.end) break;
894
+
895
+ (node.trailingComments ||= []).push(comment);
896
+ comments.shift();
897
+ end = comment.end;
898
+ }
899
+ } else if (node.end <= comments[0].start && /^[,) \t]*$/.test(slice)) {
900
+ node.trailingComments = [/** @type {CommentWithLocation} */ (comments.shift())];
901
+ }
902
+ }
903
+ }
904
+ },
905
+ });
906
+
907
+ // Special case: Trailing comments after the root node (which can only happen for expression tags or for Program nodes).
908
+ // Adding them ensures that we can later detect the end of the expression tag correctly.
909
+ if (comments.length > 0 && (comments[0].start >= ast.end || ast.type === 'Program')) {
910
+ (ast.trailingComments ||= []).push(...comments.splice(0));
911
+ }
912
+ },
913
+ };
841
914
  }
842
915
 
843
916
  export function parse(source) {
844
- const comments = [];
845
- const { onComment, add_comments } = get_comment_handlers(source, comments);
846
- let ast;
847
-
848
- try {
849
- ast = parser.parse(source, {
850
- sourceType: 'module',
851
- ecmaVersion: 13,
852
- locations: true,
853
- onComment,
854
- });
855
- } catch (e) {
856
- throw e;
857
- }
858
-
859
- add_comments(ast);
860
-
861
- return ast;
917
+ const comments = [];
918
+ const { onComment, add_comments } = get_comment_handlers(source, comments);
919
+ let ast;
920
+
921
+ try {
922
+ ast = parser.parse(source, {
923
+ sourceType: 'module',
924
+ ecmaVersion: 13,
925
+ locations: true,
926
+ onComment,
927
+ });
928
+ } catch (e) {
929
+ throw e;
930
+ }
931
+
932
+ add_comments(ast);
933
+
934
+ return ast;
862
935
  }