oxc_semantic 0.129.0

A collection of JavaScript tools written in Rust.
Documentation
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
//! Declare symbol for `BindingIdentifier`s

use oxc_allocator::{GetAddress, UnstableAddress};
use oxc_ast::{AstKind, ast::*};
use oxc_ecmascript::{BoundNames, IsSimpleParameterList};
use oxc_str::Ident;
use oxc_syntax::{node::NodeId, scope::ScopeFlags, symbol::SymbolFlags};

use crate::{SemanticBuilder, checker::is_function_decl_part_of_if_statement};

pub trait Binder<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>);
}

impl<'a> Binder<'a> for VariableDeclarator<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        let is_declare = matches!(builder
            .nodes
            .parent_kind(builder.current_node_id), AstKind::VariableDeclaration(decl) if decl.declare);

        let (mut includes, excludes) = match self.kind {
            VariableDeclarationKind::Const
            | VariableDeclarationKind::Using
            | VariableDeclarationKind::AwaitUsing => (
                SymbolFlags::BlockScopedVariable | SymbolFlags::ConstVariable,
                SymbolFlags::BlockScopedVariableExcludes,
            ),
            VariableDeclarationKind::Let => {
                (SymbolFlags::BlockScopedVariable, SymbolFlags::BlockScopedVariableExcludes)
            }
            VariableDeclarationKind::Var => {
                (SymbolFlags::FunctionScopedVariable, SymbolFlags::FunctionScopedVariableExcludes)
            }
        };

        if is_declare {
            includes |= SymbolFlags::Ambient;
        }

        if self.kind.is_lexical() {
            self.id.bound_names(&mut |ident| {
                let symbol_id = builder.declare_symbol(ident.span, ident.name, includes, excludes);
                ident.symbol_id.set(Some(symbol_id));
            });
        } else {
            // ------------------ var hosting ------------------
            let mut target_scope_id = builder.current_scope_id;
            let mut var_scope_ids = vec![];

            // Collect all scopes where variable hoisting can occur
            for scope_id in builder.scoping.scope_ancestors(target_scope_id) {
                let flags = builder.scoping.scope_flags(scope_id);
                if flags.is_var() {
                    target_scope_id = scope_id;
                    break;
                }
                var_scope_ids.push(scope_id);
            }

            self.id.bound_names(&mut |ident| {
                let span = ident.span;
                let name = ident.name;
                let mut declared_symbol_id = None;

                for &scope_id in &var_scope_ids {
                    if let Some(symbol_id) =
                        builder.check_redeclaration(scope_id, span, name, excludes)
                    {
                        builder.add_redeclare_variable(symbol_id, includes, span);
                        declared_symbol_id = Some(symbol_id);

                        // Hoist current symbol to target scope when it is not already declared
                        // in the target scope.
                        if !builder.scoping.scope_has_binding(target_scope_id, name) {
                            // remove current scope binding and add to target scope
                            // avoid same symbols appear in multi-scopes
                            builder.scoping.remove_binding(scope_id, name);
                            builder.scoping.add_binding(target_scope_id, name, symbol_id);
                            builder.scoping.set_symbol_scope_id(symbol_id, target_scope_id);
                        }
                        break;
                    }
                }

                // If a variable is already declared in the hoisted scopes,
                // we don't need to create another symbol with the same name
                // to make sure they point to the same symbol.
                let symbol_id = declared_symbol_id.unwrap_or_else(|| {
                    builder.declare_symbol_on_scope(span, name, target_scope_id, includes, excludes)
                });
                ident.symbol_id.set(Some(symbol_id));

                // Finally, add the variable to all hoisted scopes
                // to support redeclaration checks when declaring variables with the same name later.
                for &scope_id in &var_scope_ids {
                    builder.hoisting_variables.entry(scope_id).or_default().insert(name, symbol_id);
                }
            });
        }

        // Save `@__NO_SIDE_EFFECTS__` for function initializers.
        if let BindingPattern::BindingIdentifier(id) = &self.id
            && let Some(symbol_id) = id.symbol_id.get()
            && let Some(init) = &self.init
            && match init {
                Expression::FunctionExpression(func) => func.pure,
                Expression::ArrowFunctionExpression(func) => func.pure,
                _ => false,
            }
        {
            builder.scoping.no_side_effects.insert(symbol_id);
        }
    }
}

impl<'a> Binder<'a> for Class<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        let includes = if self.declare {
            SymbolFlags::Class | SymbolFlags::Ambient
        } else {
            SymbolFlags::Class
        };
        let Some(ident) = &self.id else { return };
        let symbol_id =
            builder.declare_symbol(ident.span, ident.name, includes, SymbolFlags::ClassExcludes);
        ident.symbol_id.set(Some(symbol_id));
    }
}

impl<'a> Binder<'a> for Function<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        let is_declaration = self.is_declaration();

        if let Some(ident) = &self.id {
            let includes = if self.declare {
                SymbolFlags::Function | SymbolFlags::Ambient
            } else {
                SymbolFlags::Function
            };

            let excludes = if builder.source_type.is_typescript() {
                SymbolFlags::FunctionExcludes
            } else if is_declaration && is_function_decl_part_of_if_statement(self, builder) {
                SymbolFlags::empty()
            } else {
                // `var x; function x() {}` is valid in non-strict mode, but `TypeScript`
                // doesn't care about non-strict mode, so we need to exclude this,
                // and further check in checker.
                SymbolFlags::FunctionExcludes - SymbolFlags::FunctionScopedVariable
            };

            let symbol_id = builder.declare_symbol(ident.span, ident.name, includes, excludes);
            ident.symbol_id.set(Some(symbol_id));

            // Annex B.3.2.1: In sloppy mode, plain function declarations inside block
            // scopes also create an implicit var-like binding in the enclosing function
            // scope. Hoist to the var scope — same pattern as var hoisting (line 46).
            let scope_flags = builder.current_scope_flags();
            if is_declaration // function expressions are bound in their own (var) scope
                && !self.r#async // Annex B only applies to plain functions
                && !self.generator // not generators or async generators
                && !builder.source_type.is_typescript() // Annex B is JavaScript-only
                && !scope_flags.is_var() // already in a var scope, no hoisting needed
                && !scope_flags.is_strict_mode()
            // no Annex B in strict mode / modules
            {
                let block_scope_id = builder.current_scope_id;
                let var_scope_id = builder
                    .scoping
                    .scope_ancestors(block_scope_id)
                    .skip(1)
                    .find(|&id| builder.scoping.scope_flags(id).is_var());
                if let Some(var_scope_id) = var_scope_id
                    && !builder.scoping.scope_has_binding(var_scope_id, ident.name)
                {
                    builder.scoping.move_binding(block_scope_id, var_scope_id, ident.name);
                    builder.scoping.set_symbol_scope_id(symbol_id, var_scope_id);
                    builder
                        .hoisting_variables
                        .entry(block_scope_id)
                        .or_default()
                        .insert(ident.name, symbol_id);
                }
            }

            // Save `@__NO_SIDE_EFFECTS__`
            if self.pure {
                builder.scoping.no_side_effects.insert(symbol_id);
            }
        }

        // Bind scope flags: GetAccessor | SetAccessor
        if !is_declaration
            && let AstKind::ObjectProperty(prop) =
                builder.nodes.parent_kind(builder.current_node_id)
        {
            // Do not bind scope flags when function is inside of the object property key:
            //
            // { set [function() {}](val) {} }
            //        ^^^^^^^^^^^^^
            if prop.key.address() == self.unstable_address() {
                return;
            }
            let flags = builder.scoping.scope_flags_mut(builder.current_scope_id);
            match prop.kind {
                PropertyKind::Get => *flags |= ScopeFlags::GetAccessor,
                PropertyKind::Set => *flags |= ScopeFlags::SetAccessor,
                PropertyKind::Init => {}
            }
        }
    }
}

impl<'a> Binder<'a> for BindingRestElement<'a> {
    // Binds the FormalParameters's rest of a function or method.
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        let parent_kind = builder.nodes.parent_kind(builder.current_node_id);
        let AstKind::FormalParameters(_) = parent_kind else {
            return;
        };

        let includes = SymbolFlags::FunctionScopedVariable;
        let excludes =
            SymbolFlags::FunctionScopedVariable | SymbolFlags::FunctionScopedVariableExcludes;
        self.bound_names(&mut |ident| {
            let symbol_id = builder.declare_symbol(ident.span, ident.name, includes, excludes);
            ident.symbol_id.set(Some(symbol_id));
        });
    }
}

