mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
fix(transformer/react): the require IdentifierReference does not have a reference_id (#4658)
part of #4581. Looks we had to pass `TraverseCtx` to the `ModuleImports`
This commit is contained in:
parent
09d982224f
commit
5327acdc1f
5 changed files with 29 additions and 31 deletions
|
|
@ -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<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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue