Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected token #204

Closed
julianjensen opened this issue Feb 15, 2022 · 2 comments · Fixed by #209
Closed

Unexpected token #204

julianjensen opened this issue Feb 15, 2022 · 2 comments · Fixed by #209
Assignees

Comments

@julianjensen
Copy link

I'm getting an Unexpected token error when parsing React code where an inline JavaScript expression starts with a /, see below for example. Here's a minimal example with the acorn parser thrown in for comparison.

const test = `const C = props => {
    <Component>
        <div>
            {
                /abc/.test( props.xyz ) || <OtherComponent />
            }
        </div>
    </Component>
}
`;

console.log( `------------------------ ACORN` );
const r1 = AcornParser.parse( test, { sourceType: 'module', ecmaVersion: 2022 });
console.log( r1?.type );
console.log( `------------------------ MERIYAH` );
const r2 = MeriyahParser.parse( test, { module: true, next: true, jsx: true });
console.log( r2?.type );

This produces the following output.

------------------------ ACORN
Program
------------------------ MERIYAH
file:///home/.../project/node_modules/meriyah/dist/meriyah.esm.js:181
    throw new ParseError(parser.index, parser.line, parser.column, type, ...params);
          ^

ParseError [SyntaxError]: [5:17]: Unexpected token: '/'
    at report (file:///home/.../project/node_modules/meriyah/dist/meriyah.esm.js:181:11)
    at parsePrimaryExpression (file:///home/.../project/node_modules/meriyah/dist/meriyah.esm.js:6374:13)
    at parseExpression (file:///home/.../project/node_modules/meriyah/dist/meriyah.esm.js:5802:16)
    at parseJSXExpressionContainer (file:///home/.../project/node_modules/meriyah/dist/meriyah.esm.js:8726:22)
    at parseJSXChild (file:///home/.../project/node_modules/meriyah/dist/meriyah.esm.js:8596:16)
    at parseJSXChildren (file:///home/.../project/node_modules/meriyah/dist/meriyah.esm.js:8588:23)
    at parseJSXRootElementOrFragment (file:///home/.../project/node_modules/meriyah/dist/meriyah.esm.js:8536:20)
    at parseJSXChild (file:///home/.../project/node_modules/meriyah/dist/meriyah.esm.js:8598:16)
    at parseJSXChildren (file:///home/.../project/node_modules/meriyah/dist/meriyah.esm.js:8588:23)
    at parseJSXRootElementOrFragment (file:///home/.../project/node_modules/meriyah/dist/meriyah.esm.js:8536:20) {
  index: 82,
  line: 5,
  column: 17,
  description: "[5:17]: Unexpected token: '/'",
  loc: { line: 5, column: 17 }
}

This is using meriyah version 4.2.0

@3cp 3cp self-assigned this Mar 9, 2022
@3cp
Copy link
Member

3cp commented Mar 18, 2022

The tokeniser is not working correctly for jsx, it thought that / is a divide, instead of start of regular expression.

@3cp
Copy link
Member

3cp commented Mar 31, 2022

Released v4.2.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants