107

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'],
ZiiMakc
  • 31,187
  • 24
  • 65
  • 105

1 Answers1

244

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.

 // 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'
ZiiMakc
  • 31,187
  • 24
  • 65
  • 105
  • 10
    To ignore unused variables that begin with an underscore use `varsIgnorePattern` instead of `argsIgnorePattern`. – haymez Jul 08 '21 at 14:33
  • @uladzimir it should work: https://eslint.org/docs/rules/no-unused-vars#argsignorepattern note that you mast disable basic rule if you use typescript one – ZiiMakc Nov 05 '21 at 15:37
  • the "issue" though is that all of a sudden, if you use it, it won't warn you that you are using a variable that is supposed to stay unused (and thus you should probably rename | remove the `_`) – Nathan Gouy Feb 10 '23 at 13:40
  • 1
    @NathanGouy there is no problem in using variable named `_a` as it only marked like that for eslint and it will be hard not to see underscore when reviewing or coding. – ZiiMakc May 18 '23 at 13:28
  • Where does this file live? – Antonio Jul 08 '23 at 15:02
  • 1
    @Antonio in eslint congig rules: https://eslint.org/docs/latest/use/configure/configuration-files#using-configuration-files – ZiiMakc Jul 09 '23 at 11:06