diff --git a/crates/oxc_transformer/src/jsx/comments.rs b/crates/oxc_transformer/src/jsx/comments.rs index 10119f951582e..0959005f83d20 100644 --- a/crates/oxc_transformer/src/jsx/comments.rs +++ b/crates/oxc_transformer/src/jsx/comments.rs @@ -3,12 +3,9 @@ use std::borrow::Cow; use memchr::memchr; use oxc_ast::Comment; -use oxc_syntax::{identifier::is_identifier_name, keyword::is_reserved_keyword}; use crate::{JsxOptions, JsxRuntime, TransformCtx, TypeScriptOptions}; -use super::diagnostics; - /// Scan through all comments and find the following pragmas: /// /// * @jsx Preact.h @@ -31,7 +28,7 @@ pub fn update_options_with_comments( ) { let source_text = ctx.source_text; for comment in comments { - update_options_with_comment(typescript, jsx, comment, source_text, ctx); + update_options_with_comment(typescript, jsx, comment, source_text); } } @@ -40,7 +37,6 @@ fn update_options_with_comment( jsx: &mut JsxOptions, comment: &Comment, source_text: &str, - ctx: &TransformCtx, ) { let mut comment_str = comment.content_span().source_text(source_text); @@ -48,16 +44,12 @@ fn update_options_with_comment( match keyword { // @jsx PragmaType::Jsx => { - if is_valid_pragma_value(value) { - // Don't set React option unless React transform is enabled - // otherwise can cause error in `ReactJsx::new` - if jsx.jsx_plugin || jsx.development { - jsx.pragma = Some(value.to_string()); - } - typescript.jsx_pragma = Cow::Owned(value.to_string()); - } else { - ctx.error(diagnostics::invalid_pragma_value("jsx", value)); + // Don't set React option unless React transform is enabled + // otherwise can cause error in `ReactJsx::new` + if jsx.jsx_plugin || jsx.development { + jsx.pragma = Some(value.to_string()); } + typescript.jsx_pragma = Cow::Owned(value.to_string()); } // @jsxRuntime PragmaType::JsxRuntime => match value { @@ -71,16 +63,12 @@ fn update_options_with_comment( } // @jsxFrag PragmaType::JsxFrag => { - if is_valid_pragma_value(value) { - // Don't set React option unless React transform is enabled - // otherwise can cause error in `ReactJsx::new` - if jsx.jsx_plugin || jsx.development { - jsx.pragma_frag = Some(value.to_string()); - } - typescript.jsx_pragma_frag = Cow::Owned(value.to_string()); - } else { - ctx.error(diagnostics::invalid_pragma_value("jsxFrag", value)); + // Don't set React option unless React transform is enabled + // otherwise can cause error in `ReactJsx::new` + if jsx.jsx_plugin || jsx.development { + jsx.pragma_frag = Some(value.to_string()); } + typescript.jsx_pragma_frag = Cow::Owned(value.to_string()); } } @@ -89,46 +77,6 @@ fn update_options_with_comment( } } -/// Check if a pragma value is valid. -/// -/// A pragma value is invalid if any of the following are true: -/// - It is an empty string -/// - The root part is a reserved keyword (except `this`, `null`, `true`, `false`, `import.meta`) -/// - Any part is not a valid identifier name -/// -/// Based on -fn is_valid_pragma_value(value: &str) -> bool { - if value.is_empty() { - return false; - } - - let mut parts = value.split('.'); - - let Some(root) = parts.next() else { - unreachable!(); - }; - - match root { - // Allow `this`, `null`, `true`, `false` keywords - "this" | "null" | "true" | "false" => {} - // Allow `import.meta` - "import" => { - if !matches!(parts.next(), Some("meta")) { - return false; - } - } - // Otherwise, it must be a valid identifier and not a reserved keyword - _ => { - if is_reserved_keyword(root) || !is_identifier_name(root) { - return false; - } - } - } - - // All remaining parts must be valid identifiers - parts.all(is_identifier_name) -} - /// Type of JSX pragma directive. #[derive(Clone, Copy, PartialEq, Eq, Debug)] enum PragmaType { diff --git a/crates/oxc_transformer/src/jsx/diagnostics.rs b/crates/oxc_transformer/src/jsx/diagnostics.rs index 38861ca719eb5..cfc896ca8d3ce 100644 --- a/crates/oxc_transformer/src/jsx/diagnostics.rs +++ b/crates/oxc_transformer/src/jsx/diagnostics.rs @@ -30,12 +30,3 @@ pub fn valueless_key(span: Span) -> OxcDiagnostic { OxcDiagnostic::warn("Please provide an explicit key value. Using \"key\" as a shorthand for \"key={true}\" is not allowed.") .with_label(span) } - -pub fn invalid_pragma_value(pragma_name: &str, value: &str) -> OxcDiagnostic { - OxcDiagnostic::warn(format!( - "Invalid @{pragma_name} value \"{value}\". It will be ignored." - )) - .with_help(format!( - "@{pragma_name} must be a valid JavaScript identifier or a dotted sequence of identifiers" - )) -} diff --git a/tasks/transform_conformance/snapshots/oxc.snap.md b/tasks/transform_conformance/snapshots/oxc.snap.md index bc0bc756c273d..88b69e4579fc8 100644 --- a/tasks/transform_conformance/snapshots/oxc.snap.md +++ b/tasks/transform_conformance/snapshots/oxc.snap.md @@ -1,6 +1,6 @@ commit: 3591b24e -Passed: 203/334 +Passed: 201/332 # All Passed: * babel-plugin-transform-class-static-block @@ -573,7 +573,7 @@ after transform: [ReferenceId(0), ReferenceId(1), ReferenceId(4), ReferenceId(9) rebuilt : [ReferenceId(5)] -# babel-plugin-transform-react-jsx (47/50) +# babel-plugin-transform-react-jsx (45/48) * refresh/import-after-component/input.js Missing ScopeId Missing ReferenceId: "useFoo" diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx-frag/input.jsx b/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx-frag/input.jsx deleted file mode 100644 index 8c183b316a1b1..0000000000000 --- a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx-frag/input.jsx +++ /dev/null @@ -1 +0,0 @@ -console.log(<>) // @jsxFrag import diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx-frag/options.json b/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx-frag/options.json deleted file mode 100644 index b5d7905b474b4..0000000000000 --- a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx-frag/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "transform-react-jsx", - { - "runtime": "classic" - } - ] - ], - "throws": "Invalid @jsxFrag value \"import\". It will be ignored" -} diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx/input.jsx b/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx/input.jsx deleted file mode 100644 index 592fce5c79348..0000000000000 --- a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx/input.jsx +++ /dev/null @@ -1 +0,0 @@ -console.log(<>) // @jsx 1 diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx/options.json b/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx/options.json deleted file mode 100644 index 73f56c71fb576..0000000000000 --- a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "transform-react-jsx", - { - "runtime": "classic" - } - ] - ], - "throws": "Invalid @jsx value \"1\". It will be ignored" -}