From 39e608734f5c590554aed94e07795cfb20a077b9 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Sun, 12 Nov 2023 11:17:23 +0800 Subject: [PATCH] feat(transformer/react-jsx): support for throwing ImportSourceCannotBeSet error (#1224) --- crates/oxc_transformer/src/react_jsx/mod.rs | 8 ++++++++ tasks/transform_conformance/babel.snap.md | 6 ++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/oxc_transformer/src/react_jsx/mod.rs b/crates/oxc_transformer/src/react_jsx/mod.rs index 86447172f..3dc2455b4 100644 --- a/crates/oxc_transformer/src/react_jsx/mod.rs +++ b/crates/oxc_transformer/src/react_jsx/mod.rs @@ -22,6 +22,11 @@ use crate::context::TransformerCtx; #[diagnostic(severity(warning), help("Remove `pragma` and `pragmaFrag` options."))] struct PragmaAndPragmaFragCannotBeSet; +#[derive(Debug, Error, Diagnostic)] +#[error("importSource cannot be set when runtime is classic.")] +#[diagnostic(severity(warning), help("Remove `importSource` option."))] +struct ImportSourceCannotBeSet; + #[derive(Debug, Error, Diagnostic)] #[error("Namespace tags are not supported by default. React's JSX doesn't support namespace tags. You can set `throwIfNamespace: false` to bypass this warning.")] #[diagnostic(severity(warning))] @@ -129,6 +134,9 @@ impl<'a> ReactJsx<'a> { pub fn add_react_jsx_runtime_imports(&mut self, program: &mut Program<'a>) { if self.options.runtime.is_classic() { + if self.options.import_source != "react" { + self.ctx.error(ImportSourceCannotBeSet); + } return; } diff --git a/tasks/transform_conformance/babel.snap.md b/tasks/transform_conformance/babel.snap.md index e1a0844ad..038826d3f 100644 --- a/tasks/transform_conformance/babel.snap.md +++ b/tasks/transform_conformance/babel.snap.md @@ -1,4 +1,4 @@ -Passed: 273/1113 +Passed: 275/1113 # All Passed: * babel-plugin-transform-numeric-separator @@ -852,7 +852,7 @@ Passed: 273/1113 * regression/11061/input.mjs * variable-declaration/non-null-in-optional-chain/input.ts -# babel-plugin-transform-react-jsx (138/170) +# babel-plugin-transform-react-jsx (140/170) * autoImport/after-polyfills-compiled-to-cjs/input.mjs * autoImport/complicated-scope-module/input.js * react/adds-appropriate-newlines-when-using-spread-attribute-babel-7/input.js @@ -864,8 +864,6 @@ Passed: 273/1113 * react/optimisation.react.constant-elements/input.js * react/should-add-quotes-es3/input.js * react/should-disallow-spread-children/input.js -* react/should-warn-when-importSource-is-set/input.js -* react/should-warn-when-importSource-pragma-is-set/input.js * react/wraps-props-in-react-spread-for-first-spread-attributes-babel-7/input.js * react/wraps-props-in-react-spread-for-last-spread-attributes-babel-7/input.js * react/wraps-props-in-react-spread-for-middle-spread-attributes-babel-7/input.js