Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(transformer/react): the require IdentifierReference does not have a reference_id #4658

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions crates/oxc_transformer/src/helpers/module_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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),
}
}))
}
Expand Down Expand Up @@ -116,9 +118,14 @@ impl<'a> ModuleImports<'a> {
&self,
source: Atom<'a>,
names: std::vec::Vec<NamedImport<'a>>,
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)
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
12 changes: 8 additions & 4 deletions crates/oxc_transformer/src/react/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -396,15 +400,15 @@ 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);
}
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()
Expand Down
8 changes: 6 additions & 2 deletions crates/oxc_transformer/src/react/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
25 changes: 4 additions & 21 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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