tsz-binder 0.1.2

TypeScript name binder for the tsz compiler
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
//! Module and namespace declaration binding.
//!
//! This module handles binding of module/namespace declarations, including
//! ambient modules, module augmentation, export population, and symbol visibility.

use crate::state::BinderState;
use crate::{ContainerKind, Symbol, SymbolId, SymbolTable, symbol_flags};
use tsz_parser::parser::node::{Node, NodeArena};
use tsz_parser::parser::node_flags;
use tsz_parser::parser::syntax_kind_ext;
use tsz_parser::{NodeIndex, NodeList};
use tsz_scanner::SyntaxKind;

impl BinderState {
    /// Check if `idx` is nested inside an ambient module (one with `declare` or
    /// a string-literal name). Walks up through `MODULE_BLOCK` / `MODULE_DECLARATION`
    /// ancestors until it finds one that is ambient or reaches the source file.
    fn is_inside_ambient_module(arena: &NodeArena, idx: NodeIndex) -> bool {
        let mut current = idx;
        // Walk up through the AST looking for an ambient ancestor
        for _ in 0..32 {
            // limit depth to prevent infinite loop
            let Some(ext) = arena.get_extended(current) else {
                return false;
            };
            let parent_idx = ext.parent;
            let Some(parent_node) = arena.get(parent_idx) else {
                return false;
            };
            if parent_node.kind == syntax_kind_ext::MODULE_DECLARATION
                && let Some(parent_module) = arena.get_module(parent_node)
            {
                // Check if this ancestor has `declare` modifier
                if Self::has_declare_modifier(arena, parent_module.modifiers.as_ref()) {
                    return true;
                }
                // Check if this ancestor has a string-literal name
                if let Some(name_node) = arena.get(parent_module.name)
                    && (name_node.kind == SyntaxKind::StringLiteral as u16
                        || name_node.kind == SyntaxKind::NoSubstitutionTemplateLiteral as u16)
                {
                    return true;
                }
            }
            // Keep walking up
            current = parent_idx;
        }
        false
    }

