Skip to content

Commit e89ec9a

Browse files
CopilotDunqing
andcommitted
Revert PR #16675: remove JSX pragma validation
Co-authored-by: Dunqing <[email protected]>
1 parent 75017b5 commit e89ec9a

File tree

7 files changed

+13
-98
lines changed

7 files changed

+13
-98
lines changed

crates/oxc_transformer/src/jsx/comments.rs

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ use std::borrow::Cow;
33
use memchr::memchr;
44

55
use oxc_ast::Comment;
6-
use oxc_syntax::{identifier::is_identifier_name, keyword::is_reserved_keyword};
76

87
use crate::{JsxOptions, JsxRuntime, TransformCtx, TypeScriptOptions};
98

10-
use super::diagnostics;
11-
129
/// Scan through all comments and find the following pragmas:
1310
///
1411
/// * @jsx Preact.h
@@ -31,7 +28,7 @@ pub fn update_options_with_comments(
3128
) {
3229
let source_text = ctx.source_text;
3330
for comment in comments {
34-
update_options_with_comment(typescript, jsx, comment, source_text, ctx);
31+
update_options_with_comment(typescript, jsx, comment, source_text);
3532
}
3633
}
3734

@@ -40,24 +37,19 @@ fn update_options_with_comment(
4037
jsx: &mut JsxOptions,
4138
comment: &Comment,
4239
source_text: &str,
43-
ctx: &TransformCtx,
4440
) {
4541
let mut comment_str = comment.content_span().source_text(source_text);
4642

4743
while let Some((keyword, value, remainder)) = find_jsx_pragma(comment_str) {
4844
match keyword {
4945
// @jsx
5046
PragmaType::Jsx => {
51-
if is_valid_pragma_value(value) {
52-
// Don't set React option unless React transform is enabled
53-
// otherwise can cause error in `ReactJsx::new`
54-
if jsx.jsx_plugin || jsx.development {
55-
jsx.pragma = Some(value.to_string());
56-
}
57-
typescript.jsx_pragma = Cow::Owned(value.to_string());
58-
} else {
59-
ctx.error(diagnostics::invalid_pragma_value("jsx", value));
47+
// Don't set React option unless React transform is enabled
48+
// otherwise can cause error in `ReactJsx::new`
49+
if jsx.jsx_plugin || jsx.development {
50+
jsx.pragma = Some(value.to_string());
6051
}
52+
typescript.jsx_pragma = Cow::Owned(value.to_string());
6153
}
6254
// @jsxRuntime
6355
PragmaType::JsxRuntime => match value {
@@ -71,16 +63,12 @@ fn update_options_with_comment(
7163
}
7264
// @jsxFrag
7365
PragmaType::JsxFrag => {
74-
if is_valid_pragma_value(value) {
75-
// Don't set React option unless React transform is enabled
76-
// otherwise can cause error in `ReactJsx::new`
77-
if jsx.jsx_plugin || jsx.development {
78-
jsx.pragma_frag = Some(value.to_string());
79-
}
80-
typescript.jsx_pragma_frag = Cow::Owned(value.to_string());
81-
} else {
82-
ctx.error(diagnostics::invalid_pragma_value("jsxFrag", value));
66+
// Don't set React option unless React transform is enabled
67+
// otherwise can cause error in `ReactJsx::new`
68+
if jsx.jsx_plugin || jsx.development {
69+
jsx.pragma_frag = Some(value.to_string());
8370
}
71+
typescript.jsx_pragma_frag = Cow::Owned(value.to_string());
8472
}
8573
}
8674

@@ -89,46 +77,6 @@ fn update_options_with_comment(
8977
}
9078
}
9179

92-
/// Check if a pragma value is valid.
93-
///
94-
/// A pragma value is invalid if any of the following are true:
95-
/// - It is an empty string
96-
/// - The root part is a reserved keyword (except `this`, `null`, `true`, `false`, `import.meta`)
97-
/// - Any part is not a valid identifier name
98-
///
99-
/// Based on <https://github.com/evanw/esbuild/blob/5e0e56d6d62076dfeff47f5227ae5300f91d2b16/internal/js_parser/js_parser.go#L18027-L18060>
100-
fn is_valid_pragma_value(value: &str) -> bool {
101-
if value.is_empty() {
102-
return false;
103-
}
104-
105-
let mut parts = value.split('.');
106-
107-
let Some(root) = parts.next() else {
108-
unreachable!();
109-
};
110-
111-
match root {
112-
// Allow `this`, `null`, `true`, `false` keywords
113-
"this" | "null" | "true" | "false" => {}
114-
// Allow `import.meta`
115-
"import" => {
116-
if !matches!(parts.next(), Some("meta")) {
117-
return false;
118-
}
119-
}
120-
// Otherwise, it must be a valid identifier and not a reserved keyword
121-
_ => {
122-
if is_reserved_keyword(root) || !is_identifier_name(root) {
123-
return false;
124-
}
125-
}
126-
}
127-
128-
// All remaining parts must be valid identifiers
129-
parts.all(is_identifier_name)
130-
}
131-
13280
/// Type of JSX pragma directive.
13381
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
13482
enum PragmaType {

crates/oxc_transformer/src/jsx/diagnostics.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,3 @@ pub fn valueless_key(span: Span) -> OxcDiagnostic {
3030
OxcDiagnostic::warn("Please provide an explicit key value. Using \"key\" as a shorthand for \"key={true}\" is not allowed.")
3131
.with_label(span)
3232
}
33-
34-
pub fn invalid_pragma_value(pragma_name: &str, value: &str) -> OxcDiagnostic {
35-
OxcDiagnostic::warn(format!(
36-
"Invalid @{pragma_name} value \"{value}\". It will be ignored."
37-
))
38-
.with_help(format!(
39-
"@{pragma_name} must be a valid JavaScript identifier or a dotted sequence of identifiers"
40-
))
41-
}

tasks/transform_conformance/snapshots/oxc.snap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
commit: 3591b24e
22

3-
Passed: 203/334
3+
Passed: 201/332
44

55
# All Passed:
66
* babel-plugin-transform-class-static-block
@@ -573,7 +573,7 @@ after transform: [ReferenceId(0), ReferenceId(1), ReferenceId(4), ReferenceId(9)
573573
rebuilt : [ReferenceId(5)]
574574

575575

576-
# babel-plugin-transform-react-jsx (47/50)
576+
# babel-plugin-transform-react-jsx (45/48)
577577
* refresh/import-after-component/input.js
578578
Missing ScopeId
579579
Missing ReferenceId: "useFoo"

tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx-frag/input.jsx

Lines changed: 0 additions & 1 deletion
This file was deleted.

tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx-frag/options.json

Lines changed: 0 additions & 11 deletions
This file was deleted.

tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx/input.jsx

Lines changed: 0 additions & 1 deletion
This file was deleted.

tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx/options.json

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)