@typescript-eslint/no-unused-vars 不起作用

huangapple go评论87阅读模式
英文:

@typescript-eslint/no-unused-vars not working

问题

这是您提供的配置文件的翻译:

.eslintrc.json:

{
  "root": true,
  "ignorePatterns": ["projects/**/*"],
  "overrides": [
    {
      "files": ["*.ts"],
      "extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:@angular-eslint/recommended",
        "plugin:@angular-eslint/template/process-inline-templates",
        "plugin:prettier/recommended"
      ],
      "rules": {
        "no-unused-vars": "error",
        "@typescript-eslint/no-unused-vars": "error",
        "@typescript-eslint/explicit-member-accessibility": "error",
        "@angular-eslint/directive-selector": [
          "error",
          {
            "type": "attribute",
            "prefix": "app",
            "style": "camelCase"
          }
        ],
        "@angular-eslint/component-selector": [
          "error",
          {
            "type": "element",
            "prefix": "app",
            "style": "kebab-case"
          }
        ]
      }
    },
    {
      "files": ["*.html"],
      "extends": ["plugin:@angular-eslint/template/recommended"],
      "rules": {}
    },
    {
      "files": ["*.html"],
      "excludedFiles": ["*inline-template-*.component.html"],
      "extends": ["plugin:prettier/recommended"],
      "rules": {
        "prettier/prettier": ["error", { "parser": "angular" }]
      }
    }
  ]
}

Angular 组件:

export class AppComponent {
  public title = 'angular-movieseat';
  public something = 'asd';

  public somethingFn() {
    console.log('something');
  }
}

有关未使用的变量的问题:
您提到somethingsomethingFn()从未被使用,但ESLint未标记它们为未使用。您已经尝试了以下规则:

        "no-unused-vars": "error",
        "@typescript-eslint/no-unused-vars": "error",

如果它们仍未被标记为未使用,您可能需要检查项目的其他配置或确保您在正确的文件上运行 ESLint 检查。

英文:

.eslintrc.json:

{
  "root": true,
  "ignorePatterns": ["projects/**/*"],
  "overrides": [
    {
      "files": ["*.ts"],
      "extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:@angular-eslint/recommended",
        "plugin:@angular-eslint/template/process-inline-templates",
        "plugin:prettier/recommended"
      ],
      "rules": {
        "no-unused-vars": "error",
        "@typescript-eslint/no-unused-vars": "error",
        "@typescript-eslint/explicit-member-accessibility": "error",
        "@angular-eslint/directive-selector": [
          "error",
          {
            "type": "attribute",
            "prefix": "app",
            "style": "camelCase"
          }
        ],
        "@angular-eslint/component-selector": [
          "error",
          {
            "type": "element",
            "prefix": "app",
            "style": "kebab-case"
          }
        ]
      }
    },
    {
      "files": ["*.html"],
      "extends": ["plugin:@angular-eslint/template/recommended"],
      "rules": {}
    },
    {
      "files": ["*.html"],
      "excludedFiles": ["*inline-template-*.component.html"],
      "extends": ["plugin:prettier/recommended"],
      "rules": {
        "prettier/prettier": ["error", { "parser": "angular" }]
      }
    }
  ]
}

angular component:

export class AppComponent {
  public title = 'angular-movieseat';
  public something = 'asd';

  public somethingFn() {
    console.log('something');
  }
}

the something and somethingFn() are never used, but ESlint doesn't flag them as being unused. In all the questions I've read this should resolve that:

        "no-unused-vars": "error",
        "@typescript-eslint/no-unused-vars": "error",

But it doesn't, any suggestions how to flag these class variables as unused? ESLint does work since it requires the explicit-member-accessibility rule.

For completion sake my tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "ES2022",
    "module": "ES2022",
    "useDefineForClassFields": false,
    "lib": ["ES2022", "dom"],
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

答案1

得分: 1

Angular 有点不同,因为我们可以在组件的 HTML 文件中引用组件变量和方法,ESLint 无法识别这些情况,因此您不会收到错误。唯一的方法是手动审查变量和方法,然后在 HTML 和 TS 文件中进行比较,以确定它们是否在任何地方被使用。

注意:最好进行全局搜索以查找变量名的所有用法,还应检查父级是否通过 ViewChild 访问属性。

英文:

Angular is a bit different, because we can refer to component variables and methods inside the HTML file of the component, ESLint cannot identify these cases, hence you are not getting that error. The only way to do this, is to manually audit the variables and methods and compare in the HTML and TS file and determine if they are used anywhere.

> Note: Best to do a global search for the variable name to find all usages, to find out, you should also check the parent if the property is accessed via ViewChild.

答案2

得分: 1

显然,ESLint 在与 Angular 配合时存在问题:

import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent {
  public title = 'angular-movieseat';
  private something = 'asd';

  private somethingFn(): void {
    console.log('something');
    const something = 'something';
  }
}

当我声明 const something 时,我确实得到了未使用变量的预期错误。顶部文件中的 Input 导入也会产生相同的警告。

同样,当我将它们声明为私有时,somethingsomethingFn() 也会产生警告。

英文:

Apparently ESLint doesn't "play well" with Angular:

import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent {
  public title = 'angular-movieseat';
  private something = 'asd';

  private somethingFn(): void {
    console.log('something');
    const something = 'something';
  }
}

When I declare the const something I do get the expected error of being an unused variable. Same with the Input import at the top of the file.

I also get warnings for something and somethingFn() when I declare them private.

@typescript-eslint/no-unused-vars 不起作用

答案3

得分: 0

没有实际项目参考很难说,但我假设你的 .eslintrc.json 应该像这样(我只做了更改)
"files": ["*.ts"],
"parserOptions": {
"project": ["tsconfig.json"],
"createDefaultProgram": true
},
"plugins": ["@typescript-eslint"]
英文:

It's hard to say without actual project ref but I assume your .eslintrc.json should be like this (I put changes only)

"files": ["*.ts"],
"parserOptions": {
"project": ["tsconfig.json"],
"createDefaultProgram": true
},
"plugins": ["@typescript-eslint"],

huangapple
  • 本文由 发表于 2023年4月10日 19:08:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定