【问题标题】:ESLint, TypeScript, Vue and WebPack Encore: Cannot disable ESLint ruleESLint、TypeScript、Vue 和 WebPack Encore:无法禁用 ESLint 规则
【发布时间】:2020-11-10 22:49:03
【问题描述】:

我花了几个小时研究 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 配置的相关部分:

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

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

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

编辑:

通过the help of 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"
      }
    }
  ]
}

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

【问题讨论】:

    标签: typescript vue.js webpack eslint webpack-encore


    【解决方案1】:

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

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

    编辑:根据specification 需要“文件”

    【讨论】:

    • 遗憾的是,事实并非如此。不过,感谢您的想法,值得一试!
    • 您的回答是解决方案的一部分,非常感谢您!我会将其作为公认的答案。任何有同样问题的人:请查看我的问题编辑!
    • 很高兴能帮上忙!
    猜你喜欢
    • 2019-07-16
    • 2021-10-16
    • 2021-04-14
    • 2023-03-14
    • 1970-01-01
    • 2020-06-04
    • 2019-06-25
    • 2020-02-15
    • 2021-06-18
    相关资源
    最近更新 更多