bun_js_parser 0.1.0

A Rust-native programmable browser runtime built on Servo and SpiderMonkey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
#![warn(unused_must_use)]

use bun_collections::VecExt;
use bun_core::{self, err};

use crate::lexer as js_lexer;
use crate::p::P;
use crate::parser::{
    AwaitOrYield, DeferredErrors, FnOrArrowDataParse, ParseStatementOptions, PropertyOpts,
    SkipTypeParameterResult, TypeParameterFlag,
};
use bun_ast as js_ast;
use bun_ast::flags;
use bun_ast::lexer_tables::PropertyModifierKeyword;
use bun_ast::op::Level;
use bun_ast::scope::Kind as ScopeKind;
use bun_ast::ts::Metadata as TsMetadata;
use js_ast::{
    E, Expr, ExprNodeList,
    G::{self, PropertyKind},
    Stmt, symbol,
};
use js_lexer::T;

// Zig: `fn ParseProperty(comptime ts, comptime jsx, comptime scan_only) type { return struct { ... } }`
// — file-split mixin pattern. Round-C lowered `const JSX: JSXTransformType` → `J: JsxT`, so this is
// a direct `impl P` block.

impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_ONLY> {
    fn parse_method_expression(
        &mut self,
        kind: PropertyKind,
        opts: &mut PropertyOpts,
        is_computed: bool,
        key: &mut Expr,
        key_range: bun_ast::Range,
    ) -> Result<Option<G::Property>, bun_core::Error> {
        let p = self;
        if p.lexer.token == T::TOpenParen && kind != PropertyKind::Get && kind != PropertyKind::Set
        {
            // markSyntaxFeature object extensions
        }

        let loc = p.lexer.loc();
        let scope_index = p
            .push_scope_for_parse_pass(ScopeKind::FunctionArgs, loc)
            .expect("unreachable");
        let mut is_constructor = false;

        // Forbid the names "constructor" and "prototype" in some cases
        if opts.is_class && !is_computed {
            match &key.data {
                js_ast::ExprData::EString(str_) => {
                    if !opts.is_static && str_.eql_comptime(b"constructor") {
                        if kind == PropertyKind::Get {
                            p.log().add_range_error(
                                Some(p.source),
                                key_range,
                                b"Class constructor cannot be a getter",
                            );
                        } else if kind == PropertyKind::Set {
                            p.log().add_range_error(
                                Some(p.source),
                                key_range,
                                b"Class constructor cannot be a setter",
                            );
                        } else if opts.is_async {
                            p.log().add_range_error(
                                Some(p.source),
                                key_range,
                                b"Class constructor cannot be an async function",
                            );
                        } else if opts.is_generator {
                            p.log().add_range_error(
                                Some(p.source),
                                key_range,
                                b"Class constructor cannot be a generator function",
                            );
                        } else {
                            is_constructor = true;
                        }
                    } else if opts.is_static && str_.eql_comptime(b"prototype") {
                        p.log().add_range_error(
                            Some(p.source),
                            key_range,
                            b"Invalid static method name \"prototype\"",
                        );
                    }
                }
                _ => {}
            }
        }

        let func = p.parse_fn(
            None,
            FnOrArrowDataParse {
                async_range: opts.async_range,
                needs_async_loc: key.loc,
                has_async_range: !opts.async_range.is_empty(),
                allow_await: if opts.is_async {
                    AwaitOrYield::AllowExpr
                } else {
                    AwaitOrYield::AllowIdent
                },
                allow_yield: if opts.is_generator {
                    AwaitOrYield::AllowExpr
                } else {
                    AwaitOrYield::AllowIdent
                },
                allow_super_call: opts.class_has_extends && is_constructor,
                allow_super_property: true,
                allow_ts_decorators: opts.allow_ts_decorators,
                is_constructor,
                has_decorators: opts.ts_decorators.len() > 0
                    || (opts.has_class_decorators && is_constructor),

                // Only allow omitting the body if we're parsing TypeScript class
                allow_missing_body_for_type_script: Self::IS_TYPESCRIPT_ENABLED && opts.is_class,
                ..Default::default()
            },
        )?;

        opts.has_argument_decorators =
            opts.has_argument_decorators || p.fn_or_arrow_data_parse.has_argument_decorators;
        p.fn_or_arrow_data_parse.has_argument_decorators = false;

        // "class Foo { foo(): void; foo(): void {} }"
        if func.flags.contains(flags::Function::IsForwardDeclaration) {
            // Skip this property entirely
            p.pop_and_discard_scope(scope_index);
            return Ok(None);
        }

        p.pop_scope();
        // PORT NOTE: G::Fn is not Copy (FnBody/TS-metadata aren't), so mutate in place via the
        // E::Function payload after boxing rather than copying `func`.
        let mut func = func;
        func.flags.insert(flags::Function::IsUniqueFormalParameters);
        let args = func.args.slice();
        let value = p.new_expr(E::Function { func }, loc);

        // Enforce argument rules for accessors
        match kind {
            PropertyKind::Get => {
                if args.len() > 0 {
                    let r = js_lexer::range_of_identifier(p.source, args[0].binding.loc);
                    // TODO(port): Zig used p.keyNameForError(key) inline; borrowck reshape — pre-compute name.
                    let key_name = p.key_name_for_error(key);
                    p.log().add_range_error_fmt(
                        Some(p.source),
                        r,
                        format_args!(
                            "Getter {} must have zero arguments",
                            bstr::BStr::new(key_name)
                        ),
                    );
                }
            }
            PropertyKind::Set => {
                if args.len() != 1 {
                    let mut r = js_lexer::range_of_identifier(
                        p.source,
                        if args.len() > 0 {
                            args[0].binding.loc
                        } else {
                            loc
                        },
                    );
                    if args.len() > 1 {
                        r = js_lexer::range_of_identifier(p.source, args[1].binding.loc);
                    }
                    let key_name = p.key_name_for_error(key);
                    p.log().add_range_error_fmt(
                        Some(p.source),
                        r,
                        format_args!(
                            "Setter {} must have exactly 1 argument (there are {})",
                            bstr::BStr::new(key_name),
                            args.len()
                        ),
                    );
                }
            }
            _ => {}
        }

        // Special-case private identifiers
        match &mut key.data {
            js_ast::ExprData::EPrivateIdentifier(private) => {
                let declare: symbol::Kind = match kind {
                    PropertyKind::Get => {
                        if opts.is_static {
                            symbol::Kind::PrivateStaticGet
                        } else {
                            symbol::Kind::PrivateGet
                        }
                    }
                    PropertyKind::Set => {
                        if opts.is_static {
                            symbol::Kind::PrivateStaticSet
                        } else {
                            symbol::Kind::PrivateSet
                        }
                    }
                    _ => {
                        if opts.is_static {
                            symbol::Kind::PrivateStaticMethod
                        } else {
                            symbol::Kind::PrivateMethod
                        }
                    }
                };

                let name = p.load_name_from_ref(private.ref_);
                if name == b"#constructor" {
                    p.log().add_range_error(
                        Some(p.source),
                        key_range,
                        b"Invalid method name \"#constructor\"",
                    );
                }
                private.ref_ = p
                    .declare_symbol(declare, key.loc, name)
                    .expect("unreachable");
            }
            _ => {}
        }

        let mut prop_flags = flags::PropertySet::empty();
        if is_computed {
            prop_flags.insert(flags::Property::IsComputed);
        }
        prop_flags.insert(flags::Property::IsMethod);
        if opts.is_static {
            prop_flags.insert(flags::Property::IsStatic);
        }

        Ok(Some(G::Property {
            ts_decorators: ExprNodeList::from_slice(&opts.ts_decorators),
            kind,
            flags: prop_flags,
            key: Some(*key),
            value: Some(value),
            ts_metadata: TsMetadata::MFunction,
            ..Default::default()
        }))
    }

    pub fn parse_property(
        &mut self,
        kind_: PropertyKind,
        opts: &mut PropertyOpts,
        errors_: Option<&mut DeferredErrors>,
    ) -> Result<Option<G::Property>, bun_core::Error> {
        let p = self;
        if !p.stack_check.is_safe_to_recurse() {
            return Err(err!("StackOverflow"));
        }
        let mut kind = kind_;
        let mut errors = errors_;
        // This while loop exists to conserve stack space by reducing (but not completely eliminating) recursion.
        'restart: loop {
            // Every match arm below assigns `key` (or `continue 'restart` /
            // `return`) before any read; Zig's `var key: Expr = undefined` pre-
            // init is unnecessary here.
            let mut key: Expr;
            let key_range = p.lexer.range();
            let mut is_computed = false;

            match p.lexer.token {
                T::TNumericLiteral => {
                    key = p.new_expr(
                        E::Number {
                            value: p.lexer.number,
                        },
                        p.lexer.loc(),
                    );
                    // p.checkForLegacyOctalLiteral()
                    p.lexer.next()?;
                }
                T::TStringLiteral => {
                    key = p.parse_string_literal()?;
                }
                T::TBigIntegerLiteral => {
                    key = p.new_expr(
                        E::BigInt {
                            value: p.lexer.identifier.into(),
                        },
                        p.lexer.loc(),
                    );
                    // markSyntaxFeature
                    p.lexer.next()?;
                }
                T::TPrivateIdentifier => {
                    if !opts.is_class
                        || (opts.ts_decorators.len() > 0 && !p.options.features.standard_decorators)
                    {
                        p.lexer.expected(T::TIdentifier)?;
                    }

                    let ident = p.lexer.identifier;
                    let ref_ = p.store_name_in_ref(ident).expect("unreachable");
                    key = p.new_expr(E::PrivateIdentifier { ref_ }, p.lexer.loc());
                    p.lexer.next()?;
                }
                T::TOpenBracket => {
                    is_computed = true;
                    // p.markSyntaxFeature(compat.objectExtensions, p.lexer.range())
                    p.lexer.next()?;
                    let was_identifier = p.lexer.token == T::TIdentifier;
                    let expr = p.parse_expr(Level::Comma)?;

                    if Self::IS_TYPESCRIPT_ENABLED {
                        // Handle index signatures
                        if p.lexer.token == T::TColon && was_identifier && opts.is_class {
                            match expr.data {
                                js_ast::ExprData::EIdentifier(_) => {
                                    p.lexer.next()?;
                                    p.skip_type_script_type(Level::Lowest)?;
                                    p.lexer.expect(T::TCloseBracket)?;
                                    p.lexer.expect(T::TColon)?;
                                    p.skip_type_script_type(Level::Lowest)?;
                                    p.lexer.expect_or_insert_semicolon()?;

                                    // Skip this property entirely
                                    return Ok(None);
                                }
                                _ => {}
                            }
                        }
                    }

                    p.lexer.expect(T::TCloseBracket)?;
                    key = expr;
                }
                T::TAsterisk => {
                    if kind != PropertyKind::Normal || opts.is_generator {
                        p.lexer.unexpected()?;
                        return Err(err!("SyntaxError"));
                    }

                    p.lexer.next()?;
                    opts.is_generator = true;
                    kind = PropertyKind::Normal;
                    continue 'restart;
                }

                _ => {
                    let name = p.lexer.identifier;
                    let raw = p.lexer.raw();
                    let name_range = p.lexer.range();

                    if !p.lexer.is_identifier_or_keyword() {
                        p.lexer.expect(T::TIdentifier)?;
                    }

                    p.lexer.next()?;

                    // Support contextual keywords
                    if kind == PropertyKind::Normal && !opts.is_generator {
                        // Does the following token look like a key?
                        let could_be_modifier_keyword = p.lexer.is_identifier_or_keyword()
                            || matches!(
                                p.lexer.token,
                                T::TOpenBracket
                                    | T::TNumericLiteral
                                    | T::TStringLiteral
                                    | T::TAsterisk
                                    | T::TPrivateIdentifier
                            );

                        // If so, check for a modifier keyword
                        if could_be_modifier_keyword {
                            // TODO: micro-optimization, use a smaller list for non-typescript files.
                            if let Some(keyword) = PropertyModifierKeyword::find(name) {
                                match keyword {
                                    PropertyModifierKeyword::PGet => {
                                        if !opts.is_async
                                            && PropertyModifierKeyword::find(raw)
                                                == Some(PropertyModifierKeyword::PGet)
                                        {
                                            kind = PropertyKind::Get;
                                            errors = None;
                                            continue 'restart;
                                        }
                                    }

                                    PropertyModifierKeyword::PSet => {
                                        if !opts.is_async
                                            && PropertyModifierKeyword::find(raw)
                                                == Some(PropertyModifierKeyword::PSet)
                                        {
                                            // p.markSyntaxFeature(ObjectAccessors, name_range)
                                            kind = PropertyKind::Set;
                                            errors = None;
                                            continue 'restart;
                                        }
                                    }
                                    PropertyModifierKeyword::PAsync => {
                                        if !opts.is_async
                                            && PropertyModifierKeyword::find(raw)
                                                == Some(PropertyModifierKeyword::PAsync)
                                            && !p.lexer.has_newline_before
                                        {
                                            opts.is_async = true;
                                            opts.async_range = name_range;

                                            // p.markSyntaxFeature(ObjectAccessors, name_range)

                                            errors = None;
                                            continue 'restart;
                                        }
                                    }
                                    PropertyModifierKeyword::PStatic => {
                                        if !opts.is_static
                                            && !opts.is_async
                                            && opts.is_class
                                            && PropertyModifierKeyword::find(raw)
                                                == Some(PropertyModifierKeyword::PStatic)
                                        {
                                            opts.is_static = true;
                                            kind = PropertyKind::Normal;
                                            errors = None;
                                            continue 'restart;
                                        }
                                    }
                                    PropertyModifierKeyword::PDeclare => {
                                        // skip declare keyword entirely
                                        // https://github.com/oven-sh/bun/issues/1907
                                        if opts.is_class
                                            && Self::IS_TYPESCRIPT_ENABLED
                                            && raw == b"declare"
                                        {
                                            let scope_index = p.scopes_in_order.len();
                                            if let Some(_prop) =
                                                p.parse_property(kind, opts, None)?
                                            {
                                                let mut prop = _prop;
                                                if prop.kind == PropertyKind::Normal
                                                    && prop.value.is_none()
                                                    && opts.ts_decorators.len() > 0
                                                {
                                                    prop.kind = PropertyKind::Declare;
                                                    return Ok(Some(prop));
                                                }
                                            }

                                            p.discard_scopes_up_to(scope_index);
                                            return Ok(None);
                                        }
                                    }
                                    PropertyModifierKeyword::PAbstract => {
                                        if opts.is_class
                                            && Self::IS_TYPESCRIPT_ENABLED
                                            && !opts.is_ts_abstract
                                            && raw == b"abstract"
                                        {
                                            opts.is_ts_abstract = true;
                                            let scope_index = p.scopes_in_order.len();
                                            if let Some(prop) =
                                                p.parse_property(kind, opts, None)?
                                            {
                                                if prop.kind == PropertyKind::Normal
                                                    && prop.value.is_none()
                                                    && opts.ts_decorators.len() > 0
                                                {
                                                    let mut prop_ = prop;
                                                    prop_.kind = PropertyKind::Abstract;
                                                    return Ok(Some(prop_));
                                                }
                                            }
                                            p.discard_scopes_up_to(scope_index);
                                            return Ok(None);
                                        }
                                    }
                                    PropertyModifierKeyword::PAccessor => {
                                        // "accessor" keyword for auto-accessor fields (TC39 standard decorators)
                                        if opts.is_class
                                            && p.options.features.standard_decorators
                                            && PropertyModifierKeyword::find(raw)
                                                == Some(PropertyModifierKeyword::PAccessor)
                                        {
                                            kind = PropertyKind::AutoAccessor;
                                            errors = None;
                                            continue 'restart;
                                        }
                                    }
                                    PropertyModifierKeyword::PPrivate
                                    | PropertyModifierKeyword::PProtected
                                    | PropertyModifierKeyword::PPublic
                                    | PropertyModifierKeyword::PReadonly
                                    | PropertyModifierKeyword::POverride => {
                                        // Skip over TypeScript keywords
                                        if opts.is_class
                                            && Self::IS_TYPESCRIPT_ENABLED
                                            && PropertyModifierKeyword::find(raw) == Some(keyword)
                                        {
                                            errors = None;
                                            continue 'restart;
                                        }
                                    }
                                }
                            }
                        } else if opts.is_class
                            && p.lexer.token == T::TOpenBrace
                            && name == b"static"
                        {
                            let loc = p.lexer.loc();
                            p.lexer.next()?;

                            let old_fn_or_arrow_data_parse = p.fn_or_arrow_data_parse.clone();
                            p.fn_or_arrow_data_parse = FnOrArrowDataParse {
                                is_return_disallowed: true,
                                allow_super_property: true,
                                allow_await: AwaitOrYield::ForbidAll,
                                ..Default::default()
                            };

                            let _ = p.push_scope_for_parse_pass(ScopeKind::ClassStaticInit, loc)?;
                            let mut _parse_opts = ParseStatementOptions::default();
                            let stmts = p.parse_stmts_up_to(T::TCloseBrace, &mut _parse_opts)?;

                            p.pop_scope();

                            p.fn_or_arrow_data_parse = old_fn_or_arrow_data_parse;
                            p.lexer.expect(T::TCloseBrace)?;

                            // PERF(port): was arena arena.create — bump.alloc returns &'a mut T;
                            // Vec::from_slice copies the bump-backed StmtList into a heap-backed list.
                            // TODO(perf): route ClassStaticBlock.stmts through arena slice directly.
                            let stmt_list = bun_alloc::AstVec::<Stmt>::from_slice(stmts.as_slice());
                            let block = p.arena.alloc(G::ClassStaticBlock {
                                stmts: stmt_list,
                                loc,
                            });

                            return Ok(Some(G::Property {
                                kind: PropertyKind::ClassStaticBlock,
                                class_static_block: Some(js_ast::StoreRef::from_bump(block)),
                                ..Default::default()
                            }));
                        }
                    }

                    // Handle invalid identifiers in property names
                    // https://github.com/oven-sh/bun/issues/12039
                    if p.lexer.token == T::TSyntaxError {
                        p.log().add_range_error_fmt(
                            Some(p.source),
                            name_range,
                            format_args!("Unexpected {}", bun_core::fmt::quote(name)),
                        );
                        return Err(err!("SyntaxError"));
                    }

                    key = p.new_expr(E::EString::init(name), name_range.loc);

                    // Parse a shorthand property
                    let is_shorthand_property = !opts.is_class
                        && kind == PropertyKind::Normal
                        && p.lexer.token != T::TColon
                        && p.lexer.token != T::TOpenParen
                        && p.lexer.token != T::TLessThan
                        && !opts.is_generator
                        && !opts.is_async
                        && js_lexer::keyword(name).is_none();

                    if is_shorthand_property {
                        if (p.fn_or_arrow_data_parse.allow_await != AwaitOrYield::AllowIdent
                            && name == b"await")
                            || (p.fn_or_arrow_data_parse.allow_yield != AwaitOrYield::AllowIdent
                                && name == b"yield")
                        {
                            if name == b"await" {
                                p.log().add_range_error(
                                    Some(p.source),
                                    name_range,
                                    b"Cannot use \"await\" here",
                                );
                            } else {
                                p.log().add_range_error(
                                    Some(p.source),
                                    name_range,
                                    b"Cannot use \"yield\" here",
                                );
                            }
                        }

                        let ref_ = p.store_name_in_ref(name).expect("unreachable");
                        let value = p.new_expr(E::Identifier::init(ref_), key.loc);

                        // Destructuring patterns have an optional default value
                        let mut initializer: Option<Expr> = None;
                        if let Some(errors) = errors.as_mut() {
                            if p.lexer.token == T::TEquals {
                                errors.invalid_expr_default_value = Some(p.lexer.range());
                                p.lexer.next()?;
                                initializer = Some(p.parse_expr(Level::Comma)?);
                            }
                        }

                        return Ok(Some(G::Property {
                            kind,
                            key: Some(key),
                            value: Some(value),
                            initializer,
                            flags: flags::Property::WasShorthand.into(),
                            ..Default::default()
                        }));
                    }
                }
            }

            let mut has_type_parameters = false;
            let mut has_definite_assignment_assertion_operator = false;

            if Self::IS_TYPESCRIPT_ENABLED {
                if opts.is_class {
                    if p.lexer.token == T::TQuestion {
                        // "class X { foo?: number }"
                        // "class X { foo!: number }"
                        p.lexer.next()?;
                    } else if p.lexer.token == T::TExclamation
                        && !p.lexer.has_newline_before
                        && (kind == PropertyKind::Normal || kind == PropertyKind::AutoAccessor)
                        && !opts.is_async
                        && !opts.is_generator
                    {
                        // "class X { foo!: number }"
                        p.lexer.next()?;
                        has_definite_assignment_assertion_operator = true;
                    }
                }

                // "class X { foo?<T>(): T }"
                // "const x = { foo<T>(): T {} }"
                if !has_definite_assignment_assertion_operator {
                    has_type_parameters = p.skip_type_script_type_parameters(
                        TypeParameterFlag::ALLOW_CONST_MODIFIER,
                    )? != SkipTypeParameterResult::DidNotSkipAnything;
                }
            }

            // Parse a class field with an optional initial value
            if opts.is_class
                && (kind == PropertyKind::Normal || kind == PropertyKind::AutoAccessor)
                && !opts.is_async
                && !opts.is_generator
                && p.lexer.token != T::TOpenParen
                && !has_type_parameters
                && (p.lexer.token != T::TOpenParen || has_definite_assignment_assertion_operator)
            {
                let mut initializer: Option<Expr> = None;
                let mut ts_metadata = TsMetadata::default();

                // Forbid the names "constructor" and "prototype" in some cases
                if !is_computed {
                    match &key.data {
                        js_ast::ExprData::EString(str_) => {
                            if str_.eql_comptime(b"constructor")
                                || (opts.is_static && str_.eql_comptime(b"prototype"))
                            {
                                // TODO: fmt error message to include string value.
                                p.log().add_range_error(
                                    Some(p.source),
                                    key_range,
                                    b"Invalid field name",
                                );
                            }
                        }
                        _ => {}
                    }
                }

                if Self::IS_TYPESCRIPT_ENABLED {
                    // Skip over types
                    if p.lexer.token == T::TColon {
                        p.lexer.next()?;
                        if p.options.features.emit_decorator_metadata
                            && opts.is_class
                            && opts.ts_decorators.len() > 0
                        {
                            ts_metadata = p.skip_type_script_type_with_metadata(Level::Lowest)?;
                        } else {
                            p.skip_type_script_type(Level::Lowest)?;
                        }
                    }
                }

                if p.lexer.token == T::TEquals {
                    if Self::IS_TYPESCRIPT_ENABLED {
                        if !opts.declare_range.is_empty() {
                            p.log().add_range_error(
                                Some(p.source),
                                p.lexer.range(),
                                b"Class fields that use \"declare\" cannot be initialized",
                            );
                        }
                    }

                    p.lexer.next()?;

                    // "this" and "super" property access is allowed in field initializers
                    let old_is_this_disallowed = p.fn_or_arrow_data_parse.is_this_disallowed;
                    let old_allow_super_property = p.fn_or_arrow_data_parse.allow_super_property;
                    p.fn_or_arrow_data_parse.is_this_disallowed = false;
                    p.fn_or_arrow_data_parse.allow_super_property = true;

                    initializer = Some(p.parse_expr(Level::Comma)?);

                    p.fn_or_arrow_data_parse.is_this_disallowed = old_is_this_disallowed;
                    p.fn_or_arrow_data_parse.allow_super_property = old_allow_super_property;
                }

                // Special-case private identifiers
                match &mut key.data {
                    js_ast::ExprData::EPrivateIdentifier(private) => {
                        let name = p.load_name_from_ref(private.ref_);
                        if name == b"#constructor" {
                            p.log().add_range_error(
                                Some(p.source),
                                key_range,
                                b"Invalid field name \"#constructor\"",
                            );
                        }

                        let declare: symbol::Kind = if opts.is_static {
                            symbol::Kind::PrivateStaticField
                        } else {
                            symbol::Kind::PrivateField
                        };

                        private.ref_ = p
                            .declare_symbol(declare, key.loc, name)
                            .expect("unreachable");
                    }
                    _ => {}
                }

                p.lexer.expect_or_insert_semicolon()?;

                let mut prop_flags = flags::PropertySet::empty();
                if is_computed {
                    prop_flags.insert(flags::Property::IsComputed);
                }
                if opts.is_static {
                    prop_flags.insert(flags::Property::IsStatic);
                }

                return Ok(Some(G::Property {
                    ts_decorators: ExprNodeList::from_slice(&opts.ts_decorators),
                    kind,
                    flags: prop_flags,
                    key: Some(key),
                    initializer,
                    ts_metadata,
                    ..Default::default()
                }));
            }

            // Auto-accessor fields cannot be methods
            if kind == PropertyKind::AutoAccessor && p.lexer.token == T::TOpenParen {
                p.log().add_range_error(
                    Some(p.source),
                    key_range,
                    b"auto-accessor properties cannot have a method body",
                );
                return Err(err!("SyntaxError"));
            }

            // Parse a method expression
            if p.lexer.token == T::TOpenParen
                || kind != PropertyKind::Normal
                || opts.is_class
                || opts.is_async
                || opts.is_generator
            {
                return Self::parse_method_expression(
                    p,
                    kind,
                    opts,
                    is_computed,
                    &mut key,
                    key_range,
                );
            }

            // Parse an object key/value pair
            p.lexer.expect(T::TColon)?;
            let mut prop_flags = flags::PropertySet::empty();
            if is_computed {
                prop_flags.insert(flags::Property::IsComputed);
            }
            let mut property = G::Property {
                kind,
                flags: prop_flags,
                key: Some(key),
                value: Some(Expr {
                    data: js_ast::ExprData::EMissing(E::Missing {}),
                    loc: bun_ast::Loc::default(),
                }),
                ..Default::default()
            };

            // PORT NOTE: reshaped for borrowck — `errors` is Option<&mut _>, reborrow via as_deref_mut
            p.parse_expr_or_bindings(
                Level::Comma,
                errors.as_deref_mut(),
                property.value.as_mut().unwrap(),
            )?;
            return Ok(Some(property));
        }
    }
}

// ported from: src/js_parser/ast/parseProperty.zig