如何将allow:选项与eslintrc.js一起使用

Ale*_*Dim 3 javascript typescript eslint eslint-config-airbnb eslintrc

我有以下情况eslintrc.js

module.exports = {
  extends: ['airbnb-typescript/base'],
  parserOptions: {
    project: './tsconfig.json',
    sourceType: 'module',
  },
  ignorePatterns: ['*.js'],
  rules: {
    'no-underscore-dangle': 0,
    ...
  }
};
Run Code Online (Sandbox Code Playgroud)

我想包括一些例外情况allow: [ /** */ ]。但是每次当我将其添加为文件中的 key: value 属性时,ESLint 都会返回一个错误,其中包含以下文本:unable to use allow on top level。那么问题来了,如何用allow来达到这样的结果呢?

长话短说,我无法将我的规则集与 MongoDB_id命名一起使用,因此我使用 ESLint 仅忽略_id变量命名,而不是禁用命名约定规则本身。

有什么办法可以解决这个问题吗?

小智 5

这个对我有用:

"no-underscore-dangle": ["error", { allow: ["_id"] }]
Run Code Online (Sandbox Code Playgroud)

如果您正在使用该@typescript-eslint/naming-convention规则,您可能还需要添加以下内容:

"@typescript-eslint/naming-convention": [
  "error",
  {
    selector: ["variable"],
    format: ["strictCamelCase", "PascalCase", "UPPER_CASE"],
    filter: {
      regex: "^_id$",
      match: false,
    },
  },
],
Run Code Online (Sandbox Code Playgroud)