-
Notifications
You must be signed in to change notification settings - Fork 94
fix: handle TsTypeAssertion that conflicts with JSX handling for tarball bundling #7049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
pieh
wants to merge
3
commits into
main
Choose a base branch
from
fix/handle-TsTypeAssertion-when-rewriting-import-assertions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,35 @@ | ||
| import { Parser, Node } from 'acorn' | ||
| import type { ExportAllDeclaration, ExportNamedDeclaration, ImportDeclaration, ImportExpression } from 'acorn' | ||
| import type { | ||
| ExportAllDeclaration, | ||
| ExportNamedDeclaration, | ||
| ImportDeclaration, | ||
| ImportExpression, | ||
| Options as AcornOptions, | ||
| Program, | ||
| } from 'acorn' | ||
| import { tsPlugin } from '@sveltejs/acorn-typescript' | ||
|
|
||
| const acorn = Parser.extend(tsPlugin({ jsx: true })) | ||
| const acornNoJSX = Parser.extend(tsPlugin({ jsx: false })) | ||
| const acornJSX = Parser.extend(tsPlugin({ jsx: true })) | ||
|
|
||
| const parseOptions: AcornOptions = { | ||
| ecmaVersion: 'latest', | ||
| sourceType: 'module', | ||
| locations: true, | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
| } | ||
|
Comment on lines
+21
to
+32
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential alternative I considered is picking parser based on extension (with |
||
|
|
||
| /** | ||
| * Given source code rewrites import assert into import with | ||
|
|
@@ -15,11 +42,7 @@ export function rewriteSourceImportAssertions(source: string): string { | |
| let modified = source | ||
|
|
||
| try { | ||
| const parsedAST = acorn.parse(source, { | ||
| ecmaVersion: 'latest', | ||
| sourceType: 'module', | ||
| locations: true, | ||
| }) | ||
| const parsedAST = parseAST(source) | ||
|
|
||
| const statements = collectImportAssertions(source, parsedAST.body) | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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