    pub(crate) fn bind_module_declaration(
        &mut self,
        arena: &NodeArena,
        node: &Node,
        idx: NodeIndex,
    ) {
        if let Some(module) = arena.get_module(node) {
            if self.in_module_augmentation
                && let Some(ref module_spec) = self.current_augmented_module
                && let Some(name) = Self::get_identifier_name(arena, module.name)
            {
                self.module_augmentations
                    .entry(module_spec.clone())
                    .or_default()
                    .push(crate::state::ModuleAugmentation::new(name.to_string(), idx));
            }

            let is_global_augmentation = u32::from(node.flags) & node_flags::GLOBAL_AUGMENTATION
                != 0
                || arena
                    .get(module.name)
                    .and_then(|name_node| {
                        if let Some(ident) = arena.get_identifier(name_node) {
                            return Some(ident.escaped_text == "global");
                        }
                        if name_node.kind == SyntaxKind::GlobalKeyword as u16 {
                            return Some(true);
                        }
                        None
                    })
                    .unwrap_or(false);

            if is_global_augmentation {
                if module.body.is_some() {
                    self.node_scope_ids
                        .insert(module.body.0, self.current_scope_id);
                    // Set flag so interface declarations inside are tracked as augmentations
                    let was_in_global_augmentation = self.in_global_augmentation;
                    self.in_global_augmentation = true;
                    self.bind_node(arena, module.body);
                    self.in_global_augmentation = was_in_global_augmentation;
                }
                return;
            }

            let mut declared_module_specifier: Option<String> = None;
            let mut is_augmentation = false;
            if let Some(name_node) = arena.get(module.name)
                && (name_node.kind == SyntaxKind::StringLiteral as u16
                    || name_node.kind == SyntaxKind::NoSubstitutionTemplateLiteral as u16)
            {
                // Ambient module declaration with string literal name
                if let Some(lit) = arena.get_literal(name_node)
                    && !lit.text.is_empty()
                {
                    let module_specifier = lit.text.clone();
                    declared_module_specifier = Some(module_specifier.clone());

                    // Rule #44: Detect module augmentation
                    // A `declare module "x"` in an external module (file with imports/exports)
                    // is a module augmentation if it references an existing or external module.
                    is_augmentation = self.is_external_module
                        && self.is_potential_module_augmentation(&module_specifier);

                    if is_augmentation {
                        // Track as module augmentation - bind body with augmentation context
                        if module.body.is_none() {
                            // Shorthand ambient module: `declare module "*.json";` (no body)
                            // Even when classified as augmentation, a bodyless declaration
                            // is a shorthand that makes matching imports resolve to `any`.
                            self.shorthand_ambient_modules.insert(module_specifier);
                        } else {
                            self.node_scope_ids
                                .insert(module.body.0, self.current_scope_id);
                            let was_in_augmentation = self.in_module_augmentation;
                            let prev_module = self.current_augmented_module.take();
                            self.in_module_augmentation = true;
                            self.current_augmented_module = Some(module_specifier);
                            self.bind_node(arena, module.body);
                            self.in_module_augmentation = was_in_augmentation;
                            self.current_augmented_module = prev_module;
                        }
                        return;
                    }

                    // Not an augmentation - track as ambient module declaration
                    self.declared_modules.insert(module_specifier);
                }
            }

            let name = Self::get_identifier_name(arena, module.name)
                .map(str::to_string)
                .or_else(|| {
                    arena
                        .get(module.name)
                        .and_then(|name_node| arena.get_literal(name_node))
                        .map(|lit| lit.text.clone())
                });
            let mut prior_exports: Option<SymbolTable> = None;
            let mut module_symbol_id = SymbolId::NONE;
            if let Some(name) = name {
                let mut is_exported = Self::has_export_modifier(arena, module.modifiers.as_ref());
                if !is_exported && let Some(ext) = arena.get_extended(idx) {
                    let parent_idx = ext.parent;
                    if let Some(parent_node) = arena.get(parent_idx)
                        && parent_node.kind == syntax_kind_ext::MODULE_DECLARATION
                        && let Some(parent_module) = arena.get_module(parent_node)
                        && parent_module.body == idx
                    {
                        is_exported = true;
                    }
                }

                if self.in_global_augmentation {
                    self.global_augmentations
                        .entry(name.clone())
                        .or_default()
                        .push(crate::state::GlobalAugmentation::new(idx));
                }

                let flags = symbol_flags::VALUE_MODULE | symbol_flags::NAMESPACE_MODULE;
                module_symbol_id = self.declare_symbol(&name, flags, idx, is_exported);
                prior_exports = self
                    .symbols
                    .get(module_symbol_id)
                    .and_then(|symbol| symbol.exports.as_ref())
                    .map(|exports| exports.as_ref().clone());
            }

            // Enter module scope
            self.enter_scope(ContainerKind::Module, idx);

            if let Some(exports) = prior_exports {
                for (name, &child_id) in exports.iter() {
                    self.current_scope.set(name.clone(), child_id);
                }
            }

            // Also register the MODULE_BLOCK body node with the same scope
            // so that identifiers inside the namespace can find their enclosing scope
            // when walking up through the parent chain (identifier -> ... -> MODULE_BLOCK -> MODULE_DECLARATION)
            if module.body.is_none() {
                // Shorthand ambient module declaration: `declare module "foo"` without body
                // Track this so imports from these modules are typed as `any`
                if let Some(name_node) = arena.get(module.name)
                    && let Some(lit) = arena.get_literal(name_node)
                    && !lit.text.is_empty()
                {
                    self.shorthand_ambient_modules.insert(lit.text.clone());
                }
            } else {
                self.node_scope_ids
                    .insert(module.body.0, self.current_scope_id);
            }

            self.bind_node(arena, module.body);

            // Populate exports for the module symbol
            if module_symbol_id.is_some() && module.body.is_some() {
                let mut is_ambient_module = !is_augmentation
                    && (declared_module_specifier.is_some()
                        || Self::has_declare_modifier(arena, module.modifiers.as_ref()));

                // Nested namespaces inside ambient contexts should treat declarations
                // as ambient-exported for symbol visibility. This covers:
                // - `declare module "x" { namespace N { ... } }` (external modules)
                // - `declare namespace A { namespace B { namespace C { ... } } }`
                // Walk up through all ancestors to find any ambient module.
                if !is_ambient_module && Self::is_inside_ambient_module(arena, idx) {
                    is_ambient_module = true;
                }
                self.populate_module_exports(
                    arena,
                    module.body,
                    module_symbol_id,
                    is_ambient_module,
                );
                if let Some(module_specifier) = declared_module_specifier.as_ref()
                    && let Some(symbol) = self.symbols.get(module_symbol_id)
                    && let Some(exports) = symbol.exports.as_ref()
                    && !exports.is_empty()
                {
                    self.module_exports
                        .insert(module_specifier.clone(), exports.as_ref().clone());
                }
            }

            self.exit_scope(arena);
        }
    }

