oxc_react_compiler 0.135.0

oxc integration for the Rust port of React 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
// Copyright (c) Meta Platforms, Inc. and affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

use std::collections::HashMap;

use indexmap::IndexMap;
use oxc_ast::AstKind;
use oxc_ast::ast::Program;
use oxc_semantic::Semantic;
use oxc_span::GetSpan;
use oxc_syntax::symbol::SymbolFlags;
use react_compiler_ast::scope::*;
use rustc_hash::FxHashMap;

/// Convert OXC's semantic analysis into React Compiler's ScopeInfo.
pub fn convert_scope_info(semantic: &Semantic, _program: &Program) -> ScopeInfo {
    let scoping = semantic.scoping();
    let nodes = semantic.nodes();

    let mut scopes: Vec<ScopeData> = Vec::new();
    let mut bindings: Vec<BindingData> = Vec::new();
    let mut node_to_scope: HashMap<u32, ScopeId> = HashMap::new();
    let mut node_to_scope_end: HashMap<u32, u32> = HashMap::new();
    // In OXC, span.start is used as node_id (OXC spans are unique).
    let mut node_id_to_scope: HashMap<u32, ScopeId> = HashMap::new();
    let mut ref_node_id_to_binding: IndexMap<u32, BindingId> = IndexMap::new();

    let mut symbol_to_binding: FxHashMap<oxc_syntax::symbol::SymbolId, BindingId> =
        FxHashMap::default();

    // First pass: Create all bindings from symbols
    for symbol_id in scoping.symbol_ids() {
        let symbol_flags = scoping.symbol_flags(symbol_id);
        let name = scoping.symbol_name(symbol_id).to_string();

        let kind = get_binding_kind(symbol_flags, semantic, symbol_id);

        let (declaration_type, declaration_start) =
            get_declaration_info(semantic, symbol_id, &name);

        let import = if matches!(kind, BindingKind::Module) {
            get_import_data(semantic, symbol_id)
        } else {
            None
        };

        let binding_id = BindingId(bindings.len() as u32);
        symbol_to_binding.insert(symbol_id, binding_id);

        bindings.push(BindingData {
            id: binding_id,
            name,
            kind,
            scope: ScopeId(0), // Placeholder, filled in second pass
            declaration_type,
            declaration_start,
            declaration_node_id: declaration_start,
            import,
        });
    }

    // Second pass: Create all scopes and update binding scope references
    for scope_id in scoping.scope_descendants_from_root() {
        let scope_flags = scoping.scope_flags(scope_id);
        let parent = scoping.scope_parent_id(scope_id);

        let our_scope_id = ScopeId(scope_id.index() as u32);

        let kind = get_scope_kind(scope_flags, semantic, scope_id);

        let mut scope_bindings: HashMap<String, BindingId> = HashMap::new();
        for symbol_id in scoping.iter_bindings_in(scope_id) {
            if let Some(&binding_id) = symbol_to_binding.get(&symbol_id) {
                let name = bindings[binding_id.0 as usize].name.clone();
                scope_bindings.insert(name, binding_id);
                bindings[binding_id.0 as usize].scope = our_scope_id;
            }
        }

        let node_id = scoping.get_node_id(scope_id);
        let node = nodes.get_node(node_id);
        let span = node.kind().span();
        let start = span.start;
        let end = span.end;
        // For function scopes inside object methods, also map the parent
        // ObjectProperty start so the compiler can look up the scope using the
        // ObjectMethod's start position (which matches the property start in Babel).
        if matches!(kind, ScopeKind::Function) {
            if let AstKind::Function(_) = node.kind() {
                let parent_node_id = nodes.parent_id(node_id);
                let parent_node = nodes.get_node(parent_node_id);
                match parent_node.kind() {
                    AstKind::ObjectProperty(prop)
                        if prop.method
                            || matches!(
                                prop.kind,
                                oxc_ast::ast::PropertyKind::Get | oxc_ast::ast::PropertyKind::Set
                            ) =>
                    {
                        let prop_start = parent_node.kind().span().start;
                        if prop_start != start {
                            node_to_scope.insert(prop_start, our_scope_id);
                            node_to_scope_end.insert(prop_start, end);
                            node_id_to_scope.insert(prop_start, our_scope_id);
                        }
                    }
                    _ => {}
                }
            }
        }

        if end > start {
            node_to_scope.insert(start, our_scope_id);
            node_to_scope_end.insert(start, end);
        } else {
            node_to_scope.insert(start, our_scope_id);
        }
        node_id_to_scope.insert(start, our_scope_id);

        scopes.push(ScopeData {
            id: our_scope_id,
            parent: parent.map(|p| ScopeId(p.index() as u32)),
            kind,
            bindings: scope_bindings,
        });
    }

    // Third pass: Map all resolved references to bindings
    for symbol_id in scoping.symbol_ids() {
        if let Some(&binding_id) = symbol_to_binding.get(&symbol_id) {
            for &ref_id in scoping.get_resolved_reference_ids(symbol_id) {
                let reference = scoping.get_reference(ref_id);
                let ref_node = nodes.get_node(reference.node_id());
                let start = ref_node.kind().span().start;
                ref_node_id_to_binding.insert(start, binding_id);
            }
        }
    }

    // Also map declaration identifiers to their bindings
    for symbol_id in scoping.symbol_ids() {
        if let Some(&binding_id) = symbol_to_binding.get(&symbol_id) {
            if let Some(start) = bindings[binding_id.0 as usize].declaration_start {
                ref_node_id_to_binding.entry(start).or_insert(binding_id);
            }
        }
    }

    // Map `export default function Foo` — Babel treats the ExportDefaultDeclaration
    // as a reference to the function's binding. OXC doesn't create a reference for
    // the export itself, so we add one at the export statement's start position.
    for stmt in &_program.body {
        if let oxc_ast::ast::Statement::ExportDefaultDeclaration(export) = stmt {
            if let oxc_ast::ast::ExportDefaultDeclarationKind::FunctionDeclaration(func) =
                &export.declaration
            {
                if let Some(id) = &func.id {
                    let name = id.name.as_str();
                    if let Some(symbol_id) = id.symbol_id.get() {
                        if let Some(&binding_id) = symbol_to_binding.get(&symbol_id) {
                            let export_start = export.span().start;
                            ref_node_id_to_binding.entry(export_start).or_insert(binding_id);
                        }
                    } else {
                        // Fallback: look up binding by name
                        for (sym_id, &bind_id) in &symbol_to_binding {
                            if scoping.symbol_name(*sym_id) == name {
                                let export_start = export.span().start;
                                ref_node_id_to_binding.entry(export_start).or_insert(bind_id);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    // Map `export default function Foo` — Babel treats the ExportDefaultDeclaration
    // as a reference to the function's binding. OXC doesn't create a reference for
    // the export itself, so we add one at the export statement's start position.
    for stmt in &_program.body {
        if let oxc_ast::ast::Statement::ExportDefaultDeclaration(export) = stmt {
            if let oxc_ast::ast::ExportDefaultDeclarationKind::FunctionDeclaration(func) =
                &export.declaration
            {
                if let Some(id) = &func.id {
                    let name = id.name.as_str();
                    if let Some(symbol_id) = id.symbol_id.get() {
                        if let Some(&binding_id) = symbol_to_binding.get(&symbol_id) {
                            ref_node_id_to_binding.entry(export.span().start).or_insert(binding_id);
                        }
                    } else {
                        // Fallback: look up binding by name
                        for (sym_id, &bind_id) in &symbol_to_binding {
                            if scoping.symbol_name(*sym_id) == name {
                                ref_node_id_to_binding
                                    .entry(export.span().start)
                                    .or_insert(bind_id);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    let program_scope = ScopeId(scoping.root_scope_id().index() as u32);

    ScopeInfo {
        scopes,
        bindings,
        node_to_scope,
        node_to_scope_end,
        reference_to_binding: IndexMap::new(),
        ref_node_id_to_binding,
        node_id_to_scope,
        program_scope,
    }
}

/// Map OXC ScopeFlags to our ScopeKind.
fn get_scope_kind(
    flags: oxc_syntax::scope::ScopeFlags,
    semantic: &Semantic,
    scope_id: oxc_syntax::scope::ScopeId,
) -> ScopeKind {
    if flags.contains(oxc_syntax::scope::ScopeFlags::Top) {
        return ScopeKind::Program;
    }

    if flags.intersects(oxc_syntax::scope::ScopeFlags::Function) {
        return ScopeKind::Function;
    }

    if flags.contains(oxc_syntax::scope::ScopeFlags::CatchClause) {
        return ScopeKind::Catch;
    }

    if flags.contains(oxc_syntax::scope::ScopeFlags::ClassStaticBlock) {
        return ScopeKind::Class;
    }

    // Check the AST node to determine if it's a for loop, class, or switch
    let node_id = semantic.scoping().get_node_id(scope_id);
    let node = semantic.nodes().get_node(node_id);
    match node.kind() {
        AstKind::ForStatement(_) | AstKind::ForInStatement(_) | AstKind::ForOfStatement(_) => {
            ScopeKind::For
        }
        AstKind::Class(_) => ScopeKind::Class,
        AstKind::SwitchStatement(_) => ScopeKind::Switch,
        _ => ScopeKind::Block,
    }
}

/// Map OXC SymbolFlags to our BindingKind.
fn get_binding_kind(
    flags: SymbolFlags,
    semantic: &Semantic,
    symbol_id: oxc_syntax::symbol::SymbolId,
) -> BindingKind {
    if flags.contains(SymbolFlags::Import) {
        return BindingKind::Module;
    }

    // Check the declaration node first — FormalParameter and CatchParameter
    // need to be detected before the FunctionScopedVariable flag check, because
    // OXC marks function parameters and catch parameters with FunctionScopedVariable.
    let decl_node = semantic.symbol_declaration(symbol_id);
    match decl_node.kind() {
        AstKind::FormalParameter(_) => return BindingKind::Param,
        AstKind::FormalParameterRest(_) => return BindingKind::Param,
        AstKind::CatchParameter(_) => return BindingKind::Let,
        AstKind::TSTypeAliasDeclaration(_) => return BindingKind::Local,
        AstKind::TSEnumDeclaration(_) => return BindingKind::Local,
        AstKind::TSModuleDeclaration(_) => return BindingKind::Local,
        AstKind::Function(_) => {
            if flags.contains(SymbolFlags::Function) {
                return BindingKind::Hoisted;
            }
            return BindingKind::Local;
        }
        AstKind::Class(_) => return BindingKind::Local,
        _ => {}
    }

    if flags.contains(SymbolFlags::FunctionScopedVariable) {
        return BindingKind::Var;
    }

    if flags.contains(SymbolFlags::BlockScopedVariable) {
        if flags.contains(SymbolFlags::ConstVariable) {
            return BindingKind::Const;
        } else {
            return BindingKind::Let;
        }
    }

    if flags.contains(SymbolFlags::Function) {
        BindingKind::Hoisted
    } else if flags.contains(SymbolFlags::Class) {
        BindingKind::Local
    } else {
        BindingKind::Unknown
    }
}

/// Get the declaration type string and start position for a binding.
fn get_declaration_info(
    semantic: &Semantic,
    symbol_id: oxc_syntax::symbol::SymbolId,
    name: &str,
) -> (String, Option<u32>) {
    let decl_node = semantic.symbol_declaration(symbol_id);
    let declaration_type = ast_kind_to_string(decl_node.kind());
    let declaration_start = find_binding_identifier_start(decl_node.kind(), name);
    (declaration_type, declaration_start)
}

/// Convert an AstKind to its Babel-equivalent string representation.
fn ast_kind_to_string(kind: AstKind) -> String {
    match kind {
        AstKind::BindingIdentifier(_) => "BindingIdentifier",
        AstKind::VariableDeclarator(_) => "VariableDeclarator",
        AstKind::Function(f) => {
            if f.is_declaration() {
                "FunctionDeclaration"
            } else {
                "FunctionExpression"
            }
        }
        AstKind::Class(c) => {
            if c.is_declaration() {
                "ClassDeclaration"
            } else {
                "ClassExpression"
            }
        }
        AstKind::FormalParameter(_) => "FormalParameter",
        AstKind::FormalParameterRest(_) => "FormalParameter",
        AstKind::ImportSpecifier(_) => "ImportSpecifier",
        AstKind::ImportDefaultSpecifier(_) => "ImportDefaultSpecifier",
        AstKind::ImportNamespaceSpecifier(_) => "ImportNamespaceSpecifier",
        AstKind::CatchClause(_) => "CatchClause",
        AstKind::CatchParameter(_) => "CatchClause",
        AstKind::TSTypeAliasDeclaration(_) => "TSTypeAliasDeclaration",
        AstKind::TSEnumDeclaration(_) => "TSEnumDeclaration",
        AstKind::TSModuleDeclaration(_) => "TSModuleDeclaration",
        _ => "Unknown",
    }
    .to_string()
}

/// Find the binding identifier's start position within an AST node.
fn find_binding_identifier_start(kind: AstKind, name: &str) -> Option<u32> {
    match kind {
        AstKind::BindingIdentifier(ident) => {
            if ident.name.as_str() == name {
                Some(ident.span.start)
            } else {
                None
            }
        }
        AstKind::VariableDeclarator(decl) => find_identifier_in_pattern(&decl.id, name),
        AstKind::Function(func) => func
            .id
            .as_ref()
            .and_then(|id| if id.name.as_str() == name { Some(id.span.start) } else { None }),
        AstKind::Class(class) => class
            .id
            .as_ref()
            .and_then(|id| if id.name.as_str() == name { Some(id.span.start) } else { None }),
        AstKind::FormalParameter(param) => find_identifier_in_pattern(&param.pattern, name),
        AstKind::FormalParameterRest(rest) => find_identifier_in_pattern(&rest.rest.argument, name),
        AstKind::ImportSpecifier(spec) => Some(spec.local.span.start),
        AstKind::ImportDefaultSpecifier(spec) => Some(spec.local.span.start),
        AstKind::ImportNamespaceSpecifier(spec) => Some(spec.local.span.start),
        AstKind::CatchClause(catch) => {
            catch.param.as_ref().and_then(|p| find_identifier_in_pattern(&p.pattern, name))
        }
        AstKind::CatchParameter(param) => find_identifier_in_pattern(&param.pattern, name),
        AstKind::TSTypeAliasDeclaration(decl) => {
            if decl.id.name.as_str() == name {
                Some(decl.id.span.start)
            } else {
                None
            }
        }
        AstKind::TSEnumDeclaration(decl) => {
            if decl.id.name.as_str() == name {
                Some(decl.id.span.start)
            } else {
                None
            }
        }
        AstKind::TSModuleDeclaration(decl) => {
            match &decl.id {
                oxc_ast::ast::TSModuleDeclarationName::Identifier(id) => {
                    if id.name.as_str() == name { Some(id.span.start) } else { None }
                }
                _ => None,
            }
        }
        _ => None,
    }
}

/// Recursively find a binding identifier within a binding pattern.
fn find_identifier_in_pattern(pattern: &oxc_ast::ast::BindingPattern, name: &str) -> Option<u32> {
    use oxc_ast::ast::BindingPattern;

    match pattern {
        BindingPattern::BindingIdentifier(ident) => {
            if ident.name.as_str() == name {
                Some(ident.span.start)
            } else {
                None
            }
        }
        BindingPattern::ObjectPattern(obj) => {
            for prop in &obj.properties {
                if let Some(start) = find_identifier_in_pattern(&prop.value, name) {
                    return Some(start);
                }
            }
            if let Some(rest) = &obj.rest {
                if let Some(start) = find_identifier_in_pattern(&rest.argument, name) {
                    return Some(start);
                }
            }
            None
        }
        BindingPattern::ArrayPattern(arr) => {
            for element in arr.elements.iter().flatten() {
                if let Some(start) = find_identifier_in_pattern(element, name) {
                    return Some(start);
                }
            }
            if let Some(rest) = &arr.rest {
                if let Some(start) = find_identifier_in_pattern(&rest.argument, name) {
                    return Some(start);
                }
            }
            None
        }
        BindingPattern::AssignmentPattern(assign) => find_identifier_in_pattern(&assign.left, name),
    }
}

/// Extract import data for a module binding.
fn get_import_data(
    semantic: &Semantic,
    symbol_id: oxc_syntax::symbol::SymbolId,
) -> Option<ImportBindingData> {
    let decl_node = semantic.symbol_declaration(symbol_id);

    match decl_node.kind() {
        AstKind::ImportDefaultSpecifier(_) => {
            let import_decl = find_import_declaration(semantic, decl_node.id())?;
            Some(ImportBindingData {
                source: import_decl.source.value.to_string(),
                kind: ImportBindingKind::Default,
                imported: None,
            })
        }
        AstKind::ImportNamespaceSpecifier(_) => {
            let import_decl = find_import_declaration(semantic, decl_node.id())?;
            Some(ImportBindingData {
                source: import_decl.source.value.to_string(),
                kind: ImportBindingKind::Namespace,
                imported: None,
            })
        }
        AstKind::ImportSpecifier(spec) => {
            let import_decl = find_import_declaration(semantic, decl_node.id())?;
            let imported_name = match &spec.imported {
                oxc_ast::ast::ModuleExportName::IdentifierName(ident) => ident.name.to_string(),
                oxc_ast::ast::ModuleExportName::IdentifierReference(ident) => {
                    ident.name.to_string()
                }
                oxc_ast::ast::ModuleExportName::StringLiteral(lit) => lit.value.to_string(),
            };
            Some(ImportBindingData {
                source: import_decl.source.value.to_string(),
                kind: ImportBindingKind::Named,
                imported: Some(imported_name),
            })
        }
        _ => None,
    }
}

/// Find the ImportDeclaration node that contains the given import specifier.
fn find_import_declaration<'a>(
    semantic: &'a Semantic,
    specifier_node_id: oxc_semantic::NodeId,
) -> Option<&'a oxc_ast::ast::ImportDeclaration<'a>> {
    let mut current_id = specifier_node_id;
    // Walk up the parent chain (max 10 levels to avoid infinite loop)
    for _ in 0..10 {
        let parent_id = semantic.nodes().parent_id(current_id);
        if parent_id == current_id {
            // Root node, no more parents
            return None;
        }
        let parent_node = semantic.nodes().get_node(parent_id);

        if let AstKind::ImportDeclaration(decl) = parent_node.kind() {
            return Some(decl);
        }

        current_id = parent_id;
    }
    None
}