Warm tip: This article is reproduced from serverfault.com, please click

Eslint does not recognize private field declaration using nodejs 12

发布于 2019-08-06 23:17:19

Eslint will not recognize private fields marked with # in class declarations, even though I'm using NodeJS version 12 (which supports them).

I am running NodeJS v12.7.0. I have searched all DuckDuckGo and Goole and I cannot find a plugin or option in eslint which will tell it to accept the private field notation (#). I have emca set to version 10.

class MyClass {
   #foo = 'bar';
   #bar = 'foo';

   constructor(foo, bar) {
      this.#foo = foo;
      this.#bar = bar;
   }
   ...
};

When I run eslint on the above code, I get:

2:3 error Parsing error: Unexpected character '#'

The project I'm working on does not use Babel, and I don't want to have to include it just to make private fields work. Any ideas how to make this work without having to resort to using Babel?

(Nothing against Babel of course, it's just on this particular project I don't want it).

Questioner
Doug Barbieri
Viewed
0
Michał Czapliński 2020-10-01 04:09:22

I think that you might have to bite the bullet and use babel-eslint: https://github.com/babel/babel-eslint, which requires that you install babel/core@>=7.2.0

Even though the private class fields are included in node 12, it's still a Stage 3 experimental feature according to the spec (as of August 2019)

npm install eslint babel-eslint --save-dev
# or
yarn add eslint babel-eslint -D

and add

  "parser": "babel-eslint",

to your .eslintrc.js file