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.

Proposal: throw syntax error for `}` and `>` in JSX text.

See original GitHub issue

Feature Request

  • I would like to work on this feature!
    • If needs be, I can look into it

Is your feature request related to a problem? Please describe.

The JSX spec [1][2] lists } and > as invalid JSX text characters.

JSXTextCharacter :

  • SourceCharacter but not one of {, <, > or }

However, babel does not throw a syntax error when it encounters a naked } or > in a JSXText string.

According to the spec, the only way you should be able to include either of these characters (similarly for { and <), is via a string expression: {'>'}.

Describe the solution you’d like I propose that babel should follow the JSX spec, and throw a syntax error if it encounters a “naked” } or > within jsx text.

From my experience, it’s uncommon that these characters are intentionally put in JSX text, and are instead the result of typos (i.e. typing {}, but having an editor auto-complete an extra } => {}}), or copy pasting errors.

Flow has already updated their parser to flag this as an error [3][4]. TypeScript is happy to make this change as well [5] (if Babel makes the change, as babel is generally considered the “reference implementation” for JSX). I have also raised an issue in acorn for the same functionality [6].

Describe alternatives you’ve considered

  • Custom lint rules
    • We had these implemented at facebook before flow made it a parsing error. (The lint rules actually spurred the change).
    • Outside of a tightly controlled monorepo, I can see that it would be hard to gain adoption for the rule, as not everyone uses linting tools, not everyone uses the react plugins, etc etc.
  • Prettier auto-formatting to convert } to {'}'}.
    • This is an AST change, and prettier doesn’t like to change the AST when formatting.

Teachability, Documentation, Adoption, Migration Strategy If you can, explain how users will be able to use this and possibly write out a version the docs. Maybe a screenshot or design?

I think the best way to document this would be via clear and explicit error messaging. For example, the following code:

function F() {
  return <div>></div>;
}

function G() {
  return <div>{1}}</div>;
}

produces the following flow errors:

4:   return <div>></div>;
                 ^ Unexpected token `>`. Did you mean `{'>'}`?
8:   return <div>{1}}</div>;
                    ^ Unexpected token `}`. Did you mean `{'}'}`?

flow.org repl


Related: [1] https://facebook.github.io/jsx/ [2] https://github.com/facebook/jsx/pull/18 [3] https://github.com/facebook/flow/blob/master/Changelog.md#01160 [4] facebook/flow@e1d0038 [5] https://github.com/microsoft/TypeScript/issues/36341 [6] https://github.com/acornjs/acorn-jsx/issues/106

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jridgewellcommented, Jan 22, 2020

We’re just a few weeks away: https://github.com/babel/babel/issues/10746

This is actually a super easy fix, so it’s a Good First Issue. Do you want to do it? The only changes necessary are in https://github.com/babel/babel/blob/1815c349877acca1d3275ea72512ee7824db49e3/packages/babel-parser/src/plugins/jsx/index.js#L411-L447, and some tests.

1reaction
jridgewellcommented, Mar 20, 2020

Closed by #11046

Read more comments on GitHub >

github_iconTop Results From Across the Web

throw syntax error for `}` and `>` in JSX text. · Issue #106 ...
I propose that acorn should follow the JSX spec, and throw a syntax error if it encounters a "naked" } or > within...
Read more >
ReactJS throwing syntax error - Stack Overflow
First, make sure that you are using some JSX compiler. Adjacent JSX elements must be wrapped in an enclosing tag. Considering:
Read more >
Error Handling in React 16 – React Blog
Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.
Read more >
babel/plugin-transform-react-jsx
NOTE**: This plugin is included in `@babel/preset-react` ... Toggles whether or not to throw an error if an XML namespaced tag name is...
Read more >
React Error Handling and Logging Best Practices
This article will go over best practices for error handling and logging ... However, if any code segment throws an error, you must...
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