fix(transformer): validate JSX pragma values and reject invalid identifiers#16675
fix(transformer): validate JSX pragma values and reject invalid identifiers#16675
Conversation
- Validate that @jsx and @jsxFrag pragma values are valid JavaScript identifiers - Emit warning when invalid pragma values like backticks are found - Fall back to defaults (React.createElement/React.Fragment) when invalid - Add comprehensive unit tests for pragma validation Co-authored-by: Dunqing <29533304+Dunqing@users.noreply.github.com>
Use iterator methods and string prefix checks instead of collecting into Vec for better performance Co-authored-by: Dunqing <29533304+Dunqing@users.noreply.github.com>
25b2c8a to
ebc78f5
Compare
CodSpeed Performance ReportMerging #16675 will not alter performanceComparing Summary
Footnotes
|
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where invalid JSX pragma values (like backticks, numbers, or bare reserved keywords) were being used as-is in the transformer, generating broken JavaScript code. The fix adds validation for @jsx and @jsxFrag pragma values and emits warnings when they're invalid, falling back to default values (React.createElement and React.Fragment).
Key Changes:
- Added
is_valid_pragma_value()validation function that checks pragma values against JavaScript identifier rules - Added
invalid_pragma_value()diagnostic to warn users about malformed pragmas - Integration tests verify that invalid pragmas (numbers, reserved keywords) are properly rejected
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
crates/oxc_transformer/src/jsx/diagnostics.rs |
Adds new warning diagnostic for invalid pragma values with helpful message |
crates/oxc_transformer/src/jsx/comments.rs |
Implements validation logic for pragma values, rejecting invalid identifiers while allowing special forms like this, null, true, false, and import.meta |
tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx/input.jsx |
Test fixture for invalid @jsx pragma (numeric value) |
tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx/options.json |
Test configuration expecting warning for invalid @jsx value |
tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx-frag/input.jsx |
Test fixture for invalid @jsxFrag pragma (reserved keyword) |
tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx-frag/options.json |
Test configuration expecting warning for invalid @jsxFrag value |
tasks/transform_conformance/snapshots/oxc.snap.md |
Updated snapshot showing 2 new passing tests (203/334 vs 201/332) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
..._conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/invalid-jsx-frag/input.jsx
Show resolved
Hide resolved
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Dunqing <dengqing0821@gmail.com>
|
@Dunqing Does this allow |
Oops, never thought it could use a string literal. It doesn't allow it currently. I will follow up. |
Co-authored-by: Dunqing <29533304+Dunqing@users.noreply.github.com>
…s and reject invalid identifiers" (#16793) Reverts commit 853c20d which added JSX pragma validation. The validation rejected invalid pragma values (backticks, numbers, keywords) and emitted warnings while falling back to defaults. ## Changes - **`crates/oxc_transformer/src/jsx/comments.rs`**: Removed `is_valid_pragma_value()` function and validation checks for `@jsx` and `@jsxFrag` pragmas. Invalid pragma values are now accepted without validation. - **`crates/oxc_transformer/src/jsx/diagnostics.rs`**: Removed `invalid_pragma_value()` diagnostic. - **Test fixtures**: Removed `invalid-jsx` and `invalid-jsx-frag` test cases. ## Behavior Invalid pragma values are now used as-is in transformation: ```jsx // @jsxRuntime classic console.log(<></>) // @jsxFrag ` ``` Transforms to invalid code: ```js console.log(React.createElement(`, null)); ``` <!-- START COPILOT CODING AGENT SUFFIX --> <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > Revert #16675 </details> <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/oxc-project/oxc/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Dunqing <29533304+Dunqing@users.noreply.github.com>
Invalid JSX pragma values in comments (e.g., backticks) were being used as-is, generating broken JavaScript code. The transformer now validates pragma values and emits warnings when they're invalid.
Changes
Validation: Added
is_valid_pragma_value()to validate@jsxand@jsxFragpragma values against JavaScript identifier rulesh,FragmentReact.Fragment,Preact.hthis.foo,import.meta.barDiagnostics: Added
invalid_pragma_value()warning diagnostic that guides users to fix malformed pragmasFallback behavior: Invalid pragmas are ignored and defaults (
React.createElement,React.Fragment) are usedExample
Before:
After:
This aligns with esbuild's behavior of warning users about invalid pragmas while continuing transformation with defaults.
Original prompt
// @jsxFrag`` generates invalid code #16653✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.