acornjs / acorn-jsx

Alternative, faster React.js JSX parser
MIT License
648 stars 74 forks source link

syntax error on HTML comments. #41

Closed mysticatea closed 7 years ago

mysticatea commented 8 years ago

From https://github.com/ternjs/acorn/issues/454#issuecomment-242154528.

If I enable this plugin, HTML comments come to trigger syntax errors.

In the case that JSX plugin is disabled:

"use strict";

var acorn = require('acorn-jsx');
var ast = acorn.parse(`
<!--
`.trim(), {plugins: {jsx: false}});

console.log(JSON.stringify(ast, null, 4));
{
    "type": "Program",
    "start": 0,
    "end": 4,
    "body": [],
    "sourceType": "script"
}

In the case that JSX plugin is enabled:

"use strict";

var acorn = require('acorn-jsx');
var ast = acorn.parse(`
<!--
`.trim(), {plugins: {jsx: true}});

console.log(JSON.stringify(ast, null, 4));
C:\Users\t-nagashima.AD\Documents\GitHub\acorn\node_modules\acorn-jsx\node_modules\acorn\dist\acorn.js:2223
    throw err
    ^

SyntaxError: Unexpected token (1:1)
    at Parser.pp$4.raise (C:\Users\t-nagashima.AD\Documents\GitHub\acorn\node_modules\acorn-jsx\node_modules\acorn\dist\acorn.js:2221:15)
    at Parser.pp.unexpected (C:\Users\t-nagashima.AD\Documents\GitHub\acorn\node_modules\acorn-jsx\node_modules\acorn\dist\acorn.js:603:10)
    at Parser.pp.jsx_parseIdentifier (C:\Users\t-nagashima.AD\Documents\GitHub\acorn\node_modules\acorn-jsx\inject.js:191:12)
    at Parser.pp.jsx_parseNamespacedName (C:\Users\t-nagashima.AD\Documents\GitHub\acorn\node_modules\acorn-jsx\inject.js:200:21)
    at Parser.pp.jsx_parseElementName (C:\Users\t-nagashima.AD\Documents\GitHub\acorn\node_modules\acorn-jsx\inject.js:213:21)
    at Parser.pp.jsx_parseOpeningElementAt (C:\Users\t-nagashima.AD\Documents\GitHub\acorn\node_modules\acorn-jsx\inject.js:287:22)
    at Parser.pp.jsx_parseElementAt (C:\Users\t-nagashima.AD\Documents\GitHub\acorn\node_modules\acorn-jsx\inject.js:310:31)
    at Parser.pp.jsx_parseElement (C:\Users\t-nagashima.AD\Documents\GitHub\acorn\node_modules\acorn-jsx\inject.js:359:17)
    at Parser.parseExprAtom (C:\Users\t-nagashima.AD\Documents\GitHub\acorn\node_modules\acorn-jsx\inject.js:381:23)
    at Parser.pp$3.parseExprSubscripts (C:\Users\t-nagashima.AD\Documents\GitHub\acorn\node_modules\acorn-jsx\node_modules\acorn\dist\acorn.js:1715:21)

If there are HTML comments at places that JSXElements cannot exist, HTML comments don't trigger syntax errors.

"use strict";

var acorn = require('acorn-jsx');
var ast = acorn.parse(`
a<!--
`.trim(), {plugins: {jsx: true}});

console.log(JSON.stringify(ast, null, 4));
{
    "type": "Program",
    "start": 0,
    "end": 5,
    "body": [
        {
            "type": "ExpressionStatement",
            "start": 0,
            "end": 1,
            "expression": {
                "type": "Identifier",
                "start": 0,
                "end": 1,
                "name": "a"
            }
        }
    ],
    "sourceType": "script"
}
RReverser commented 7 years ago

Released a fixed version, sorry for not noticing it for so long.