impl<'a> Binder<'a> for FormalParameter<'a> {
    // Binds the FormalParameter of a function or method.
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        let parent_kind = builder.nodes.parent_kind(builder.current_node_id);
        let AstKind::FormalParameters(parameters) = parent_kind else { unreachable!() };

        let includes = SymbolFlags::FunctionScopedVariable;

        let is_not_allowed_duplicate_parameters = matches!(
                parameters.kind,
                // ArrowFormalParameters: UniqueFormalParameters
                FormalParameterKind::ArrowFormalParameters |
                // UniqueFormalParameters : FormalParameters
                // * It is a Syntax Error if BoundNames of FormalParameters contains any duplicate elements.
                FormalParameterKind::UniqueFormalParameters
            ) ||
            // Multiple occurrences of the same BindingIdentifier in a FormalParameterList is only allowed for functions which have simple parameter lists and which are not defined in strict mode code.
            builder.strict_mode() ||
            // FormalParameters : FormalParameterList
            // * It is a Syntax Error if IsSimpleParameterList of FormalParameterList is false and BoundNames of FormalParameterList contains any duplicate elements.
            !parameters.is_simple_parameter_list();

        let excludes = if is_not_allowed_duplicate_parameters {
            SymbolFlags::FunctionScopedVariable | SymbolFlags::FunctionScopedVariableExcludes
        } else {
            SymbolFlags::FunctionScopedVariableExcludes
        };

        self.bound_names(&mut |ident| {
            let symbol_id = builder.declare_symbol(ident.span, ident.name, includes, excludes);
            ident.symbol_id.set(Some(symbol_id));
        });
    }
}

impl<'a> Binder<'a> for FormalParameterRest<'a> {
    // Binds the FormalParameter of a function or method.
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        let parent_kind = builder.nodes.parent_kind(builder.current_node_id);
        let AstKind::FormalParameters(parameters) = parent_kind else { unreachable!() };

        let includes = SymbolFlags::FunctionScopedVariable;

        let is_not_allowed_duplicate_parameters = matches!(
                parameters.kind,
                // ArrowFormalParameters: UniqueFormalParameters
                FormalParameterKind::ArrowFormalParameters |
                // UniqueFormalParameters : FormalParameters
                // * It is a Syntax Error if BoundNames of FormalParameters contains any duplicate elements.
                FormalParameterKind::UniqueFormalParameters
            ) ||
            // Multiple occurrences of the same BindingIdentifier in a FormalParameterList is only allowed for functions which have simple parameter lists and which are not defined in strict mode code.
            builder.strict_mode() ||
            // FormalParameters : FormalParameterList
            // * It is a Syntax Error if IsSimpleParameterList of FormalParameterList is false and BoundNames of FormalParameterList contains any duplicate elements.
            !parameters.is_simple_parameter_list();

        let excludes = if is_not_allowed_duplicate_parameters {
            SymbolFlags::FunctionScopedVariable | SymbolFlags::FunctionScopedVariableExcludes
        } else {
            SymbolFlags::FunctionScopedVariableExcludes
        };

        self.rest.argument.bound_names(&mut |ident| {
            let symbol_id = builder.declare_symbol(ident.span, ident.name, includes, excludes);
            ident.symbol_id.set(Some(symbol_id));
        });
    }
}

impl<'a> Binder<'a> for CatchParameter<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        let current_scope_id = builder.current_scope_id;
        // https://tc39.es/ecma262/#sec-variablestatements-in-catch-blocks
        // It is a Syntax Error if any element of the BoundNames of CatchParameter also occurs in the VarDeclaredNames of Block
        // unless CatchParameter is CatchParameter : BindingIdentifier
        if let BindingPattern::BindingIdentifier(ident) = &self.pattern {
            let includes = SymbolFlags::FunctionScopedVariable | SymbolFlags::CatchVariable;
            let symbol_id =
                builder.declare_shadow_symbol(ident.name, ident.span, current_scope_id, includes);
            ident.symbol_id.set(Some(symbol_id));
        } else {
            self.pattern.bound_names(&mut |ident| {
                let symbol_id = builder.declare_symbol(
                    ident.span,
                    ident.name,
                    SymbolFlags::BlockScopedVariable | SymbolFlags::CatchVariable,
                    SymbolFlags::BlockScopedVariableExcludes,
                );
                ident.symbol_id.set(Some(symbol_id));
            });
        }
    }
}

fn declare_symbol_for_import_specifier<'a>(
    ident: &BindingIdentifier<'a>,
    is_type: bool,
    builder: &mut SemanticBuilder<'a>,
) {
    let includes = if is_type
        || matches!(builder.nodes.parent_kind(builder.current_node_id), AstKind::ImportDeclaration(decl) if decl.import_kind.is_type(),
        ) {
        SymbolFlags::TypeImport
    } else {
        SymbolFlags::Import
    };

    let symbol_id = builder.declare_symbol(
        ident.span,
        ident.name,
        includes,
        SymbolFlags::ImportBindingExcludes,
    );
    ident.symbol_id.set(Some(symbol_id));
}

impl<'a> Binder<'a> for ImportSpecifier<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        declare_symbol_for_import_specifier(&self.local, self.import_kind.is_type(), builder);
    }
}

impl<'a> Binder<'a> for ImportDefaultSpecifier<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        declare_symbol_for_import_specifier(&self.local, false, builder);
    }
}

impl<'a> Binder<'a> for ImportNamespaceSpecifier<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        declare_symbol_for_import_specifier(&self.local, false, builder);
    }
}

impl<'a> Binder<'a> for TSImportEqualsDeclaration<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        declare_symbol_for_import_specifier(&self.id, false, builder);
    }
}

impl<'a> Binder<'a> for TSTypeAliasDeclaration<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        let includes = if self.declare {
            SymbolFlags::TypeAlias | SymbolFlags::Ambient
        } else {
            SymbolFlags::TypeAlias
        };
        let symbol_id = builder.declare_symbol(
            self.id.span,
            self.id.name,
            includes,
            SymbolFlags::TypeAliasExcludes,
        );
        self.id.symbol_id.set(Some(symbol_id));
    }
}

impl<'a> Binder<'a> for TSInterfaceDeclaration<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        let includes = if self.declare {
            SymbolFlags::Interface | SymbolFlags::Ambient
        } else {
            SymbolFlags::Interface
        };
        let symbol_id = builder.declare_symbol(
            self.id.span,
            self.id.name,
            includes,
            SymbolFlags::InterfaceExcludes,
        );
        self.id.symbol_id.set(Some(symbol_id));
    }
}

impl<'a> Binder<'a> for TSEnumDeclaration<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        let is_const = self.r#const;
        let includes = if self.declare { SymbolFlags::Ambient } else { SymbolFlags::empty() };
        let (includes, excludes) = if is_const {
            (SymbolFlags::ConstEnum | includes, SymbolFlags::ConstEnumExcludes)
        } else {
            (SymbolFlags::RegularEnum | includes, SymbolFlags::RegularEnumExcludes)
        };
        let symbol_id = builder.declare_symbol(self.id.span, self.id.name, includes, excludes);
        self.id.symbol_id.set(Some(symbol_id));
    }
}

impl<'a> Binder<'a> for TSEnumMember<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        // Extract Ident directly from Identifier variant to preserve precomputed hash.
        let name = match &self.id {
            TSEnumMemberName::Identifier(ident) => ident.name,
            _ => Ident::from(self.id.static_name()),
        };
        builder.declare_symbol(
            self.span,
            name,
            SymbolFlags::EnumMember,
            SymbolFlags::EnumMemberExcludes,
        );
    }
}

impl<'a> Binder<'a> for TSModuleDeclaration<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        let TSModuleDeclarationName::Identifier(id) = &self.id else { return };
        let instantiated =
            get_module_instance_state(builder, self, builder.current_node_id).is_instantiated();
        let (mut includes, excludes) = if instantiated {
            (SymbolFlags::ValueModule, SymbolFlags::ValueModuleExcludes)
        } else {
            (SymbolFlags::NamespaceModule, SymbolFlags::NamespaceModuleExcludes)
        };

        if self.declare {
            includes |= SymbolFlags::Ambient;
        }
        let symbol_id = builder.declare_symbol(id.span, id.name, includes, excludes);

        id.set_symbol_id(symbol_id);
    }
}

#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub enum ModuleInstanceState {
    NonInstantiated,
    Instantiated,
    ConstEnumOnly,
}

impl ModuleInstanceState {
    fn is_instantiated(self) -> bool {
        self != ModuleInstanceState::NonInstantiated
    }
}

/// Determines if a module is instantiated or not.
///
/// Based on `https://github.com/microsoft/TypeScript/blob/15392346d05045742e653eab5c87538ff2a3c863/src/compiler/binder.ts#L342-L474`
fn get_module_instance_state<'a>(
    builder: &mut SemanticBuilder<'a>,
    decl: &TSModuleDeclaration<'a>,
    current_node_id: NodeId,
) -> ModuleInstanceState {
    get_module_instance_state_impl(builder, decl, current_node_id, &mut Vec::new())
}

fn get_module_instance_state_impl<'a, 'b>(
    builder: &mut SemanticBuilder<'a>,
    decl: &'b TSModuleDeclaration<'a>,
    current_node_id: NodeId,
    module_declaration_stmts: &mut Vec<&'b Statement<'a>>,
) -> ModuleInstanceState {
    // `SemanticBuilder` takes an immutable reference to AST, so `unstable_address` produces stable `Address`es
    let address = decl.unstable_address();

    if let Some(state) = builder.module_instance_state_cache.get(&address) {
        return *state;
    }

    let Some(body) = &decl.body else {
        // For modules without a block, we consider them instantiated
        return ModuleInstanceState::Instantiated;
    };

    // A module is uninstantiated if it contains only specific declarations
    let state = match body {
        TSModuleDeclarationBody::TSModuleBlock(block) => {
            module_declaration_stmts.extend(block.body.iter());

            let mut child_state = ModuleInstanceState::NonInstantiated;
            for stmt in &block.body {
                child_state = get_module_instance_state_for_statement(
                    builder,
                    stmt,
                    current_node_id,
                    module_declaration_stmts,
                );
                if child_state.is_instantiated() {
                    break;
                }
            }
            child_state
        }
        TSModuleDeclarationBody::TSModuleDeclaration(module) => {
            get_module_instance_state(builder, module, current_node_id)
        }
    };

    builder.module_instance_state_cache.insert(address, state);
    state
}

fn get_module_instance_state_for_statement<'a, 'b>(
    builder: &mut SemanticBuilder<'a>,
    stmt: &'b Statement<'a>,
    current_node_id: NodeId,
    module_declaration_stmts: &mut Vec<&'b Statement<'a>>,
) -> ModuleInstanceState {
    let address = stmt.address();
    if let Some(state) = builder.module_instance_state_cache.get(&address) {
        return *state;
    }

    let state = match stmt {
            // 1. interface declarations, type alias declarations
            Statement::TSInterfaceDeclaration(_)
            | Statement::TSTypeAliasDeclaration(_)
            // 3. non-exported import declarations
            | Statement::TSImportEqualsDeclaration(_) => {
                ModuleInstanceState::NonInstantiated
            }
            // 2. const enum declarations
            Statement::TSEnumDeclaration(enum_decl) => {
                if enum_decl.r#const {
                    ModuleInstanceState::ConstEnumOnly
                } else {
                    ModuleInstanceState::Instantiated
                }
            }
            Statement::ExportDefaultDeclaration(export_decl)  => {
                if matches!(export_decl.declaration, ExportDefaultDeclarationKind::TSInterfaceDeclaration(_)) {
                    ModuleInstanceState::NonInstantiated
                } else {
                    ModuleInstanceState::Instantiated
                }
            }
            Statement::ExportNamedDeclaration(export_decl) if export_decl.declaration.is_some() => {
                match export_decl.declaration.as_ref().unwrap() {
                    Declaration::TSModuleDeclaration(module_decl) => {
                        get_module_instance_state_impl(builder, module_decl, current_node_id, module_declaration_stmts)
                    }
                    decl => if decl.is_type() {
                        ModuleInstanceState::NonInstantiated
                    } else {
                        ModuleInstanceState::Instantiated
                    }
                }
            }
            // 4. Export alias declarations pointing at uninstantiated modules
            Statement::ExportNamedDeclaration(export_decl) => {
                if export_decl.source.is_none() {
                    let mut export_state = ModuleInstanceState::NonInstantiated;
                    for specifier in &export_decl.specifiers {
                        export_state = get_module_instance_state_for_alias_target(builder, specifier, current_node_id, module_declaration_stmts.as_slice());
                        if export_state.is_instantiated() {
                            break;
                        }
                    }
                    export_state
                } else {
                    ModuleInstanceState::Instantiated
                }
            }
            // 5. other module declarations
            Statement::TSModuleDeclaration(module_decl) => {
                get_module_instance_state_impl(builder, module_decl, current_node_id, module_declaration_stmts)
            }
            // Any other type of statement means the module is instantiated
            _ => ModuleInstanceState::Instantiated,
        };

    builder.module_instance_state_cache.insert(address, state);
    state
}

