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 error when parsing a class with static properties #1231

Closed
philipkaiser opened this issue Aug 4, 2023 · 1 comment
Closed

Comments

@philipkaiser
Copy link

Description

When parsing an input that defines a class with a static class property an unexpected token error is thrown.

Here is my stack-trace:

SyntaxError: Unexpected token (1:24)
    at pp$4.raise (/.../node_modules/acorn/dist/acorn.js:3560:15)
    at pp$9.unexpected (/.../node_modules/acorn/dist/acorn.js:768:10)
    at pp$9.expect (/.../node_modules/acorn/dist/acorn.js:762:28)
    at pp$5.parseMethod (/.../node_modules/acorn/dist/acorn.js:3315:10)
    at pp$8.parseClassMethod (/.../node_modules/acorn/dist/acorn.js:1529:37)
    at pp$8.parseClassElement (/.../node_modules/acorn/dist/acorn.js:1487:12)
    at pp$8.parseClass (/.../node_modules/acorn/dist/acorn.js:1405:26)
    at pp$8.parseStatement (/.../node_modules/acorn/dist/acorn.js:914:19)
    at pp$8.parseTopLevel (/.../node_modules/acorn/dist/acorn.js:825:23)
    at Parser.parse (/.../node_modules/acorn/dist/acorn.js:597:17) {
  pos: 24,
  loc: Position { line: 1, column: 24 },
  raisedAt: 25
}

Node.js v18.16.0

I assume that the class property is mistakenly identified / parsed as a class method as there is an invocation of parseClassMethod in the middle of the stack-trace. This thought matches with the unexpected token error type as the expected token would be a ( for a class method in this case.

Repro steps

  1. run npm init -y
  2. run npm i acorn
  3. create an index.js file:
const acorn = require("acorn");
acorn.parse("class Example {static x = {};}", { ecmaVersion: 2020 });
  1. run the file node index.js
@marijnh
Copy link
Member

marijnh commented Aug 4, 2023

Which ECMAScript version introduced class properties?

Right, 2022.

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

No branches or pull requests

2 participants