javascripteslintclass-fieldsecmascript-2021

Unexpected token "=" reported while running eslint on arrow functions


I have a JavaScript class and inside it I have an async method which looks like below.

class ABC {
    func = async () => { //----line 10
        //some code
    }
    func2 = () => { //----line 11
        //some code 
    }
}

When I run ESLint it's reporting one error. The application itself is working as expected.

unexpected token '=' at line 10 (& 11)

eslintrc.json

{
   "env":{
       "es2021":true
    }
}

What do I need to do in order to get rid of these lint errors and still keep these methods as arrow functions?

ESLint version: eslint :"^7.32.0"


Solution

  • Upgrade to ESLint 8 and add this setting to your .eslintrc:

    "parserOptions": {
      "ecmaVersion": 2022
    }
    

    Reason: you are using class fields. Support for class fields syntax in ESLint has been introduced with version 8.

    Note that the specification of class fields, although already finalized in April 2021, will be published with ECMAScript 2022, expected next year.