Eslint无法使用Node.js 12识别私有字段声明 [英] Eslint does not recognize private field declaration using nodejs 12

查看:128
本文介绍了Eslint无法使用Node.js 12识别私有字段声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使我使用的是NodeJS版本12(支持它们),Eslint也无法识别类声明中带有#标记的私有字段.

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

我正在运行NodeJS v12.7.0.我已经搜索了所有DuckDuckGo和Goole,但是在eslint中找不到插件或选项来告诉它接受私有字段符号(#).我已将 emca 设置为版本 10 .

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;
   }
   ...
};

在上述代码上运行 eslint 时,我得到:

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

2:3错误解析错误:意外字符'#'

我正在处理的项目未使用Babel,并且我不想为了使私有字段正常工作而将其包括在内.有什么想法可以在不依靠Babel的情况下完成这项工作吗?

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?

(当然不反对Babel,这只是我不想要的特定项目).

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

推荐答案

我认为您可能不得不硬着头皮使用 babel-eslint :

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

即使私有类字段包含在节点12中,根据

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

并添加

  "parser": "babel-eslint",

到您的 .eslintrc.js 文件

这篇关于Eslint无法使用Node.js 12识别私有字段声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