jquery / esprima

ECMAScript parsing infrastructure for multipurpose analysis
http://esprima.org
BSD 2-Clause "Simplified" License
7.04k stars 786 forks source link

where I can find list of all options. static keyword is failing #2068

Closed nsisodiya closed 3 years ago

nsisodiya commented 3 years ago

My code is like this

const filename = './src/components/SignUpForm/SignUpForm.js';
const ast = esprima.parse(fs.readFileSync(filename, 'utf-8'), { tokens: true, jsx: true, sourceType: 'module' });

The SignUpForm.js' code is like

40 - export class SignUpForm extends Component {
41 -   static displayName = 'SignUpForm';
42 -   static whyDidYouRender = true;

Getting the following error message. Searching the Internet but not able to get a list of all options to try.


/Users/narendra/Coding/prophecy/metaindex/frontend/node_modules/esprima/dist/esprima.js:1995
                throw this.unexpectedTokenError(token, message);
                ^
Error: Line 41: Unexpected token =
    at ErrorHandler.constructError (/Users/narendra/Coding/prophecy/metaindex/frontend/node_modules/esprima/dist/esprima.js:5012:22)
    at ErrorHandler.createError (/Users/narendra/Coding/prophecy/metaindex/frontend/node_modules/esprima/dist/esprima.js:5028:27)
    at JSXParser.Parser.unexpectedTokenError (/Users/narendra/Coding/prophecy/metaindex/frontend/node_modules/esprima/dist/esprima.js:1985:39)
    at JSXParser.Parser.throwUnexpectedToken (/Users/narendra/Coding/prophecy/metaindex/frontend/node_modules/esprima/dist/esprima.js:1995:21)```

Please help
nsisodiya commented 3 years ago

Demo - https://esprima.org/demo/parse.html?code=class%20Foo%20%7B%0A%20%20%20%20static%20displayName%20%3D%20%27SignUpForm%27%3B%0A%20%20%20%20constructor()%20%7B%0A%20%20%20%20%20%20%20%20if%20(new.target%20%3D%3D%3D%20Foo)%20%7B%7D%0A%20%20%20%20%7D%0A%7D%0A

jianweiqin commented 3 years ago

It's not the problem of keyword 'static'. The problem is you cannot declare fields (includes public and private, static and non-static) in the brace of class. You can use a getter/setter or do this outside the class:

class Foo { static get staticField () { return ''; } constructor() { this.instanceField = ''; } // instanceField = ''; // error get instanceField () { return ''; } } Foo.staticField = '';