Skip to content

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

Closed
@bradzacher

Description

@bradzacher

TypeScript Version: 3.7.x-dev.201xxxxx

Search Terms:
jsx children child

Code

import * as React from 'react';

const a = <div>}</div>;
const b = <div>></div>;

Expected behavior:

Typescript throws a syntax error, as } and > are not valid JSX text characters, as per the JSX spec:

https://facebook.github.io/jsx/

JSXTextCharacter :

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

Actual behavior:

No errors.

Related - typescript errors on { and <, but not because they are invalid characters; it instead errors due to the ambiguity in the syntax and it treating them as an unterminated expression and an incorrect tag respectively.

Playground Link: https://www.typescriptlang.org/play/?jsx=2&ssl=1&ssc=1&pln=5&pc=1#code/JYWwDg9gTgLgBAKjgQwM5wEoFNkGN4BmUEIcA5FDvmQNwBQduEAdqvMnALxwA8AJsABuAPgC+PAPQCR9Jq3gAjLr2nDhk1fSA

Related Issues:
https://github.com/facebook/flow/blob/master/Changelog.md#01160
facebook/flow@e1d0038
babel/babel#11042
acornjs/acorn-jsx#106

I'm bringing this up because flow just made this change, and it seems like a logical one.

Considering it's almost always going to be a UI bug caused due to typos, it seems like a good idea to error on it. Those that want those characters can be explicit and wrap the character as an expression ({'>'}, which is the same thing you do if you want an end of line space).

For reference, this is what now happens when you attempt to parse } or > in flow:

This 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing codeBugA bug in TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions