benjamn / recast

JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator
MIT License
4.95k stars 346 forks source link

esprima throws on React Fragment shorthand notation #1337

Open Jazzmanpw opened 1 year ago

Jazzmanpw commented 1 year ago

Steps to reproduce

import {parse} from 'recast'

parse('<></>')

Expected result

A Program is returned with JSXFragmen expression

Actual result

An error is thrown

Error: Line 1: Unexpected token >
    at ErrorHandler.constructError (...\node_modules\esprima\dist\esprima.js:5012:22)
    at ErrorHandler.createError (...\node_modules\esprima\dist\esprima.js:5028:27)
    at JSXParser.Parser.unexpectedTokenError (...\node_modules\esprima\dist\esprima.js:1985:39)
    at JSXParser.Parser.throwUnexpectedToken (...\node_modules\esprima\dist\esprima.js:1995:21)
    at JSXParser.parseJSXIdentifie ( ...\node_modules\esprima\dist\esprima.js:724:19)
    at JSXParser.parseJSXElementName (...\node_modules\esprima\dist\esprima.js:730:33)
    at JSXParser.parseJSXOpeningElement (...\node_modules\esprima\dist\esprima.js:817:26)
    at JSXParser.parseJSXElement (...\node_modules\esprima\dist\esprima.js:926:29)
    at JSXParser.parseJSXRoot (...\node_modules\esprima\dist\esprima.js:942:29)
    at JSXParser.parsePrimaryExpression (...\node_modules\esprima\dist\esprima.js:466:40)

I found an issue from esprima, but it was fixed long ago. Also I found a recast issue, but it had different error and provided no details on solution

Current workaround that I found is to pass babel parser, but esprima can handle this case, so I'm confused why it's happening

import * as babelParser from 'recast/parsers/babel';

parse('<></>', {parser: babelParser}); // parses with no issues

Any idea on why is it happening?

eventualbuddha commented 1 year ago

Looks like https://github.com/jquery/esprima/issues/2020 isn't actually fixed, so that's likely the cause.