// `module_declaration_stmts` is stored statements that are collected from the all ModuleBlocks.
// The reason we need to collect and pass in this method is that we need to check export specifiers
// whether they refer to a declaration that declared in the module block or not. And we can't use
// `self.nodes.node(node_id)` to get the nested module block's statements since the child ModuleBlock
// AstNode hasn't created yet.
fn get_module_instance_state_for_alias_target<'a>(
    builder: &mut SemanticBuilder<'a>,
    specifier: &ExportSpecifier<'a>,
    mut current_node_id: NodeId,
    module_declaration_stmts: &[&Statement<'a>],
) -> ModuleInstanceState {
    let ModuleExportName::IdentifierReference(local) = &specifier.local else {
        return ModuleInstanceState::Instantiated;
    };

    let name = local.name;
    let mut current_block_stmts = module_declaration_stmts.to_vec();
    loop {
        let mut found = false;
        for stmt in &current_block_stmts {
            match stmt {
                Statement::VariableDeclaration(decl) => {
                    decl.bound_names(&mut |id| {
                        if id.name == name {
                            found = true;
                        }
                    });
                }
                match_declaration!(Statement) => {
                    if stmt.to_declaration().id().is_some_and(|id| id.name == name) {
                        found = true;
                    }
                }
                Statement::ExportNamedDeclaration(decl) => match decl.declaration.as_ref() {
                    Some(Declaration::VariableDeclaration(decl)) => {
                        decl.bound_names(&mut |id| {
                            if id.name == name {
                                found = true;
                            }
                        });
                    }
                    Some(decl) => {
                        if decl.id().is_some_and(|id| id.name == name) {
                            found = true;
                        }
                    }
                    None => {
                        continue;
                    }
                },
                Statement::ExportDefaultDeclaration(decl) => match &decl.declaration {
                    ExportDefaultDeclarationKind::FunctionDeclaration(decl)
                        if decl.id.as_ref().is_some_and(|id| id.name == name) =>
                    {
                        found = true;
                    }
                    ExportDefaultDeclarationKind::ClassDeclaration(decl)
                        if decl.id.as_ref().is_some_and(|id| id.name == name) =>
                    {
                        found = true;
                    }
                    ExportDefaultDeclarationKind::TSInterfaceDeclaration(decl)
                        if decl.id.name == name =>
                    {
                        found = true;
                    }
                    _ => {}
                },
                _ => {}
            }

            if found {
                if matches!(stmt, Statement::TSImportEqualsDeclaration(_)) {
                    // Treat re-exports of import aliases as instantiated,
                    // since they're ambiguous. This is consistent with
                    // `export import x = mod.x` being treated as instantiated:
                    //   import x = mod.x;
                    //   export { x };
                    return ModuleInstanceState::Instantiated;
                }
                return get_module_instance_state_for_statement(
                    builder,
                    stmt,
                    current_node_id,
                    &mut Vec::default(), // No need to check export specifier
                );
            }
        }

        let Some((node_id, node)) =
            builder.nodes.ancestors_enumerated(current_node_id).find(|(_, node)| {
                matches!(
                    node.kind(),
                    AstKind::Program(_) | AstKind::TSModuleBlock(_) | AstKind::BlockStatement(_)
                )
            })
        else {
            break;
        };

        current_node_id = node_id;
        current_block_stmts.clear();
        // Didn't find the declaration whose name matches export specifier
        // in the current block, so we need to check the parent block.
        current_block_stmts.extend(match node.kind() {
            AstKind::Program(program) => program.body.iter(),
            AstKind::TSModuleBlock(block) => block.body.iter(),
            AstKind::BlockStatement(block) => block.body.iter(),
            _ => unreachable!(),
        });
    }

    // Not found in any of the statements
    ModuleInstanceState::Instantiated
}

impl<'a> Binder<'a> for TSTypeParameter<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        let scope_id = if matches!(
            builder.nodes.parent_kind(builder.current_node_id),
            AstKind::TSInferType(_)
        ) {
            builder
                .scoping
                .scope_ancestors(builder.current_scope_id)
                .find(|scope_id| builder.scoping.scope_flags(*scope_id).is_ts_conditional())
        } else {
            None
        };

        let symbol_id = builder.declare_symbol_on_scope(
            self.name.span,
            self.name.name,
            scope_id.unwrap_or(builder.current_scope_id),
            SymbolFlags::TypeParameter,
            SymbolFlags::TypeParameterExcludes,
        );
        self.name.symbol_id.set(Some(symbol_id));
    }
}

impl<'a> Binder<'a> for TSMappedType<'a> {
    fn bind(&self, builder: &mut SemanticBuilder<'a>) {
        let symbol_id = builder.declare_symbol_on_scope(
            self.key.span,
            self.key.name,
            builder.current_scope_id,
            SymbolFlags::TypeParameter,
            SymbolFlags::TypeParameterExcludes,
        );
        self.key.symbol_id.set(Some(symbol_id));
    }
}