Skip to content
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

Fail to parse typescript type assertion syntax #68

Closed
gregoryzh opened this issue May 13, 2021 · 7 comments
Closed

Fail to parse typescript type assertion syntax #68

gregoryzh opened this issue May 13, 2021 · 7 comments
Labels
good first issue Good for newcomers

Comments

@gregoryzh
Copy link

Hi, i think the parser fails at angular bracket type assertion syntax, is this an known issue?
image

@ehaynes99
Copy link
Contributor

The error thrown is SyntaxError: Unterminated JSX contents during @babel/parser here:

return [parser.parse(_data, config), _data];

@connectdotz
Copy link
Collaborator

yes, this is a known issue, fix is not hard: we are enabling "jsx" babel plugin for all file types, what we should do is only enable "jsx" for react file types.

It's a relatively simple change if anybody is interested. You can start from here

@gregoryzh
Copy link
Author

yes, this is a known issue, fix is not hard: we are enabling "jsx" babel plugin for all file types, what we should do is only enable "jsx" for react file types.

It's a relatively simple change if anybody is interested. You can start from here

hi, created PR
#72

@ehaynes99
Copy link
Contributor

JSX is not specific to React; it's used by many different libraries.

@connectdotz
Copy link
Collaborator

@ehaynes99 the current thinking is to only remove 'jsx' babel plugin from .ts files. All the other file extensions will continue to be able to use 'jsx' syntax/plugin. Does that address your concern? If not, please elaborate.

Based on these discussions (microsoft/TypeScript#26489 and microsoft/TypeScript#30503), typescript standard is to allow 'jsx' syntax only for .tsx files. Even though many users have asked for 'jsx' in .ts files, the request did not seem to be approved so far. So I think removing 'jsx' plugin from .ts file would be all right as it is aligned with the typescript standard.

@ehaynes99
Copy link
Contributor

I think it's safe to consider bracket-style type assertions and .tsx files to be mutually exclusive, as typescript itself was unable to solve for both in the same file, leading to the introduction of the as keyword.
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-6.html#new-tsx-file-extension-and-as-operator

It's NOT safe to remove the jsx plugin for .js files. create-react-app started recommending against using the .jsx extension in 2016, and the overwhelming majority of React files are simply .js: https://github.com/facebook/create-react-app/releases/tag/v0.4.1

Finally, it's a misnomer to equate React and JSX. It's the most popular library using it, but certainly not the only one.

@ehaynes99
Copy link
Contributor

Here is a PR that allows for distinct handling of the 3 types. I also did a bit of cleanup on the parser test.
#75

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants