question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Better parser error when using jsx

See original GitHub issue

Was 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:closed
  • Created 3 years ago
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

4reactions
nicolo-ribaudocommented, Apr 29, 2020

This bug needs different steps in order to be fixed, but they can be done in the same PR.

  1. First, we should make @babel/parser throw a developer-friendly error. Consider these examples:

    require("@babel/parser").parse("<div></div>", {});
    require("@babel/parser").parse("a |> b", {});
    

    The first one throws SyntaxError: Unexpected token (1:0), while the second one throws a more actionable SyntaxError: This experimental syntax requires enabling the parser plugin: 'pipelineOperator' (1:2). Why this difference? If you search for the "pipelineOperator" string in packages/babel-parser/src, you’ll see that (as @hzoo mentioned above) we are calling the this.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 the jsx 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 big switch statement, and (at the end) we throw this.unexpected() if we don’t find any valid token. We could add a new case and, if the token is <, call this.expectPlugin("jsx").

    Here are a few possible tests:

    • When the jsx plugin is not enabled, this code should throw the “nice” error:
      <div /></div>
      
    • This is valid JS code, and it should never throw:
      "use strict"
      <div
      
    • When the typescript plugin is enabled, this code shouldn’t throw because it’s a type parameter:
      <div>() => {}
      
  2. Then, we should make @babel/core handle the parser error and suggest the correct @babel/... plugin/preset. You can add the syntax plugin and the react preset at packages/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 and yarn available on your machine)

  1. Write a comment there to let other possible contributors know that you are working on this bug.
  2. Fork the repo
  3. Run git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
  4. Run yarn && make bootstrap
  5. Wait ⏳
  6. Run make watch (or make build whenever you change a file)
  7. Add some tests: packages/babel-parser/test/fixtures/jsx/errors/_no-plugin, packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-no-jsx and packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param. You can see an example at packages/babel-parser/test/fixtures/experimental/_no-plugin/import-meta.
  8. Update the code!
  9. yarn jest babel-parser to run the tests
    • If some test outputs don’t match but the new results are correct, you can delete the bad output.js files and run the tests again
    • If you prefer, you can run OVERWRITE=true yarn jest babel-parser and they will be automatically updated.
  10. If it is working, run make test to run all the tests
  11. Run git push and open a PR!
3reactions
existentialismcommented, Apr 29, 2020

@karansapolia it’s yours! let us know (here or on slack) if you have any questions!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found