Skip to content

fix: handle TsTypeAssertion that conflicts with JSX handling for tarball bundling#7049

Draft
pieh wants to merge 3 commits into
mainfrom
fix/handle-TsTypeAssertion-when-rewriting-import-assertions
Draft

fix: handle TsTypeAssertion that conflicts with JSX handling for tarball bundling#7049
pieh wants to merge 3 commits into
mainfrom
fix/handle-TsTypeAssertion-when-rewriting-import-assertions

Conversation

@pieh
Copy link
Copy Markdown
Contributor

@pieh pieh commented May 8, 2026

🎉 Thanks for submitting a pull request! 🎉

Summary

Fixes #<replace_with_issue_number>


For us to review and ship your PR efficiently, please perform the following steps:

  • Open a bug/issue before writing your code 🧑‍💻. This ensures
    we can discuss the changes and get feedback from everyone that should be involved. If you`re fixing a typo or
    something that`s on fire 🔥 (e.g. incident related), you can skip this step.
  • Read the contribution guidelines 📖. This ensures
    your code follows our style guide and passes our tests.
  • Update or add tests (if any source code was changed or added) 🧪
  • Update or add documentation (if features were changed or added) 📝
  • Make sure the status checks below are successful ✅

A picture of a cute animal (not mandatory, but encouraged)

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 8, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e79ca680-edc1-427f-bbd0-71e3e3064ce4

📥 Commits

Reviewing files that changed from the base of the PR and between 07a4565 and 052591b.

📒 Files selected for processing (2)
  • packages/edge-bundler/node/utils/import_attributes.test.ts
  • packages/edge-bundler/node/utils/import_attributes.ts

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Improved import assertion rewriting to handle edge cases when TypeScript and JSX syntax appear together in the same file.
  • Tests

    • Added test coverage for import assertion rewriting edge cases.

Walkthrough

This PR refactors AST parsing in rewriteSourceImportAssertions to handle edge cases where TypeScript type syntax is misparsed as JSX. The change introduces a parseAST helper that attempts parsing with JSX enabled first, then retries with JSX disabled if a SyntaxError occurs. The function signature remains unchanged. A new test validates correct import assertion rewriting (assertwith) when TS type assertions appear alongside static import assertions.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is the unmodified PR template with no actual pull request details filled in—the issue number placeholder, motivation, and animal picture are all unfilled. Fill in the specific issue number, explain the motivation and problem being solved, and provide actual PR context instead of the bare template.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing TsTypeAssertion handling that conflicts with JSX during tarball bundling, which aligns with the code changes adding JSX-aware parsing logic.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/handle-TsTypeAssertion-when-rewriting-import-assertions

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

// We want to make sure we can handle this case and still rewrite the import assertions correctly.
const source = `
import data3 from './data.json' assert { type: 'json' };
const params = <Params> inputs;
Copy link
Copy Markdown
Contributor Author

@pieh pieh May 8, 2026

Choose a reason for hiding this comment

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

This is valid and documented syntax - https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions - it just conflicts in JSX mode because angled brackets become ambiguous so parsers intentionally disable handling of this syntax for type casting in JSX mode

@pieh pieh marked this pull request as ready for review May 8, 2026 11:39
@pieh pieh requested a review from a team as a code owner May 8, 2026 11:39
Comment on lines +21 to +32
const parseAST = (source: string): Program => {
try {
return acornJSX.parse(source, parseOptions)
} catch (error) {
// for non-jsx typescript casting to type via "<type> value" (normally done with "value as type") will throw an "Unexpected token" error in acorn-jsx,
// but is valid syntax in TypeScript. In this case, we can retry parsing with the non-jsx parser.
if (error instanceof SyntaxError) {
return acornNoJSX.parse(source, parseOptions)
}
throw error
}
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Potential alternative I considered is picking parser based on extension (with .ts specifically using NoJSX mode), but fallback felt like more sure thing that is potentially not susceptible to wild and unexpected configurations

@pieh pieh marked this pull request as draft May 13, 2026 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant