我有一个angular 12.x项目,它使用eslint,这是当前的根配置文件:
{
"env": {
"browser": true,
"node": true
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.base.json",
"sourceType": "module"
},
"plugins": [
"eslint-plugin-import",
"@angular-eslint/eslint-plugin",
"@angular-eslint/eslint-plugin-template",
"@typescript-eslint"
],
"extends": ["prettier", "eslint:recommended"],
"rules":{... lots of rules}
}我需要使用的规则之一是member-ordering as described on their docs。
我有一个库(使用NX创建),它们是可重用的组件,让我在这个例子中使用MyComponent。该组件有自己的eslintrc,它在其内容下扩展了根组件:
{
"extends": ["../../../.eslintrc.json"], //path to the root eslintrc
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts"],
"extends": [
"plugin:@nrwl/nx/angular",
"plugin:@angular-eslint/template/process-inline-templates"
],
"rules": {
"@angular-eslint/directive-selector": [
"error",
{
"type": "attribute",
"prefix": "suppressed",
"style": "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
"type": "element",
"prefix": "suppressed",
"style": "kebab-case"
}
]
}
},
{
"files": ["*.html"],
"extends": ["plugin:@nrwl/nx/angular-template"],
"rules": {}
}
]
}这是使用上述eslintrc的MyComponent:
export class MyComponent implements OnInit, OnDestroy {
constructor() {}
ngOnInit(): void {}
@Dispatch()
clearState() {
return new ViewActions.ClearState();
}
private functio() {
return null;
}
static handle() {}
ngOnDestroy(): void {
this.clearState();
}
}根据我链接的文档,根据规则的默认配置,由于违反规则,上面的代码应该从@Dispatch()一直到destroy()都有错误。
但输出与预期不同:

现在,如果我按照文档将规则配置添加到我的组件eslintrc.json中,它将按预期工作:方法clearState将出现错误,因为它被注释了,并且预计它将出现在未注释的方法之前,在此ngOnInit中,私有方法也会出现错误。
实际的问题是,我的组件eslintrc.json的第一行就有针对根eslintrc的extends,而我希望在该文件上覆盖我的所有规则。如果我将成员顺序更改为根eslintrc,则规则将停止工作,并恢复为添加的映像的错误。
此外,当从我的组件目录运行npx eslint --print-config .\my-component.ts时,我得到了以下配置,用于应用成员排序:
"@typescript-eslint/member-ordering": [
"error",
{
"default": [
"static-field",
"instance-field",
"static-method",
"instance-method"
]
}
],我真的不知道这是从哪里来的。
有人知道为什么我的组件eslint没有从根eslintrc扩展规则吗?
=更新=
package.json上与Eslint相关的依赖项
"@angular-eslint/eslint-plugin": "1.1.0",
"@angular-eslint/eslint-plugin-template": "1.1.0",
"@angular-eslint/template-parser": "12.3.0",
"@nrwl/eslint-plugin-nx": "12.9.0",
"@typescript-eslint/eslint-plugin": "4.28.5",
"@typescript-eslint/eslint-plugin-tslint": "4.12.0",
"@typescript-eslint/parser": "4.28.5",
"eslint": "7.32.0",
"eslint-config-prettier": "8.1.0",
"eslint-plugin-cypress": "2.10.3",
"eslint-plugin-import": "2.24.0",
"@angular-eslint/schematics": "12.3.1",发布于 2021-10-22 20:29:13
也许this能帮上忙。我认为你一般应该倾向于在项目中使用"root": true。
https://stackoverflow.com/questions/69682573
复制相似问题