    /// Populate the exports table of a module/namespace symbol based on exported declarations in its body.
    pub(crate) fn populate_module_exports(
        &mut self,
        arena: &NodeArena,
        body_idx: NodeIndex,
        module_symbol_id: SymbolId,
        is_ambient_module: bool,
    ) {
        // Get the module block statements
        let statements = if let Some(module_block) = arena.get_module_block_at(body_idx) {
            if let Some(stmts) = &module_block.statements {
                &stmts.nodes
            } else {
                return;
            }
        } else {
            return;
        };

        for &stmt_idx in statements {
            if let Some(stmt_node) = arena.get(stmt_idx) {
                // Check for export modifier
                let mut is_exported = match stmt_node.kind {
                    syntax_kind_ext::VARIABLE_STATEMENT => arena
                        .get_variable(stmt_node)
                        .and_then(|v| v.modifiers.as_ref())
                        .is_some_and(|mods| Self::has_export_modifier_any(arena, mods)),
                    syntax_kind_ext::FUNCTION_DECLARATION => arena
                        .get_function(stmt_node)
                        .and_then(|f| f.modifiers.as_ref())
                        .is_some_and(|mods| Self::has_export_modifier_any(arena, mods)),
                    syntax_kind_ext::CLASS_DECLARATION => arena
                        .get_class(stmt_node)
                        .and_then(|c| c.modifiers.as_ref())
                        .is_some_and(|mods| Self::has_export_modifier_any(arena, mods)),
                    syntax_kind_ext::INTERFACE_DECLARATION => arena
                        .get_interface(stmt_node)
                        .and_then(|i| i.modifiers.as_ref())
                        .is_some_and(|mods| Self::has_export_modifier_any(arena, mods)),
                    syntax_kind_ext::TYPE_ALIAS_DECLARATION => arena
                        .get_type_alias(stmt_node)
                        .and_then(|t| t.modifiers.as_ref())
                        .is_some_and(|mods| Self::has_export_modifier_any(arena, mods)),
                    syntax_kind_ext::ENUM_DECLARATION => arena
                        .get_enum(stmt_node)
                        .and_then(|e| e.modifiers.as_ref())
                        .is_some_and(|mods| Self::has_export_modifier_any(arena, mods)),
                    syntax_kind_ext::MODULE_DECLARATION => arena
                        .get_module(stmt_node)
                        .and_then(|m| m.modifiers.as_ref())
                        .is_some_and(|mods| Self::has_export_modifier_any(arena, mods)),
                    syntax_kind_ext::EXPORT_DECLARATION | syntax_kind_ext::EXPORT_ASSIGNMENT => {
                        true
                    }
                    _ => false,
                };
                if is_ambient_module {
                    is_exported = true;
                }

                if is_exported {
                    // Collect the exported names and direct symbol mappings first
                    let mut exported_names = Vec::new();
                    let mut exported_symbols: Vec<(String, SymbolId)> = Vec::new();
                    let mut collect_var_exports =
                        |var_stmt: &tsz_parser::parser::node::VariableData| {
                            for &list_idx in &var_stmt.declarations.nodes {
                                if let Some(list_node) = arena.get(list_idx)
                                    && let Some(decl_list) = arena.get_variable(list_node)
                                {
                                    for &decl_idx in &decl_list.declarations.nodes {
                                        if let Some(decl_node) = arena.get(decl_idx)
                                            && let Some(decl) =
                                                arena.get_variable_declaration(decl_node)
                                            && let Some(name_node) = arena.get(decl.name)
                                            && let Some(ident) = arena.get_identifier(name_node)
                                        {
                                            exported_names.push(ident.escaped_text.clone());
                                            if let Some(&sym_id) =
                                                self.node_symbols.get(&decl.name.0)
                                            {
                                                exported_symbols
                                                    .push((ident.escaped_text.clone(), sym_id));
                                            }
                                        }
                                    }
                                } else if let Some(decl_node) = arena.get(list_idx)
                                    && let Some(decl) = arena.get_variable_declaration(decl_node)
                                    && let Some(name_node) = arena.get(decl.name)
                                    && let Some(ident) = arena.get_identifier(name_node)
                                {
                                    exported_names.push(ident.escaped_text.clone());
                                    if let Some(&sym_id) = self.node_symbols.get(&decl.name.0) {
                                        exported_symbols.push((ident.escaped_text.clone(), sym_id));
                                    }
                                }
                            }
                        };

                    match stmt_node.kind {
                        syntax_kind_ext::VARIABLE_STATEMENT => {
                            if let Some(var_stmt) = arena.get_variable(stmt_node) {
                                collect_var_exports(var_stmt);
                            }
                        }
                        syntax_kind_ext::FUNCTION_DECLARATION => {
                            if let Some(func) = arena.get_function(stmt_node)
                                && let Some(name) = Self::get_identifier_name(arena, func.name)
                            {
                                exported_names.push(name.to_string());
                            }
                        }
                        syntax_kind_ext::CLASS_DECLARATION => {
                            if let Some(class) = arena.get_class(stmt_node)
                                && let Some(name) = Self::get_identifier_name(arena, class.name)
                            {
                                exported_names.push(name.to_string());
                            }
                        }
                        syntax_kind_ext::ENUM_DECLARATION => {
                            if let Some(enm) = arena.get_enum(stmt_node)
                                && let Some(name) = Self::get_identifier_name(arena, enm.name)
                            {
                                exported_names.push(name.to_string());
                            }
                        }
                        syntax_kind_ext::INTERFACE_DECLARATION => {
                            if let Some(iface) = arena.get_interface(stmt_node)
                                && let Some(name) = Self::get_identifier_name(arena, iface.name)
                            {
                                exported_names.push(name.to_string());
                            }
                        }
                        syntax_kind_ext::TYPE_ALIAS_DECLARATION => {
                            if let Some(alias) = arena.get_type_alias(stmt_node)
                                && let Some(name) = Self::get_identifier_name(arena, alias.name)
                            {
                                exported_names.push(name.to_string());
                            }
                        }
                        syntax_kind_ext::MODULE_DECLARATION => {
                            if let Some(module) = arena.get_module(stmt_node) {
                                let name = Self::get_identifier_name(arena, module.name)
                                    .map(str::to_string)
                                    .or_else(|| {
                                        arena
                                            .get(module.name)
                                            .and_then(|name_node| arena.get_literal(name_node))
                                            .map(|lit| lit.text.clone())
                                    });
                                if let Some(name) = name {
                                    exported_names.push(name);
                                }
                            }
                        }
                        syntax_kind_ext::EXPORT_ASSIGNMENT => {
                            if let Some(assign) = arena.get_export_assignment(stmt_node)
                                && let Some(target_name) =
                                    Self::get_identifier_name(arena, assign.expression)
                                && let Some(sym_id) = self
                                    .current_scope
                                    .get(target_name)
                                    .or_else(|| self.file_locals.get(target_name))
                            {
                                if assign.is_export_equals {
                                    exported_symbols.push(("export=".to_string(), sym_id));

                                    // Also expose members of the export-assignment target for
                                    // named import compatibility (e.g. `export = alias; import { f }`).
                                    let mut target_sym_id = sym_id;

                                    if let Some(target_sym) = self.symbols.get(sym_id)
                                        && (target_sym.flags & symbol_flags::ALIAS) != 0
                                    {
                                        let decl_idx = if target_sym.value_declaration.is_none() {
                                            target_sym
                                                .declarations
                                                .first()
                                                .copied()
                                                .unwrap_or(NodeIndex::NONE)
                                        } else {
                                            target_sym.value_declaration
                                        };

                                        if decl_idx.is_some()
                                            && let Some(decl_node) = arena.get(decl_idx)
                                            && decl_node.kind
                                                == syntax_kind_ext::IMPORT_EQUALS_DECLARATION
                                            && let Some(import_decl) =
                                                arena.get_import_decl(decl_node)
                                        {
                                            let module_ref = import_decl.module_specifier;
                                            if let Some(module_ref_node) = arena.get(module_ref)
                                                && module_ref_node.kind
                                                    != SyntaxKind::StringLiteral as u16
                                                && let Some(ref_name) =
                                                    Self::get_identifier_name(arena, module_ref)
                                                && let Some(resolved) = self
                                                    .current_scope
                                                    .get(ref_name)
                                                    .or_else(|| self.file_locals.get(ref_name))
                                            {
                                                target_sym_id = resolved;
                                            }
                                        }
                                    }

                                    if let Some(target_symbol) = self.symbols.get(target_sym_id) {
                                        let collect_members_from_symbol =
                                            |symbol: &Symbol,
                                             exported_symbols: &mut Vec<(String, SymbolId)>| {
                                                if let Some(exports) = symbol.exports.as_ref() {
                                                    for (export_name, &export_sym_id) in exports.iter() {
                                                        if export_name != "export=" {
                                                            exported_symbols.push((
                                                                export_name.clone(),
                                                                export_sym_id,
                                                            ));
                                                        }
                                                    }
                                                }
                                                if let Some(members) = symbol.members.as_ref() {
                                                    for (member_name, &member_sym_id) in members.iter() {
                                                        exported_symbols
                                                            .push((member_name.clone(), member_sym_id));
                                                    }
                                                }
                                            };

                                        collect_members_from_symbol(
                                            target_symbol,
                                            &mut exported_symbols,
                                        );

                                        // Some declaration patterns keep value and namespace halves in
                                        // sibling symbols with the same name (e.g. function + namespace).
                                        // Include namespace-shaped siblings so `export = X` exposes all
                                        // merged members for named import compatibility.
                                        for candidate_id in self
                                            .symbols
                                            .find_all_by_name(&target_symbol.escaped_name)
                                        {
                                            if candidate_id == target_sym_id {
                                                continue;
                                            }
                                            let Some(candidate_symbol) =
                                                self.symbols.get(candidate_id)
                                            else {
                                                continue;
                                            };
                                            if (candidate_symbol.flags
                                                & (symbol_flags::MODULE
                                                    | symbol_flags::NAMESPACE_MODULE
                                                    | symbol_flags::VALUE_MODULE))
                                                == 0
                                            {
                                                continue;
                                            }
                                            collect_members_from_symbol(
                                                candidate_symbol,
                                                &mut exported_symbols,
                                            );
                                        }
                                    }
                                } else {
                                    // export default target_name;
                                    exported_symbols.push(("default".to_string(), sym_id));
                                }
                            }
                        }
                        syntax_kind_ext::EXPORT_DECLARATION => {
                            if let Some(export_decl) = arena.get_export_decl(stmt_node)
                                && export_decl.export_clause.is_some()
                                && let Some(clause_node) = arena.get(export_decl.export_clause)
                            {
                                if export_decl.is_default_export {
                                    if let Some(sym_id) = self.current_scope.get("default") {
                                        exported_symbols.push(("default".to_string(), sym_id));
                                    }
                                } else {
                                    match clause_node.kind {
                                        syntax_kind_ext::VARIABLE_STATEMENT => {
                                            if let Some(var_stmt) = arena.get_variable(clause_node)
                                            {
                                                collect_var_exports(var_stmt);
                                            }
                                        }
                                        syntax_kind_ext::FUNCTION_DECLARATION => {
                                            if let Some(func) = arena.get_function(clause_node)
                                                && let Some(name) =
                                                    Self::get_identifier_name(arena, func.name)
                                            {
                                                exported_names.push(name.to_string());
                                                if let Some(&sym_id) = self
                                                    .node_symbols
                                                    .get(&export_decl.export_clause.0)
                                                {
                                                    exported_symbols
                                                        .push((name.to_string(), sym_id));
                                                }
                                            }
                                        }
                                        syntax_kind_ext::CLASS_DECLARATION => {
                                            if let Some(class) = arena.get_class(clause_node)
                                                && let Some(name) =
                                                    Self::get_identifier_name(arena, class.name)
                                            {
                                                exported_names.push(name.to_string());
                                                if let Some(&sym_id) = self
                                                    .node_symbols
                                                    .get(&export_decl.export_clause.0)
                                                {
                                                    exported_symbols
                                                        .push((name.to_string(), sym_id));
                                                }
                                            }
                                        }
                                        syntax_kind_ext::ENUM_DECLARATION => {
                                            if let Some(enm) = arena.get_enum(clause_node)
                                                && let Some(name) =
                                                    Self::get_identifier_name(arena, enm.name)
                                            {
                                                exported_names.push(name.to_string());
                                                if let Some(&sym_id) = self
                                                    .node_symbols
                                                    .get(&export_decl.export_clause.0)
                                                {
                                                    exported_symbols
                                                        .push((name.to_string(), sym_id));
                                                }
                                            }
                                        }
                                        syntax_kind_ext::INTERFACE_DECLARATION => {
                                            if let Some(iface) = arena.get_interface(clause_node)
                                                && let Some(name) =
                                                    Self::get_identifier_name(arena, iface.name)
                                            {
                                                exported_names.push(name.to_string());
                                                if let Some(&sym_id) = self
                                                    .node_symbols
                                                    .get(&export_decl.export_clause.0)
                                                {
                                                    exported_symbols
                                                        .push((name.to_string(), sym_id));
                                                }
                                            }
                                        }
                                        syntax_kind_ext::TYPE_ALIAS_DECLARATION => {
                                            if let Some(alias) = arena.get_type_alias(clause_node)
                                                && let Some(name) =
                                                    Self::get_identifier_name(arena, alias.name)
                                            {
                                                exported_names.push(name.to_string());
                                                if let Some(&sym_id) = self
                                                    .node_symbols
                                                    .get(&export_decl.export_clause.0)
                                                {
                                                    exported_symbols
                                                        .push((name.to_string(), sym_id));
                                                }
                                            }
                                        }
                                        syntax_kind_ext::MODULE_DECLARATION => {
                                            if let Some(module) = arena.get_module(clause_node) {
                                                let name =
                                                    Self::get_identifier_name(arena, module.name)
                                                        .map(str::to_string)
                                                        .or_else(|| {
                                                            arena
                                                                .get(module.name)
                                                                .and_then(|name_node| {
                                                                    arena.get_literal(name_node)
                                                                })
                                                                .map(|lit| lit.text.clone())
                                                        });
                                                if let Some(name) = name {
                                                    exported_names.push(name.clone());
                                                    if let Some(&sym_id) = self
                                                        .node_symbols
                                                        .get(&export_decl.export_clause.0)
                                                    {
                                                        exported_symbols
                                                            .push((name.clone(), sym_id));
                                                    }
                                                }
                                            }
                                        }
                                        syntax_kind_ext::NAMED_EXPORTS => {
                                            if let Some(named_exports) =
                                                arena.get_named_imports(clause_node)
                                            {
                                                for &specifier_idx in &named_exports.elements.nodes
                                                {
                                                    if let Some(spec_node) =
                                                        arena.get(specifier_idx)
                                                        && let Some(spec) =
                                                            arena.get_specifier(spec_node)
                                                    {
                                                        let name_idx = if spec.name.is_none() {
                                                            spec.property_name
                                                        } else {
                                                            spec.name
                                                        };
                                                        if let Some(name_node) = arena.get(name_idx)
                                                            && let Some(ident) =
                                                                arena.get_identifier(name_node)
                                                        {
                                                            exported_names
                                                                .push(ident.escaped_text.clone());
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        _ => {}
                                    }
                                }
                            }
                        }
                        _ => {}
                    }

                    if is_ambient_module {
                        let in_scope: Vec<String> = exported_names
                            .iter()
                            .filter(|name| {
                                self.current_scope.has(name.as_str())
                                    || self.file_locals.has(name.as_str())
                            })
                            .cloned()
                            .collect();
                        let module_name = self
                            .symbols
                            .get(module_symbol_id)
                            .map_or("<unknown>", |sym| sym.escaped_name.as_str());
                        tracing::debug!(
                            module_name,
                            exported_names = ?exported_names,
                            in_scope = ?in_scope,
                            "Ambient module export candidates"
                        );
                    }

                    // Now add them to exports
                    for (name, sym_id) in &exported_symbols {
                        if let Some(module_sym) = self.symbols.get_mut(module_symbol_id) {
                            let exports = module_sym
                                .exports
                                .get_or_insert_with(|| Box::new(SymbolTable::new()));
                            exports.set(name.clone(), *sym_id);
                        }
                        if let Some(child_sym) = self.symbols.get_mut(*sym_id) {
                            child_sym.is_exported = true;
                        }
                    }
                    for name in &exported_names {
                        if exported_symbols.iter().any(|(n, _)| n == name) {
                            continue;
                        }
                        if let Some(sym_id) = self
                            .current_scope
                            .get(name)
                            .or_else(|| self.file_locals.get(name))
                        {
                            if let Some(module_sym) = self.symbols.get_mut(module_symbol_id) {
                                let exports = module_sym
                                    .exports
                                    .get_or_insert_with(|| Box::new(SymbolTable::new()));
                                exports.set(name.clone(), sym_id);
                            }
                            // Mark the child symbol as exported
                            if let Some(child_sym) = self.symbols.get_mut(sym_id) {
                                child_sym.is_exported = true;
                            }
                        }
                    }
                }
            }
        }
    }

    /// Check if any modifier in a `NodeList` is the export keyword.
    pub(crate) fn has_export_modifier_any(arena: &NodeArena, modifiers: &NodeList) -> bool {
        for &mod_idx in &modifiers.nodes {
            if let Some(mod_node) = arena.get(mod_idx)
                && mod_node.kind == SyntaxKind::ExportKeyword as u16
            {
                return true;
            }
        }
        false
    }
}