尝试对Angular模板进行代码检查时出现ESLint错误。

16 浏览
0 Comments

尝试对Angular模板进行代码检查时出现ESLint错误。

我有一个使用eslint和prettier设置的Angular 10应用程序,目前在对TypeScript文件进行linting方面运行良好。我现在还尝试对我的组件模板文件进行linting,遵循此文档。但是,当我使用`eslint . --ext .js,.ts,.component.html --fix`运行时,我一直得到以下错误:`Error while loading rule '@typescript-eslint/dot-notation': You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.`

我的`.eslintrc.js`文件如下(请注意`overrides`部分中的第二个块):

module.exports = {
  extends: [
    "plugin:@angular-eslint/recommended",
    "plugin:jasmine/recommended",
    "airbnb-typescript/base",
    "prettier/@typescript-eslint",
    "plugin:prettier/recommended",
  ],
  plugins: [
    "prettier",
    "jasmine",
  ],
  rules: {
    "import/no-unresolved": "off",
    "@angular-eslint/component-selector": [
      "error", { type: "element", prefix: "icc", style: "kebab-case" }
    ],
    "@angular-eslint/directive-selector": [
      "error", { type: "attribute", prefix: "icc", style: "camelCase" }
    ],
    "import/extensions": "off",
    "@typescript-eslint/lines-between-class-members": "off",
    "lines-between-class-members": "off",
    "class-methods-use-this": "off",
    "import/prefer-default-export": "off",
    "prettier/prettier": ["error", {
      "endOfLine":"auto"
    }],
  },
  parser: "@typescript-eslint/parser",
  parserOptions: {
    project: "./tsconfig.app.json",
    ecmaVersion: 2020,
    sourceType: "module",
  },
  settings: {
    'import/resolver': {
      node: {
        extensions: ['.ts', '.js'],
        moduleDirectory: ['node_modules', 'src/app'],
      }
    }
  },
  overrides: [
    {
      files: ["*.component.ts"],
      parser: "@typescript-eslint/parser",
      parserOptions: {
        project: "./tsconfig.app.json",
        ecmaVersion: 2020,
        sourceType: "module",
      },
      plugins: ["@angular-eslint/template"],
      processor: "@angular-eslint/template/extract-inline-html",
    },
    {
      files: ["*.component.html"],
      parser: "@angular-eslint/template-parser",
      parserOptions: {
        project: "./tsconfig.app.json",
        ecmaVersion: 2020,
        sourceType: "module",
      },
      plugins: ["@angular-eslint/template"],
    },
    {
      files: ["src/**/*.spec.ts", "src/**/*.d.ts"],
      parser: "@typescript-eslint/parser",
      parserOptions: {
        project: "./tsconfig.spec.json",
        ecmaVersion: 2020,
        sourceType: "module",
      },
      extends: ["plugin:jasmine/recommended"],
      plugins: ["jasmine"],
      env: { jasmine: true },
    }
  ],
};

正如您所看到的,我在每个地方都指定了`parserOptions.project`。我特别困惑的是错误消息抱怨与`@typescript-eslint/parser`相关的问题,而这不是应该用于模板的解析器。从`overrides`部分中删除`parserOptions`块并没有产生任何区别。

我可以为它抱怨的所有规则设置`"@typescript-eslint/dot-notation": "off"`,但显然这并不理想。有人能告诉我我漏掉了什么吗?

更新:我像Brad建议的那样关闭了所有的type-aware规则。然而,我现在得到的错误是:

TypeError: Cannot read property 'length' of undefined
Occurred while linting /path/to/src/app/app.component.html:1
    at getUseStrictDirectives (/path/to/node_modules/eslint/lib/rules/strict.js:27:36)

但是我的`app.component.html`只是这样的:


所以我又一次迷失了方向。

更新2:我终于成功了,根据Brad在他的答案中建议的,逐个关闭了模板解析器的规则。我的工作`overrides`块现在看起来像这样:

{
  files: ["*.component.html"],
  parser: "@angular-eslint/template-parser",
  rules: {
    "@typescript-eslint/dot-notation": "off",
    "@typescript-eslint/no-implied-eval": "off",
    "@typescript-eslint/no-throw-literal": "off",
    "strict": "off",
    "import/first": "off",
    "lines-around-directive": "off"
  },
  plugins: ["@angular-eslint/template"],
}

0
0 Comments

ESLint在尝试对Angular模板进行lint时出现错误的原因是由于配置文件中覆盖了用于解析Angular模板的解析器。这意味着你没有使用-eslint/parser来解析你的Angular模板。

原因是-eslint/parser无法理解Angular模板。然而,-eslint/template-parser也没有提供进行类型感知linting所需的基础设施。

因此,lint规则在你的文件上失败了。你需要在使用-eslint/template-parser解析的文件上禁用所有类型感知lint规则。

关于angular-eslint,我对它了解不多,但我认为项目的工作方式意味着你必须在Angular模板文件上禁用所有标准的eslint lint规则。

你是对的,我现在已经解决了这个问题,并相应地更新了我的答案。

0