Better parser error when using jsx
See original GitHub issueWas checking https://github.com/babel/babel/pull/11478 and testing out syntax and noticed we don’t error with the normal “use this plugin/preset” like we do for proposals with JSX itself. @nicolo-ribaudo mentioned it’s probably since jsx is implemented as a normal plugin so we didn’t have this.expectPlugin("recordAndTuple");
unknown: Unterminated regular expression (1:5)
> 1 | <a></a>
| ^
unknown: Support for the experimental syntax 'pipelineOperator' isn't currently enabled (1:3):
> 1 | a |> b
| ^
Add @babel/plugin-proposal-pipeline-operator (https://git.io/vb4SU) to the 'plugins' section of your Babel config to enable transformation.
Issue Analytics
- State:
- Created 3 years ago
- Comments:15 (13 by maintainers)
Top Results From Across the Web
Getting a parsing error in with React.js - Stack Overflow
I've gone through the code to look for syntax errors, but I don't see anything. Has anyone else had this problem? javascript ·...
Read more >Parse error on JSX - SonarQube - Sonar Community
I try to remove errors raised by SonarJS sensor when running code analysis: ##[error]ERROR: Unable to parse file … ##[error]ERROR: Parse ...
Read more >babel/parser
The Babel parser generates AST according to Babel AST format. It is based on ESTree spec with the following deviations: ... AST for...
Read more >Parse Error Adjacent JSX elements must be wrapped in an ...
I am trying to set up my React.js app so that it only renders if a variable I have set is true. The...
Read more >[Solved]-React JSX parser error-Reactjs - appsloveworld.com
in JSX, <MyComponent /> alone is valid while <MyComponent> isn't. In your case you miss close tag for img . Try to use:...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This bug needs different steps in order to be fixed, but they can be done in the same PR.
First, we should make
@babel/parser
throw a developer-friendly error. Consider these examples:The first one throws
SyntaxError: Unexpected token (1:0)
, while the second one throws a more actionableSyntaxError: This experimental syntax requires enabling the parser plugin: 'pipelineOperator' (1:2)
. Why this difference? If you search for the"pipelineOperator"
string inpackages/babel-parser/src
, you’ll see that (as @hzoo mentioned above) we are calling thethis.expectPlugin("pipelineOperator")
function when we find the “pipeline” (|>
) operator.We can do something similar for JSX, but it’s harder. The main problem is that checks for ECMAScript proposals (such as
|>
) are inlined in the parser code, while checks for JSX code are only enabled when thejsx
plugin is enabled.However, we know that JSX elements are _atomic expressions (parsed by the
parseExprAtom
method) and that no other expression can start with<
. That method contains a bigswitch
statement, and (at the end) we throwthis.unexpected()
if we don’t find any valid token. We could add a new case and, if the token is<
, callthis.expectPlugin("jsx")
.Here are a few possible tests:
jsx
plugin is not enabled, this code should throw the “nice” error:typescript
plugin is enabled, this code shouldn’t throw because it’s a type parameter:Then, we should make
@babel/core
handle the parser error and suggest the correct@babel/...
plugin/preset. You can add the syntax plugin and thereact
preset atpackages/babel-core/src/parser/util/missing-plugin-helper.js
.If it is the first time that you contribute to Babel, follow these steps: (you need to have
make
andyarn
available on your machine)git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
yarn && make bootstrap
make watch
(ormake build
whenever you change a file)packages/babel-parser/test/fixtures/jsx/errors/_no-plugin
,packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-no-jsx
andpackages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param
. You can see an example atpackages/babel-parser/test/fixtures/experimental/_no-plugin/import-meta
.yarn jest babel-parser
to run the testsoutput.js
files and run the tests againOVERWRITE=true yarn jest babel-parser
and they will be automatically updated.make test
to run all the testsgit push
and open a PR!@karansapolia it’s yours! let us know (here or on slack) if you have any questions!