簡體   English   中英

ESLint、TypeScript、Vue 和 WebPack Encore:無法禁用 ESLint 規則

[英]ESLint, TypeScript, Vue and WebPack Encore: Cannot disable ESLint rule

我花了幾個小時研究 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"
      }
    }
  ]
}

謝謝你們幫我解決這個問題,伙計們!

您會嘗試將其放入 your.eslintrc.json 的覆蓋部分嗎?

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

編輯:根據規范需要“文件”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM