3

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"

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Naxi
  • 1,504
  • 5
  • 33
  • 72
  • 1
    the issue is with running lint only on local. The app code is working fine. – Naxi Dec 01 '21 at 16:39
  • 1
    Also there are quite a few search results when I search for that error in combination with "eslint" -> [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – Andreas Dec 01 '21 at 16:42
  • 1
    eslint :"^7.32.0" – Naxi Dec 01 '21 at 16:42
  • 2
    Why are you using this syntax at all? If those as class functions, just declare them as such. `class ABC { async func(...) { ... } func2(...) { ... }}`. Don't declare them as instance properties. – Mike 'Pomax' Kamermans Dec 01 '21 at 16:46

1 Answers1

6

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.

GOTO 0
  • 42,323
  • 22
  • 125
  • 158