Eslint does not allow static class properties
Asked Answered
A

4

48

I'm current developing an API on Node 12.14.1 and using Eslint to help me write the code. Unfortunately it does not allow me to set static class properties as shown below:

class AuthManager {
  static PROP = 'value'
}

The following error is given: Parsing error: Unexpected token =eslint

Static class properties are already supported on JS and on Node.
How can this rule be disable?

I also have the following .eslintrc.json file:

{
  "env": {
      "es6": true,
      "node": true
  },
  "extends": "eslint:recommended",
  "globals": {
      "Atomics": "readonly",
      "SharedArrayBuffer": "readonly"
  },
  "parserOptions": {
      "ecmaVersion": 2018,
      "sourceType": "module"
  }
}
Avner answered 3/2, 2020 at 20:40 Comment(0)
L
68

ESLint v8 now supports static class properties natively: https://eslint.org/blog/2021/10/eslint-v8.0.0-released

parserOptions ecmaVersion should be set to 13, 2022, or "latest" to enable the support.

Add this to your .eslint.(cjs | json | js)

{
  parserOptions: {
    ecmaVersion: 2022,
  }
}
Landscape answered 14/10, 2021 at 18:43 Comment(7)
As of v8 being released, this is now the 'right' answer.Patchouli
For some reason I'm still getting the same error on 8.6.0. Not sure if I'm the only one but if anyone reads this, know that you are not...Lorenzoloresz
FWIW, I ended up having to use @babel/eslint-parser as specified in other answers. Was trying to avoid using babel anywhere. But, at least it still works.Lorenzoloresz
Here is the configuration you need to add in your eslintrc config file (for a JSON file): { "parserOptions": { "ecmaVersion": "latest" } }Present
I tried this as I'm using @typescript-eslint/parser but does not work. using [email protected]Berton
I'm confirming that it works in with node 12, eslint 8.15. I just added exactly the lines that need to be added to .eslint.cjsPerutz
If you are targeting Node.js 14, do not set ecmaVersion to 2022. Only some of ES2022's features have been implemented in Node.js 14.Laxation
A
44

ESLint with its default parser does not support class fields syntax for now. You can solve the problem by changing the configured parser to babel-eslint.

npm install --save-dev babel-eslint
// eslintrc.json
{
  "parser": "babel-eslint",
  ...
}

Eslint's default parser, Espree, does not support class fields because that syntax is currently stage 3, and that it is decided that only stage 4 proposals are to be supported in Espree.

Afflux answered 29/2, 2020 at 11:5 Comment(4)
Thanks! After setting babel-eslint the problem was solved.Avner
babel-eslint is now @babel/eslint-parser.Milzie
According to developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… static is supported since node 6. I'm using node 14, the latest stable LTS version. It is not pretty clear for me what is the reason for ESLint to not support it yet.Perutz
@Perutz according to TC39 Finished Proposals, Class Properties is a finished proposal and is included in ES2022. At the meantime, ESLint 7.x offically supports up to ES2020. This is why you have to use @babel/eslint-parser. Babel ESLint Parser covers all experimental syntax (lists here) that ESLint default parser doesn't support.Toxoplasmosis
C
8

As of now, I had to use these configs

.eslintrc.js

module.exports = {
  env: {
    node: true,
    es6: true,
  },
  extends: [
    'airbnb-base',
  ],
  parser: '@babel/eslint-parser',
  parserOptions: {
    babelOptions: {
      configFile: './.babelrc',
    },
    ecmaVersion: 2018, // needed to support spread in objects
  },
  plugins: ['@babel'],
};

.babelrc

{
  "presets": ["@babel/env"],
  "plugins": [
    "@babel/plugin-syntax-class-properties"
  ]
}

For which I had to install:

npm i -D @babel/preset-env
npm i -D @babel/eslint-parser
npm i -D @babel/eslint-plugin
npm i -D @babel/plugin-syntax-class-properties

Notice that the @babel modules above are the only @babel modules in my package.json.

Copolymer answered 5/2, 2021 at 10:43 Comment(0)
H
7

you need to install @babel/eslint-parser:

yarn add --dev @babel/eslint-parser

And have the parser in your .eslintrc.yml for instance:

parser: "@babel/eslint-parser"
Hackamore answered 31/1, 2021 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.