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 committed 2024-09-20 01:26:52 +00:00
1 parent bd7a6535f6
commit 9758c1a5cc
6 files changed
+20 -1

No files matched your search

@@ -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>,
+2
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);
}
}
+2 -1
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
@@ -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
}} />;
@@ -0,0 +1,3 @@
{
"plugins": ["syntax-jsx", "transform-react-jsx-source"]
}