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

How to disable warn about some unused params, but keep "@typescript-eslint/no-unused-vars" rule

我想在某些情况下禁用 no unused params 警告,但保留“unused vars”警告。

例如,在这里我想保留参数以查看传递给解析器的内容:

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() }
      },
    })
  },
})

我收到警告:

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

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/*'],
}

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

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

我发现的唯一方法是在规则选项中使用 ignore pattern argsIgnorePattern。如果您的变量未被使用,只需添加下划线 _ctx 并且 ESLint 将忽略它,但 no-unused-vars 规则仍然适用于其他值。在您需要使用此值后,只需删除下划线 ctx.

 // note you must disable the base rule as it can report incorrect errors
"no-unused-vars": "off",
'@typescript-eslint/no-unused-vars': [
  'warn', // or error
  { 
    argsIgnorePattern: '^_',
    varsIgnorePattern: '^_',
    caughtErrorsIgnorePattern: '^_',
  },
],

您可以根据需要使用 RegExp 更改此模式^_