17

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"
  }
}
Bergi
  • 513,640
  • 108
  • 821
  • 1,164
Lucas Fabre
  • 948
  • 1
  • 7
  • 20

3 Answers3

26

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.

golopot
  • 6,892
  • 2
  • 27
  • 38
4

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"
Dorian
  • 1,551
  • 8
  • 23
0

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.

aercolino
  • 1,707
  • 1
  • 17
  • 17