macroforge_ts_syn 0.1.81

TypeScript syntax types for compile-time macro code generation
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
//! AST lowering from Oxc types to IR representations.

use crate::LoweredTarget;
use crate::TsSynError;
use crate::abi::*;
use ::oxc::ast::ast::*;
use oxc::span::GetSpan;
use std::collections::HashSet;

/// Convert a 0-based Oxc span to a 1-based SpanIR (matching the patch applicator convention).
fn oxc_span_ir(span: oxc::span::Span) -> SpanIR {
    SpanIR::new(span.start + 1, span.end + 1)
}

/// Collect JSDoc field-level decorators (e.g. `/** @serde({ rename: "user_id" }) */`)
/// from the source text preceding a given 0-based byte offset.
///
/// This is the Oxc equivalent of the SWC `collect_leading_macro_directives`.
fn collect_leading_decorators_oxc(
    source: &str,
    target_start_0: usize,
    valid_annotations: Option<&HashSet<String>>,
) -> Vec<DecoratorIR> {
    use crate::jsdoc::parse_all_macro_directives;

    if target_start_0 == 0 || target_start_0 > source.len() {
        return Vec::new();
    }

    let search_area = &source[..target_start_0];

    let Some(end_idx) = search_area.rfind("*/") else {
        return Vec::new();
    };

    let Some(start_idx) = search_area[..end_idx].rfind("/**") else {
        return Vec::new();
    };

    let end_of_comment_block = end_idx + 2;

    // Only accept if the comment is directly adjacent (modulo allowed modifiers)
    let between = &search_area[end_of_comment_block..];
    let between_trimmed = between.trim();
    if !between_trimmed.is_empty() {
        let allowed_modifiers = ["export", "declare", "abstract", "default", "async"];
        let remaining: String = between_trimmed
            .split_whitespace()
            .filter(|word| !allowed_modifiers.contains(word))
            .collect::<Vec<_>>()
            .join(" ");
        if !remaining.is_empty() {
            return Vec::new();
        }
    }

    let mut all_directives = Vec::new();
    let mut current_start = start_idx;
    let mut current_end = end_idx;

    loop {
        let comment_body = &search_area[current_start + 3..current_end];

        // Skip macro import comments
        let body_lower = comment_body.to_ascii_lowercase();
        let is_macro_import = body_lower.contains("import") && body_lower.contains("macro");

        let directives = if is_macro_import {
            Vec::new()
        } else {
            parse_all_macro_directives(comment_body, valid_annotations)
        };

        for (name, args_src) in directives {
            // Use 1-based spans for SpanIR (matching the convention)
            all_directives.push(DecoratorIR {
                name,
                args_src,
                span: SpanIR::new(current_start as u32 + 1, (current_end + 2) as u32 + 1),
                #[cfg(feature = "swc")]
                node: None,
            });
        }

        // Check for adjacent preceding comment
        let before_comment = &search_area[..current_start];
        let before_trimmed = before_comment.trim_end();

        if !before_trimmed.ends_with("*/") {
            break;
        }

        let prev_end = before_trimmed.len() - 2;
        let Some(prev_start) = before_trimmed[..prev_end].rfind("/**") else {
            break;
        };

        current_start = prev_start;
        current_end = prev_end;
    }

    all_directives
}

pub fn lower_classes_oxc(
    program: &Program<'_>,
    source: &str,
    filter: Option<&HashSet<String>>,
) -> Result<Vec<ClassIR>, TsSynError> {
    let mut classes = Vec::new();
    for stmt in &program.body {
        match stmt {
            Statement::ClassDeclaration(decl) => {
                if let Some(class_ir) = lower_class(decl, source, filter) {
                    classes.push(class_ir);
                }
            }
            Statement::ExportNamedDeclaration(decl) => {
                if let Some(Declaration::ClassDeclaration(class_decl)) = &decl.declaration
                    && let Some(class_ir) = lower_class(class_decl, source, filter)
                {
                    classes.push(class_ir);
                }
            }
            Statement::ExportDefaultDeclaration(decl) => {
                if let ExportDefaultDeclarationKind::ClassDeclaration(class_decl) =
                    &decl.declaration
                    && let Some(class_ir) = lower_class(class_decl, source, filter)
                {
                    classes.push(class_ir);
                }
            }
            _ => {}
        }
    }
    Ok(classes)
}

