【发布时间】:2022-01-02 22:39:45
【问题描述】:
我按照How do I configure ESLint to allow fat arrow class methods 上的建议将解析器设置为babel-eslint。
我安装并更新了我的配置文件如下:
{
"parserOptions": {
"parser": "babel-eslint",
"ecmaVersion": 12,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"rules": {
"semi": "error",
"indent": ["error", 2],
"eqeqeq": ["error", "always"],
"max-depth": ["error", 5],
"space-before-function-paren": ["error", "never"],
"template-curly-spacing": ["error", "always"],
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"curly": "error",
"brace-style": ["error", "1tbs"],
"space-before-blocks": "error"
}
}
但是还是破坏了eslint,解析错误如下:
class Person {
constructor() {
console.log(this);
this.hello1();
this.hello2();
}
// breaks eslint, but WHY?
hello1 = () => {
console.log(this);
}
hello2() {
console.log(this);
}
}
const P1 = new Person();
它突出显示第一个= 并说:
解析错误 意外令牌 =
如何进一步解决此问题? ESLint 正确地应用了这个文件中的所有规则,但似乎忽略了解析器选项。
还是别的什么?
我不确定这是否与此处相关:
但我真的不知道那是什么意思。
【问题讨论】:
-
具体的eslint错误是什么?
-
变量真的定义了吗?你的 Babel 设置是什么?给minimal reproducible example。
-
请只发minimal reproducible example。我们只需要看相关代码。 (编辑:谢谢)
-
我可以复制该问题 (bit.ly/3JAh3nl) - 这绝对是一个配置问题,并且一直存在于 since at least 2015
-
你还没有展示你的 Babel 配置,它包含类属性吗?根据babeljs.io/docs/en/babel-plugin-proposal-class-properties,它们在 ES2022 的预设中,不是 ecmaVersion: 12。
标签: javascript babeljs eslint class-fields