首页 > 解决方案 > ESLint、TypeScript、Vue 和 WebPack Encore:无法禁用 ESLint 规则

问题描述

我花了几个小时研究 Webpack Encore 和 ESLint 问题,但我似乎找不到解决这个问题的方法。

我指定要更改或关闭一些 TypeScript-ESLint 规则,但没有任何反应。无论如何,我都会收到 linter 警告。我的配置如下所示:

.eslintrc.json

{
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "plugins": [
    "@typescript-eslint"
  ],
  "rules": {
    "@typescript-eslint/no-non-null-assertion": "off"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "ES2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "strict": true,
    "noImplicitAny": true,
    "esModuleInterop": true,
    "baseUrl": ".",
  },
  "include": [
    "assets/scripts/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}

以下是我的安可配置的相关部分:

Encore
  // ...
  .enableTypeScriptLoader()
  .enableForkedTypeScriptTypesChecking()
  .enableVueLoader()
  .enableEslintLoader((options) => {}, { lintVue: true })
  // ...
;

这是我收到的警告,尽管我禁用了规则:

/some/path/my-file.ts
  97:82  warning  Forbidden non-null assertion  @typescript-eslint/no-non-null-assertion

对我来说,这个 PR似乎应该解决我的问题,但事实并非如此。

你们有什么想法吗?我将非常感谢任何提示:)

编辑:

lotype和我的一个同事的帮助下,我终于找到了解决方案。有几件事必须改变。

首先,确保使用正确的解析器:

.enableEslintLoader((options) => {
  options.parser = require('./.eslintrc.json').parser;
  // https://webpack.js.org/loaders/eslint-loader/#cache
  options.cache = false; // optional, but recommended
}, { lintVue: true })

然后,确保将 TypeScript 规则添加到覆盖部分,并将“普通”规则添加到规则部分,如下所示:

{
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "plugins": [
    "@typescript-eslint"
  ],
  "rules": {
    "no-console": "warn"
  },
  "overrides": [
    {
      "files": [
        "*.ts"
      ],
      "rules": {
        "@typescript-eslint/no-non-null-assertion": "off",
        "@typescript-eslint/no-explicit-any": "off"
      }
    }
  ]
}

谢谢你们帮我解决这个问题,伙计们!

标签: typescriptvue.jswebpackeslintwebpack-encore

解决方案


您会尝试将其放入 .eslintrc.json 的覆盖部分吗?

  "overrides": [
    {
      "files": ["*.ts"],
      "rules": {
        "@typescript-eslint/no-non-null-assertion": "off"
      }
    }
  ]

编辑:根据规范需要“文件”


推荐阅读