Skip to content

fix(transformer): validate JSX pragma values and reject invalid identifiers#16675

Merged
Dunqing merged 6 commits intomainfrom
copilot/fix-jsxfrag-transformation
Dec 11, 2025
Merged

fix(transformer): validate JSX pragma values and reject invalid identifiers#16675
Dunqing merged 6 commits intomainfrom
copilot/fix-jsxfrag-transformation

Conversation

Copy link
Contributor

Copilot AI commented Dec 10, 2025

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 @jsx and @jsxFrag pragma values against JavaScript identifier rules

    • Accepts single identifiers: h, Fragment
    • Accepts dotted identifiers: React.Fragment, Preact.h
    • Accepts special forms: this.foo, import.meta.bar
    • Rejects invalid syntax: backticks, numbers, empty strings
  • Diagnostics: Added invalid_pragma_value() warning diagnostic that guides users to fix malformed pragmas

  • Fallback behavior: Invalid pragmas are ignored and defaults (React.createElement, React.Fragment) are used

Example

Before:

// @jsxRuntime classic
console.log(<></>) // @jsxFrag `

// Transforms to invalid code:
console.log(React.createElement(`, null));

After:

// Emits warning: Invalid @jsxFrag value "`". It will be ignored.
// Transforms to valid code:
console.log(React.createElement(React.Fragment, null));

This aligns with esbuild's behavior of warning users about invalid pragmas while continuing transformation with defaults.

Original prompt

This section details on the original issue you should resolve

<issue_title>transformer: // @jsxFrag ` generates invalid code</issue_title>
<issue_description>```js
// @jsxRuntime classic
console.log(<></>) // @jsxFrag `

is transformed to
```js
// @jsxRuntime classic
console.log(/* @__PURE__ */ React.createElement(`, null));

. But this should be

// @jsxRuntime classic
console.log(/* @__PURE__ */ React.createElement(React.Fragment, null));

(the pragma comment should be ignored).
playground

Probably a warning should be output like esbuild does (esbuild try).

related: evanw/esbuild@2102e06</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits December 10, 2025 01:46
- 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>
Copilot AI changed the title [WIP] Fix transformer for invalid code generation with @jsxFrag fix(transformer): validate JSX pragma values and reject invalid identifiers Dec 10, 2025
Copilot AI requested a review from Dunqing December 10, 2025 01:50
@github-actions github-actions bot added A-transformer Area - Transformer / Transpiler C-bug Category - Bug labels Dec 11, 2025
@Dunqing Dunqing marked this pull request as ready for review December 11, 2025 07:16
Copilot AI review requested due to automatic review settings December 11, 2025 07:16
@Dunqing Dunqing force-pushed the copilot/fix-jsxfrag-transformation branch from 25b2c8a to ebc78f5 Compare December 11, 2025 07:17
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 11, 2025

CodSpeed Performance Report

Merging #16675 will not alter performance

Comparing copilot/fix-jsxfrag-transformation (403d91f) with main (3909460)

Summary

✅ 38 untouched
⏩ 7 skipped1

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Dunqing <dengqing0821@gmail.com>
@Dunqing Dunqing merged commit 853c20d into main Dec 11, 2025
27 checks passed
@Dunqing Dunqing deleted the copilot/fix-jsxfrag-transformation branch December 11, 2025 08:30
@sapphi-red
Copy link
Member

sapphi-red commented Dec 11, 2025

@Dunqing Does this allow // @jsxFrag '[' & // @jsxFrag "[", which seems to be the reason why esbuild had a test?

evanw/esbuild@2102e06#diff-77afe5d24e480c64dcd9bc0ae8b6d8849788be2ab5c28442929ce002be9420e2R1186-R1208

@Dunqing
Copy link
Member

Dunqing commented Dec 12, 2025

@Dunqing Does this allow // @jsxFrag '[' & // @jsxFrag "[", which seems to be the reason why esbuild had a test?

evanw/esbuild@2102e06#diff-77afe5d24e480c64dcd9bc0ae8b6d8849788be2ab5c28442929ce002be9420e2R1186-R1208

Oops, never thought it could use a string literal. It doesn't allow it currently. I will follow up.

Copilot AI added a commit that referenced this pull request Dec 13, 2025
Co-authored-by: Dunqing <29533304+Dunqing@users.noreply.github.com>
Dunqing added a commit that referenced this pull request Dec 15, 2025
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - Transformer / Transpiler C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transformer: `// @jsxFrag `` generates invalid code

4 participants