fn lower_class(
    decl: &Class<'_>,
    source: &str,
    filter: Option<&HashSet<String>>,
) -> Option<ClassIR> {
    let name = decl.id.as_ref()?.name.to_string();
    let span = oxc_span_ir(decl.span);
    let body_span = oxc_span_ir(decl.body.span);

    let type_params = decl
        .type_parameters
        .as_ref()
        .map(|tp| tp.params.iter().map(|p| p.name.to_string()).collect())
        .unwrap_or_default();

    let mut heritage = Vec::new();
    if let Some(super_class) = &decl.super_class
        && let Expression::Identifier(ident) = super_class
    {
        heritage.push(ident.name.to_string());
    }
    for item in &decl.implements {
        if let TSTypeName::IdentifierReference(ident) = &item.expression {
            heritage.push(ident.name.to_string());
        }
    }

    let mut decorators = Vec::new();
    // 1. Native decorators
    for dec in &decl.decorators {
        if let Expression::CallExpression(call) = &dec.expression {
            if let Expression::Identifier(ident) = &call.callee {
                decorators.push(DecoratorIR {
                    name: ident.name.to_string(),
                    args_src: source[call.span.start as usize..call.span.end as usize].to_string(),
                    span: oxc_span_ir(dec.span),
                    #[cfg(feature = "swc")]
                    node: None,
                });
            }
        } else if let Expression::Identifier(ident) = &dec.expression {
            decorators.push(DecoratorIR {
                name: ident.name.to_string(),
                args_src: String::new(),
                span: oxc_span_ir(dec.span),
                #[cfg(feature = "swc")]
                node: None,
            });
        }
    }
    // 2. JSDoc class-level decorators (e.g. @serde({ denyUnknownFields: true }))
    decorators.extend(collect_leading_decorators_oxc(
        source,
        decl.span.start as usize,
        filter,
    ));

    let mut fields = Vec::new();
    let mut methods = Vec::new();

    for element in &decl.body.body {
        match element {
            ClassElement::PropertyDefinition(prop) => {
                if let PropertyKey::StaticIdentifier(ident) = &prop.key {
                    let field_decorators =
                        collect_leading_decorators_oxc(source, prop.span.start as usize, filter);
                    fields.push(FieldIR {
                        name: ident.name.to_string(),
                        span: oxc_span_ir(prop.span),
                        ts_type: prop
                            .type_annotation
                            .as_ref()
                            .map(|ann| {
                                let sp = ann.type_annotation.span();
                                source[sp.start as usize..sp.end as usize].to_string()
                            })
                            .unwrap_or_else(|| "any".to_string()),
                        #[cfg(feature = "swc")]
                        type_ann: None,
                        optional: prop.optional,
                        readonly: prop.readonly,
                        visibility: match prop.accessibility {
                            Some(TSAccessibility::Private) => Visibility::Private,
                            Some(TSAccessibility::Protected) => Visibility::Protected,
                            _ => Visibility::Public,
                        },
                        decorators: field_decorators,
                        #[cfg(feature = "swc")]
                        prop_ast: None,
                    });
                }
            }
            ClassElement::MethodDefinition(method) => {
                if let PropertyKey::StaticIdentifier(ident) = &method.key {
                    let func = &method.value;

                    let type_params_src = func
                        .type_parameters
                        .as_ref()
                        .map(|tp| source[tp.span.start as usize..tp.span.end as usize].to_string())
                        .unwrap_or_default();

                    let params_src = {
                        let sp = func.params.span;
                        let raw = &source[sp.start as usize..sp.end as usize];
                        raw.strip_prefix('(')
                            .and_then(|s| s.strip_suffix(')'))
                            .unwrap_or(raw)
                            .trim()
                            .to_string()
                    };

                    let return_type_src = func
                        .return_type
                        .as_ref()
                        .map(|ann| {
                            let sp = ann.type_annotation.span();
                            source[sp.start as usize..sp.end as usize].to_string()
                        })
                        .unwrap_or_default();

                    let method_decorators =
                        collect_leading_decorators_oxc(source, method.span.start as usize, filter);
                    methods.push(MethodSigIR {
                        name: ident.name.to_string(),
                        span: oxc_span_ir(method.span),
                        type_params_src,
                        params_src,
                        return_type_src,
                        is_static: method.r#static,
                        is_async: method.value.r#async,
                        visibility: match method.accessibility {
                            Some(TSAccessibility::Private) => Visibility::Private,
                            Some(TSAccessibility::Protected) => Visibility::Protected,
                            _ => Visibility::Public,
                        },
                        decorators: method_decorators,
                        #[cfg(feature = "swc")]
                        member_ast: None,
                    });
                }
            }
            _ => {}
        }
    }

    Some(ClassIR {
        name,
        span,
        body_span,
        is_abstract: decl.r#abstract,
        type_params,
        heritage,
        decorators,
        #[cfg(feature = "swc")]
        decorators_ast: Vec::new(),
        fields,
        methods,
        #[cfg(feature = "swc")]
        members: Vec::new(),
    })
}

pub fn lower_interfaces_oxc(
    program: &Program<'_>,
    source: &str,
    filter: Option<&HashSet<String>>,
) -> Result<Vec<InterfaceIR>, TsSynError> {
    let mut interfaces = Vec::new();
    for stmt in &program.body {
        let decl = match stmt {
            Statement::TSInterfaceDeclaration(d) => Some(d.as_ref()),
            Statement::ExportNamedDeclaration(d) => match &d.declaration {
                Some(Declaration::TSInterfaceDeclaration(d)) => Some(d.as_ref()),
                _ => None,
            },
            _ => None,
        };
        if let Some(d) = decl {
            interfaces.push(lower_interface(d, source, filter));
        }
    }
    Ok(interfaces)
}

fn lower_interface(
    decl: &TSInterfaceDeclaration<'_>,
    source: &str,
    filter: Option<&HashSet<String>>,
) -> InterfaceIR {
    let name = decl.id.name.to_string();
    let span = oxc_span_ir(decl.span);
    let body_span = oxc_span_ir(decl.body.span);

    let type_params = decl
        .type_parameters
        .as_ref()
        .map(|tp| tp.params.iter().map(|p| p.name.to_string()).collect())
        .unwrap_or_default();

    let mut heritage = Vec::new();
    for ext in &decl.extends {
        if let Expression::Identifier(ident) = &ext.expression {
            heritage.push(ident.name.to_string());
        }
    }

    let (fields, methods) = lower_interface_members(&decl.body.body, source, filter);

    // JSDoc interface-level decorators
    let decorators = collect_leading_decorators_oxc(source, decl.span.start as usize, filter);

    InterfaceIR {
        name,
        span,
        body_span,
        type_params,
        heritage,
        decorators,
        fields,
        methods,
    }
}

fn lower_interface_members(
    body: &[TSSignature<'_>],
    source: &str,
    filter: Option<&HashSet<String>>,
) -> (Vec<InterfaceFieldIR>, Vec<InterfaceMethodIR>) {
    let mut fields = Vec::new();
    let mut methods = Vec::new();

    for elem in body {
        match elem {
            TSSignature::TSPropertySignature(prop) => {
                if let PropertyKey::StaticIdentifier(ident) = &prop.key {
                    let ts_type = prop
                        .type_annotation
                        .as_ref()
                        .map(|ann| {
                            let sp = ann.type_annotation.span();
                            source[sp.start as usize..sp.end as usize].to_string()
                        })
                        .unwrap_or_else(|| "any".to_string());

                    let field_decorators =
                        collect_leading_decorators_oxc(source, prop.span.start as usize, filter);
                    fields.push(InterfaceFieldIR {
                        name: ident.name.to_string(),
                        span: oxc_span_ir(prop.span),
                        ts_type,
                        optional: prop.optional,
                        readonly: prop.readonly,
                        decorators: field_decorators,
                    });
                }
            }
            TSSignature::TSMethodSignature(meth) => {
                if let PropertyKey::StaticIdentifier(ident) = &meth.key {
                    let params_src = {
                        let sp = meth.params.span;
                        let raw = &source[sp.start as usize..sp.end as usize];
                        raw.strip_prefix('(')
                            .and_then(|s| s.strip_suffix(')'))
                            .unwrap_or(raw)
                            .trim()
                            .to_string()
                    };

                    let type_params_src = meth
                        .type_parameters
                        .as_ref()
                        .map(|tp| source[tp.span.start as usize..tp.span.end as usize].to_string())
                        .unwrap_or_default();

                    let return_type_src = meth
                        .return_type
                        .as_ref()
                        .map(|ann| {
                            let sp = ann.type_annotation.span();
                            source[sp.start as usize..sp.end as usize].to_string()
                        })
                        .unwrap_or_else(|| "void".to_string());

                    let meth_decorators =
                        collect_leading_decorators_oxc(source, meth.span.start as usize, filter);
                    methods.push(InterfaceMethodIR {
                        name: ident.name.to_string(),
                        span: oxc_span_ir(meth.span),
                        type_params_src,
                        params_src,
                        return_type_src,
                        optional: meth.optional,
                        decorators: meth_decorators,
                    });
                }
            }
            _ => {}
        }
    }

    (fields, methods)
}

pub fn lower_enums_oxc(
    program: &Program<'_>,
    source: &str,
    _filter: Option<&HashSet<String>>,
) -> Result<Vec<EnumIR>, TsSynError> {
    let mut enums = Vec::new();
    for stmt in &program.body {
        let decl = match stmt {
            Statement::TSEnumDeclaration(d) => Some(d.as_ref()),
            Statement::ExportNamedDeclaration(d) => match &d.declaration {
                Some(Declaration::TSEnumDeclaration(d)) => Some(d.as_ref()),
                _ => None,
            },
            _ => None,
        };
        if let Some(d) = decl {
            enums.push(lower_enum(d, source));
        }
    }
    Ok(enums)
}

fn lower_enum(decl: &TSEnumDeclaration<'_>, source: &str) -> EnumIR {
    let name = decl.id.name.to_string();
    let span = oxc_span_ir(decl.span);

    let enum_source = &source[decl.span.start as usize..decl.span.end as usize];
    let body_span =
        if let (Some(open), Some(close)) = (enum_source.find('{'), enum_source.rfind('}')) {
            SpanIR::new(
                decl.span.start + open as u32 + 1,
                decl.span.start + close as u32 + 2,
            )
        } else {
            span
        };

    let mut variants = Vec::new();
    let mut next_auto_value: f64 = 0.0;

    for member in &decl.body.members {
        let member_name = match &member.id {
            TSEnumMemberName::Identifier(i) => i.name.to_string(),
            TSEnumMemberName::String(s) | TSEnumMemberName::ComputedString(s) => {
                s.value.to_string()
            }
            TSEnumMemberName::ComputedTemplateString(_) => continue,
        };

        let value = if let Some(init) = &member.initializer {
            match init {
                Expression::StringLiteral(s) => EnumValue::String(s.value.to_string()),
                Expression::NumericLiteral(n) => {
                    next_auto_value = n.value + 1.0;
                    EnumValue::Number(n.value)
                }
                Expression::UnaryExpression(unary)
                    if matches!(
                        unary.operator,
                        UnaryOperator::UnaryNegation | UnaryOperator::UnaryPlus
                    ) =>
                {
                    if let Expression::NumericLiteral(n) = &unary.argument {
                        let val = if matches!(unary.operator, UnaryOperator::UnaryNegation) {
                            -n.value
                        } else {
                            n.value
                        };
                        next_auto_value = val + 1.0;
                        EnumValue::Number(val)
                    } else {
                        let sp = init.span();
                        EnumValue::Expr(source[sp.start as usize..sp.end as usize].to_string())
                    }
                }
                _ => {
                    let sp = init.span();
                    EnumValue::Expr(source[sp.start as usize..sp.end as usize].to_string())
                }
            }
        } else {
            let val = next_auto_value;
            next_auto_value += 1.0;
            EnumValue::Number(val)
        };

        let variant_decorators =
            collect_leading_decorators_oxc(source, member.span.start as usize, None);
        variants.push(EnumVariantIR {
            name: member_name,
            span: oxc_span_ir(member.span),
            value,
            decorators: variant_decorators,
        });
    }

    let decorators = collect_leading_decorators_oxc(source, decl.span.start as usize, None);

    EnumIR {
        name,
        span,
        body_span,
        decorators,
        variants,
        is_const: decl.r#const,
    }
}

pub fn lower_type_aliases_oxc(
    program: &Program<'_>,
    source: &str,
    _filter: Option<&HashSet<String>>,
) -> Result<Vec<TypeAliasIR>, TsSynError> {
    let mut aliases = Vec::new();
    for stmt in &program.body {
        let decl = match stmt {
            Statement::TSTypeAliasDeclaration(d) => Some(d.as_ref()),
            Statement::ExportNamedDeclaration(d) => match &d.declaration {
                Some(Declaration::TSTypeAliasDeclaration(d)) => Some(d.as_ref()),
                _ => None,
            },
            _ => None,
        };
        if let Some(d) = decl {
            aliases.push(lower_type_alias(d, source));
        }
    }
    Ok(aliases)
}

fn lower_type_alias(decl: &TSTypeAliasDeclaration<'_>, source: &str) -> TypeAliasIR {
    let name = decl.id.name.to_string();
    let span = oxc_span_ir(decl.span);

    let type_params = decl
        .type_parameters
        .as_ref()
        .map(|tp| tp.params.iter().map(|p| p.name.to_string()).collect())
        .unwrap_or_default();

    let body = lower_type_body_oxc(&decl.type_annotation, source);

    let decorators = collect_leading_decorators_oxc(source, decl.span.start as usize, None);

    TypeAliasIR {
        name,
        span,
        decorators,
        type_params,
        body,
    }
}

fn lower_union_member_oxc(t: &TSType<'_>, source: &str) -> TypeMember {
    let sp = t.span();
    let text = source[sp.start as usize..sp.end as usize].to_string();
    let decorators = collect_leading_decorators_oxc(source, sp.start as usize, None);
    let kind = match t {
        TSType::TSLiteralType(_) => TypeMemberKind::Literal(text),
        TSType::TSTypeLiteral(lit) => {
            let (fields, _) = lower_interface_members(&lit.members, source, None);
            TypeMemberKind::Object { fields }
        }
        TSType::TSIntersectionType(inter) => {
            let members = inter
                .types
                .iter()
                .map(|t| lower_union_member_oxc(t, source))
                .collect();
            TypeMemberKind::Intersection(members)
        }
        TSType::TSParenthesizedType(paren) => {
            let mut inner = lower_union_member_oxc(&paren.type_annotation, source);
            if inner.decorators.is_empty() && !decorators.is_empty() {
                inner.decorators = decorators;
            }
            return inner;
        }
        _ => TypeMemberKind::TypeRef(text),
    };
    TypeMember::with_decorators(kind, decorators)
}

fn lower_type_body_oxc(ts_type: &TSType<'_>, source: &str) -> TypeBody {
    match ts_type {
        TSType::TSUnionType(union) => {
            let members = union
                .types
                .iter()
                .map(|t| lower_union_member_oxc(t, source))
                .collect();
            TypeBody::Union(members)
        }
        TSType::TSIntersectionType(inter) => {
            let members = inter
                .types
                .iter()
                .map(|t| lower_union_member_oxc(t, source))
                .collect();
            TypeBody::Intersection(members)
        }
        TSType::TSTypeLiteral(lit) => {
            let (fields, _methods) = lower_interface_members(&lit.members, source, None);
            TypeBody::Object { fields }
        }
        TSType::TSTupleType(tuple) => {
            let elements = tuple
                .element_types
                .iter()
                .map(|elem| {
                    let sp = elem.span();
                    source[sp.start as usize..sp.end as usize].to_string()
                })
                .collect();
            TypeBody::Tuple(elements)
        }
        _ => {
            let sp = ts_type.span();
            TypeBody::Other(source[sp.start as usize..sp.end as usize].to_string())
        }
    }
}

pub fn lower_targets_oxc(
    program: &Program<'_>,
    source: &str,
    filter: Option<&HashSet<String>>,
) -> Result<Vec<LoweredTarget>, TsSynError> {
    let mut targets = Vec::new();
    for class in lower_classes_oxc(program, source, filter)? {
        targets.push(LoweredTarget::Class(class));
    }
    for iface in lower_interfaces_oxc(program, source, filter)? {
        targets.push(LoweredTarget::Interface(iface));
    }
    for e in lower_enums_oxc(program, source, filter)? {
        targets.push(LoweredTarget::Enum(e));
    }
    for ta in lower_type_aliases_oxc(program, source, filter)? {
        targets.push(LoweredTarget::TypeAlias(ta));
    }
    Ok(targets)
}

/// Collect names of exported declarations from a parsed Oxc program.
pub fn collect_exported_names_oxc(program: &Program<'_>) -> HashSet<String> {
    let mut names = HashSet::new();

    for stmt in &program.body {
        match stmt {
            Statement::ExportNamedDeclaration(decl) => {
                if let Some(decl) = &decl.declaration {
                    match decl {
                        Declaration::ClassDeclaration(c) => {
                            if let Some(id) = &c.id {
                                names.insert(id.name.to_string());
                            }
                        }
                        Declaration::TSInterfaceDeclaration(i) => {
                            names.insert(i.id.name.to_string());
                        }
                        Declaration::TSEnumDeclaration(e) => {
                            names.insert(e.id.name.to_string());
                        }
                        Declaration::TSTypeAliasDeclaration(t) => {
                            names.insert(t.id.name.to_string());
                        }
                        _ => {}
                    }
                }
                // Handle export { foo, bar }
                for spec in &decl.specifiers {
                    names.insert(spec.local.name().to_string());
                }
            }
            Statement::ExportDefaultDeclaration(decl) => {
                if let ExportDefaultDeclarationKind::ClassDeclaration(c) = &decl.declaration
                    && let Some(id) = &c.id
                {
                    names.insert(id.name.to_string());
                }
            }
            _ => {}
        }
    }

    names
}