From 9758c1a5cc8619e9ff352685368159384cfbbd2e Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Fri, 20 Sep 2024 01:26:52 +0000 Subject: [PATCH] fix(transformer): JSX source: add `var _jsxFileName` statement (#5894) Fix JSX source transform when run alone without main JSX transform. The added test case is same as one of Babel's exec test cases, but the exec test fails due to `transformAsync` not being present. --- crates/oxc_transformer/src/react/jsx_source.rs | 6 ++++++ crates/oxc_transformer/src/react/mod.rs | 2 ++ tasks/transform_conformance/oxc.snap.md | 3 ++- .../test/fixtures/react-source/basic-sample/input.jsx | 1 + .../test/fixtures/react-source/basic-sample/output.jsx | 6 ++++++ .../test/fixtures/react-source/options.json | 3 +++ 6 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tasks/transform_conformance/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/input.jsx create mode 100644 tasks/transform_conformance/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/output.jsx create mode 100644 tasks/transform_conformance/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/options.json diff --git a/crates/oxc_transformer/src/react/jsx_source.rs b/crates/oxc_transformer/src/react/jsx_source.rs index 5ba0a8f0f..29a64d3d5 100644 --- a/crates/oxc_transformer/src/react/jsx_source.rs +++ b/crates/oxc_transformer/src/react/jsx_source.rs @@ -59,6 +59,12 @@ impl<'a> ReactJsxSource<'a> { } impl<'a> Traverse<'a> for ReactJsxSource<'a> { + fn exit_program(&mut self, program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) { + if let Some(stmt) = self.get_var_file_name_statement() { + program.body.insert(0, stmt); + } + } + fn enter_jsx_opening_element( &mut self, elem: &mut JSXOpeningElement<'a>, diff --git a/crates/oxc_transformer/src/react/mod.rs b/crates/oxc_transformer/src/react/mod.rs index c9523b6ad..9984c5525 100644 --- a/crates/oxc_transformer/src/react/mod.rs +++ b/crates/oxc_transformer/src/react/mod.rs @@ -84,6 +84,8 @@ impl<'a> Traverse<'a> for React<'a> { } if self.jsx_plugin { self.jsx.exit_program(program, ctx); + } else if self.jsx_source_plugin { + self.jsx.jsx_source.exit_program(program, ctx); } } diff --git a/tasks/transform_conformance/oxc.snap.md b/tasks/transform_conformance/oxc.snap.md index 373fd2467..8b6c759e1 100644 --- a/tasks/transform_conformance/oxc.snap.md +++ b/tasks/transform_conformance/oxc.snap.md @@ -1,12 +1,13 @@ commit: 3bcfee23 -Passed: 46/56 +Passed: 47/57 # All Passed: * babel-plugin-transform-nullish-coalescing-operator * babel-plugin-transform-optional-catch-binding * babel-plugin-transform-arrow-functions * babel-preset-typescript +* babel-plugin-transform-react-jsx-source * regexp diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/input.jsx b/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/input.jsx new file mode 100644 index 000000000..51fee284e --- /dev/null +++ b/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/input.jsx @@ -0,0 +1 @@ +var x = ; diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/output.jsx b/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/output.jsx new file mode 100644 index 000000000..9b04721c9 --- /dev/null +++ b/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/output.jsx @@ -0,0 +1,6 @@ +var _jsxFileName = "/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/input.jsx"; +var x = ; diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/options.json b/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/options.json new file mode 100644 index 000000000..7c7160027 --- /dev/null +++ b/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["syntax-jsx", "transform-react-jsx-source"] +}