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.
This commit is contained in:
overlookmotel 2024-09-20 01:26:52 +00:00
parent bd7a6535f6
commit 9758c1a5cc
6 changed files with 20 additions and 1 deletions

View file

@ -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>,

View file

@ -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);
}
}

View file

@ -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

View file

@ -0,0 +1,6 @@
var _jsxFileName = "<CWD>/tests/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/input.jsx";
var x = <sometag __source={{
fileName: _jsxFileName,
lineNumber: 1,
columnNumber: 9
}} />;

View file

@ -0,0 +1,3 @@
{
"plugins": ["syntax-jsx", "transform-react-jsx-source"]
}