shapesecurity / shift-parser-js

ECMAScript parser that produces a Shift format AST
http://shift-ast.org/parser.html
Apache License 2.0
245 stars 28 forks source link

JSX support #426

Open elizabethdinella opened 5 years ago

elizabethdinella commented 5 years ago

Are there any plans to add support for JSX?

bakkot commented 5 years ago

No, we track the ECMAScript spec.

That said, it probably would not be too hard to extend the parser yourself to do so. Something along the lines of

let { GenericParser } = require('shift-parser'); // or ParserWithLocation if you want the location side-table

class JSXParser extends GenericParser {
  parsePrimaryExpression() {
    if (this.match(TokenType.LT)) {
      return this.parseJSX();
    }
    return super.parsePrimaryExpression();
  }

  parseJSX() {
    // implementation goes here
  }
}

let ast = (new JSXParser(code)).parseModule();

You might need to override the tokenizer methods as well; I don't know JSX well enough to know.