fix(transformer/react_jsx): add imports to the top body (#1087)

This commit is contained in:
Boshen 2023-10-29 15:05:52 +08:00 committed by GitHub
parent cef78accf0
commit 1ad2dca45a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 7 deletions

View file

@ -103,13 +103,14 @@ impl<'a> VisitMut<'a> for Transformer<'a> {
self.typescript.as_mut().map(|t| t.transform_program(program));
self.visit_statements(&mut program.body);
self.react_jsx.as_mut().map(|t| t.add_react_jsx_runtime_imports(program));
}
fn visit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>) {
for stmt in stmts.iter_mut() {
self.visit_statement(stmt);
}
self.react_jsx.as_mut().map(|t| t.add_react_jsx_runtime_import(stmts));
// TODO: we need scope id to insert the vars into the correct statements
self.es2021_logical_assignment_operators.as_mut().map(|t| t.add_vars_to_statements(stmts));
self.es2020_nullish_coalescing_operators.as_mut().map(|t| t.add_vars_to_statements(stmts));

View file

@ -88,16 +88,17 @@ impl<'a> ReactJsx<'a> {
}
}
pub fn add_react_jsx_runtime_import(&mut self, stmts: &mut Vec<'a, Statement<'a>>) {
pub fn add_react_jsx_runtime_imports(&mut self, program: &mut Program<'a>) {
if self.options.runtime.is_classic() {
return;
}
let imports = self.ast.move_statement_vec(&mut self.imports);
let index = stmts
let index = program
.body
.iter()
.rposition(|stmt| matches!(stmt, Statement::ModuleDeclaration(m) if m.is_import()))
.map_or(0, |i| i + 1);
stmts.splice(index..index, imports);
program.body.splice(index..index, imports);
}
fn add_import<'b>(

View file

@ -1,4 +1,4 @@
Passed: 229/1083
Passed: 230/1083
# All Passed:
* babel-plugin-transform-numeric-separator
@ -804,7 +804,7 @@ Passed: 229/1083
* regression/11061/input.mjs
* variable-declaration/non-null-in-optional-chain/input.ts
# babel-plugin-transform-react-jsx (80/172)
# babel-plugin-transform-react-jsx (81/172)
* autoImport/after-polyfills-compiled-to-cjs/input.mjs
* autoImport/after-polyfills-script-not-supported/input.js
* autoImport/auto-import-react-source-type-module/input.js
@ -882,7 +882,6 @@ Passed: 229/1083
* react-automatic/should-throw-error-namespaces-if-not-flag/input.js
* react-automatic/should-throw-when-filter-is-specified/input.js
* react-automatic/should-warn-when-pragma-or-pragmaFrag-is-set/input.js
* react-automatic/weird-symbols/input.js
* regression/issue-12478-automatic/input.js
* regression/issue-12478-classic/input.js
* regression/issue-15353-classic/input.js