如何禁用有关某些未使用参数的警告,但保留“@typescript-eslint/no-unused-vars”规则

RTW*_*RTW 11 eslint

我想禁用未使用的参数警告,但保留“未使用的变量”警告。

对于此代码:

const Query = objectType({
  name: 'Query',
  definition(t) {
    t.field('test', {
      type: 'Test',
      resolve: (root, args, ctx) => {
        const x = 1

        return { id: 1, time: new Date().toString() }
      },
    })
  },
})
Run Code Online (Sandbox Code Playgroud)

我收到警告:

  26:17  warning  'root' is defined but never used        @typescript-eslint/no-unused-vars
  26:23  warning  'args' is defined but never used        @typescript-eslint/no-unused-vars
  26:29  warning  'ctx' is defined but never used         @typescript-eslint/no-unused-vars
  27:15  warning  'x' is assigned a value but never used  @typescript-eslint/no-unused-vars
Run Code Online (Sandbox Code Playgroud)

eslint 配置:

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  parserOptions: { ecmaVersion: 2020, ecmaFeatures: { jsx: true } },
  env: {
    browser: true,
    node: true,
  },
  extends: ['plugin:react-hooks/recommended', 'eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:react/recommended'],
  settings: {
    react: {
      version: 'detect',
    },
  },
  rules: {
    '@typescript-eslint/no-empty-function': 'off',
    'react/react-in-jsx-scope': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
    'react/prop-types': 'off',
    '@typescript-eslint/no-var-requires': 'off',
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    'no-unused-vars': 'off',
    '@typescript-eslint/no-unused-vars': ['off'],
  },
  ignorePatterns: ['**/generated/*'],
}
Run Code Online (Sandbox Code Playgroud)

我试图以某种方式禁用它,但发现只有这个选项可以禁用所有内容:

'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': ['off'],

RTW*_*RTW 26

我发现的唯一方法是argsIgnorePattern在规则选项中使用忽略模式。如果您的变量未使用,只需添加下划线_ctx,eslint 将忽略它,但no-unused-vars规则仍然适用于其他值。

 '@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }],
Run Code Online (Sandbox Code Playgroud)

您可以^_根据需要使用正则表达式更改此模式。

  • 要忽略以下划线开头的未使用变量,请使用“varsIgnorePattern”而不是“argsIgnorePattern”。 (12认同)

Nat*_*ouy 5

To add to ZiiMakc's solution, to allow _ exclusively for unused vars, and still allow __ for 'private' used vars:

here is a link for sandbox

{
  "rules": {
    "@typescript-eslint/naming-convention": [
      "error",
      {
        "selector": [
          "parameter",
          "variable"
        ],
        "leadingUnderscore": "require",
        "format": ["camelCase"],
        "modifiers": [
          "unused"
        ]
      },
      {
        "selector": [
          "parameter",
          "variable"
        ],
        "leadingUnderscore": "allowDouble",
        "format": [
          "camelCase"
        ]
      }
    ],
    "@typescript-eslint/no-unused-vars": [
      "error",
      {
        "args": "all",
        "argsIgnorePattern": "^_",
        "varsIgnorePattern": "^_",
        "caughtErrorsIgnorePattern": "^_"
      }
    ]
  }
}

Run Code Online (Sandbox Code Playgroud)