Skip to content

Commit 6aaab99

Browse files
committed
fix(transformer/top-level-statements): should not inject statements after non-import statement
1 parent 3541739 commit 6aaab99

File tree

6 files changed

+63
-6
lines changed

6 files changed

+63
-6
lines changed

crates/oxc_transformer/src/common/top_level_statements.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ impl<'a> TopLevelStatementsStore<'a> {
7474
return;
7575
}
7676

77-
// Insert statements after any existing `import` statements
77+
// Insert statements before the first non-import statement.
7878
let index = program
7979
.body
8080
.iter()
81-
.rposition(|stmt| matches!(stmt, Statement::ImportDeclaration(_)))
82-
.map_or(0, |i| i + 1);
81+
.position(|stmt| !matches!(stmt, Statement::ImportDeclaration(_)))
82+
.unwrap_or(program.body.len());
8383

8484
program.body.splice(index..index, stmts.drain(..));
8585
}

tasks/coverage/snapshots/semantic_typescript.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19242,7 +19242,7 @@ rebuilt : []
1924219242
semantic Error: tasks/coverage/typescript/tests/cases/compiler/jsxPartialSpread.tsx
1924319243
Symbol reference IDs mismatch for "Select":
1924419244
after transform: SymbolId(0): [ReferenceId(2), ReferenceId(3)]
19245-
rebuilt : SymbolId(0): [ReferenceId(3)]
19245+
rebuilt : SymbolId(3): [ReferenceId(3)]
1924619246
Unresolved references mismatch:
1924719247
after transform: ["Parameters", "Partial"]
1924819248
rebuilt : []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// The reason to override this test case:
2+
// We generate the import statements before the first non-import statement,
3+
// while the original test case expects them to be after all import statements.
4+
5+
import { jsx as _jsx } from "react/jsx-runtime";
6+
ReactDOM.render(
7+
/* @__PURE__ */ _jsx("p", { children: "Hello, World!" }),
8+
document.getElementById("root"),
9+
);
10+
import "react-app-polyfill/ie11";
11+
import "react-app-polyfill/stable";
12+
import ReactDOM from "react-dom";

tasks/transform_conformance/snapshots/oxc.snap.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
commit: 1d4546bc
22

3-
Passed: 180/299
3+
Passed: 180/300
44

55
# All Passed:
66
* babel-plugin-transform-class-static-block
@@ -502,7 +502,17 @@ after transform: [ReferenceId(0), ReferenceId(1), ReferenceId(4), ReferenceId(9)
502502
rebuilt : [ReferenceId(5)]
503503

504504

505-
# babel-plugin-transform-react-jsx (44/46)
505+
# babel-plugin-transform-react-jsx (44/47)
506+
* refresh/import-after-component/input.js
507+
Missing ScopeId
508+
Missing ReferenceId: "useFoo"
509+
Scope children mismatch:
510+
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
511+
rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3)]
512+
Symbol reference IDs mismatch for "useFoo":
513+
after transform: SymbolId(1): [ReferenceId(1), ReferenceId(7)]
514+
rebuilt : SymbolId(1): [ReferenceId(6), ReferenceId(11), ReferenceId(12)]
515+
506516
* refresh/react-refresh/includes-custom-hooks-into-the-signatures-when-commonjs-target-is-used/input.jsx
507517
x Output mismatch
508518

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { observer } from "mobx-react-lite";
2+
import { useFoo } from "./useFoo";
3+
4+
export const BazComponent = observer(function BazComponent_() {
5+
const foo = useFoo();
6+
return (
7+
<>
8+
{foo}
9+
{bar}
10+
</>
11+
);
12+
});
13+
14+
import { bar } from "./bar";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { observer } from "mobx-react-lite";
2+
import { useFoo } from "./useFoo";
3+
import { Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
4+
var _s = $RefreshSig$();
5+
6+
export const BazComponent = _s(observer(_c = _s(function BazComponent_() {
7+
_s();
8+
const foo = useFoo();
9+
return /* @__PURE__ */ _jsxs(_Fragment, { children: [foo, bar] });
10+
}, "useFoo{foo}", false, function() {
11+
return [useFoo];
12+
})), "useFoo{foo}", false, function() {
13+
return [useFoo];
14+
});
15+
_c2 = BazComponent;
16+
17+
import { bar } from "./bar";
18+
19+
var _c, _c2;
20+
$RefreshReg$(_c, "BazComponent$observer");
21+
$RefreshReg$(_c2, "BazComponent");

0 commit comments

Comments
 (0)