diff --git a/crates/oxc_transformer/src/helpers/module_imports.rs b/crates/oxc_transformer/src/helpers/module_imports.rs index 8459223c1..6945f6f5f 100644 --- a/crates/oxc_transformer/src/helpers/module_imports.rs +++ b/crates/oxc_transformer/src/helpers/module_imports.rs @@ -3,8 +3,10 @@ use std::cell::{Cell, RefCell}; use indexmap::IndexMap; use oxc_allocator::{Allocator, Vec}; use oxc_ast::{ast::*, AstBuilder}; +use oxc_semantic::ReferenceFlag; use oxc_span::{Atom, SPAN}; use oxc_syntax::symbol::SymbolId; +use oxc_traverse::TraverseCtx; pub struct NamedImport<'a> { imported: Atom<'a>, @@ -72,11 +74,11 @@ impl<'a> ModuleImports<'a> { } } - pub fn get_import_statements(&self) -> Vec<'a, Statement<'a>> { + pub fn get_import_statements(&self, ctx: &mut TraverseCtx<'a>) -> Vec<'a, Statement<'a>> { self.ast.vec_from_iter(self.imports.borrow_mut().drain(..).map(|(import_type, names)| { match import_type.kind { ImportKind::Import => self.get_named_import(import_type.source, names), - ImportKind::Require => self.get_require(import_type.source, names), + ImportKind::Require => self.get_require(import_type.source, names, ctx), } })) } @@ -116,9 +118,14 @@ impl<'a> ModuleImports<'a> { &self, source: Atom<'a>, names: std::vec::Vec>, + ctx: &mut TraverseCtx<'a>, ) -> Statement<'a> { let var_kind = VariableDeclarationKind::Var; - let callee = self.ast.expression_identifier_reference(SPAN, Atom::from("require")); + let symbol_id = ctx.scopes().get_root_binding("require"); + let ident = + ctx.create_reference_id(SPAN, Atom::from("require"), symbol_id, ReferenceFlag::read()); + let callee = self.ast.expression_from_identifier_reference(ident); + let args = { let arg = Argument::from(self.ast.expression_string_literal(SPAN, source)); self.ast.vec1(arg) diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index 438f2f3c2..7dd4c7e02 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -114,7 +114,7 @@ impl<'a> Traverse<'a> for Transformer<'a> { } fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) { - self.x1_react.transform_program_on_exit(program); + self.x1_react.transform_program_on_exit(program, ctx); self.x0_typescript.transform_program_on_exit(program, ctx); } diff --git a/crates/oxc_transformer/src/react/jsx.rs b/crates/oxc_transformer/src/react/jsx.rs index eb112da0e..8741cddb7 100644 --- a/crates/oxc_transformer/src/react/jsx.rs +++ b/crates/oxc_transformer/src/react/jsx.rs @@ -365,8 +365,12 @@ impl<'a> ReactJsx<'a> { } } - pub fn transform_program_on_exit(&mut self, program: &mut Program<'a>) { - self.add_runtime_imports(program); + pub fn transform_program_on_exit( + &mut self, + program: &mut Program<'a>, + ctx: &mut TraverseCtx<'a>, + ) { + self.add_runtime_imports(program, ctx); } pub fn transform_jsx_element( @@ -396,7 +400,7 @@ impl<'a> ReactJsx<'a> { // Add imports impl<'a> ReactJsx<'a> { - pub fn add_runtime_imports(&mut self, program: &mut Program<'a>) { + pub fn add_runtime_imports(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) { if self.bindings.is_classic() { if let Some(stmt) = self.jsx_source.get_var_file_name_statement() { program.body.insert(0, stmt); @@ -404,7 +408,7 @@ impl<'a> ReactJsx<'a> { return; } - let imports = self.ctx.module_imports.get_import_statements(); + let imports = self.ctx.module_imports.get_import_statements(ctx); let mut index = program .body .iter() diff --git a/crates/oxc_transformer/src/react/mod.rs b/crates/oxc_transformer/src/react/mod.rs index abff2fb3c..ab9bd9c10 100644 --- a/crates/oxc_transformer/src/react/mod.rs +++ b/crates/oxc_transformer/src/react/mod.rs @@ -62,9 +62,13 @@ impl<'a> React<'a> { // Transforms impl<'a> React<'a> { - pub fn transform_program_on_exit(&mut self, program: &mut Program<'a>) { + pub fn transform_program_on_exit( + &mut self, + program: &mut Program<'a>, + ctx: &mut TraverseCtx<'a>, + ) { if self.jsx_plugin { - self.jsx.transform_program_on_exit(program); + self.jsx.transform_program_on_exit(program, ctx); } } diff --git a/tasks/transform_conformance/babel.snap.md b/tasks/transform_conformance/babel.snap.md index 683d9a95b..8cbe06083 100644 --- a/tasks/transform_conformance/babel.snap.md +++ b/tasks/transform_conformance/babel.snap.md @@ -1,8 +1,9 @@ commit: 12619ffe -Passed: 418/927 +Passed: 434/927 # All Passed: +* babel-preset-react * babel-plugin-transform-react-display-name * babel-plugin-transform-react-jsx-self * babel-plugin-transform-react-jsx-source @@ -507,27 +508,9 @@ Passed: 418/927 * optimize-const-enums/merged-exported/input.ts * regression/15768/input.ts -# babel-preset-react (6/9) -* preset-options/development-runtime-automatic/input.js -* preset-options/empty-options/input.js -* preset-options/runtime-automatic/input.js - -# babel-plugin-transform-react-jsx (133/142) -* autoImport/after-polyfills-script-not-supported/input.js -* autoImport/auto-import-react-source-type-script/input.js -* autoImport/complicated-scope-script/input.js -* pure/false-default-pragma-automatic-runtime/input.js -* pure/true-default-pragma-automatic-runtime/input.js -* pure/unset-default-pragma-automatic-runtime/input.js +# babel-plugin-transform-react-jsx (141/142) * react-automatic/does-not-add-source-self-automatic/input.mjs -* runtime/defaults-to-automatic/input.js -* runtime/runtime-automatic/input.js -# babel-plugin-transform-react-jsx-development (4/10) -* cross-platform/fragments/input.js -* cross-platform/handle-fragments-with-key/input.js -* cross-platform/handle-nonstatic-children/input.js -* cross-platform/handle-static-children/input.js -* cross-platform/within-derived-classes-constructor/input.js +# babel-plugin-transform-react-jsx-development (9/10) * cross-platform/within-ts-module-block/input.ts