Search code examples
eslinttypescript-eslint

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


I want to disable no unused params warning in some cases but keep "unused vars" rule.

For example here I would want to leave arguments in place to see what is passed to resolver:

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

I get warnings:

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 config:

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

I was trying to disable it somehow, but found only this option that disables everything:

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

Solution

  • Only way that I found is to use ignore pattern argsIgnorePattern in rule options. If your variable is unused, just add underscore _ctx and ESLint will ignore it, but no-unused-vars rule will still work for other values. When you will need to use this value, just remove underscore ctx.

    // .eslintrc.json
    {
      // ...
      "rules": {
        // 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": "^_"
          }
        ]
      }
    }
    

    You can change this pattern ^_ as you like using RegExp.

    Example:

    const _a = 'unused, with underscore, no warning'
    const b = 'unused, no underscore, warning'