Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 11 additions & 63 deletions crates/oxc_transformer/src/jsx/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

Expand All @@ -40,24 +37,19 @@ 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);

while let Some((keyword, value, remainder)) = find_jsx_pragma(comment_str) {
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 {
Expand All @@ -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());
}
}

Expand All @@ -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 <https://github.com/evanw/esbuild/blob/5e0e56d6d62076dfeff47f5227ae5300f91d2b16/internal/js_parser/js_parser.go#L18027-L18060>
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 {
Expand Down
9 changes: 0 additions & 9 deletions crates/oxc_transformer/src/jsx/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
))
}
4 changes: 2 additions & 2 deletions tasks/transform_conformance/snapshots/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
commit: 3591b24e

Passed: 203/334
Passed: 201/332

# All Passed:
* babel-plugin-transform-class-static-block
Expand Down Expand Up @@ -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"
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading