Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" warning.

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'],
like image 218
ZiiMakc Avatar asked Sep 24 '20 18:09

ZiiMakc


People also ask

How do I disable no-unused-vars ESLint?

To apply the ESLint no-unused-vars rule to a block of JavaScript code, we can wrap the code block that we want to apply the rule to with /* eslint-disable no-unused-vars */ and /* eslint-enable no-unused-vars */ respectively. to wrap the code that we want the rule to apply with the comments.

How do you remove unused variables from ESLint?

To automatically remove unused imports, we will need to add the eslint-plugin-unused-imports plugin. Now, when you run ESLint, you should see error lines saying error '<imported-var>' is defined but never used unused-imports/no-unused-imports for the files where you have unused imports.

How do I disable typescript ESLint error?

If you are using ESLint and you need to disable the next line, use this code: eslint-disable-next-line.


1 Answers

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. After 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.

like image 86
ZiiMakc Avatar answered Sep 21 '22 11:09

ZiiMakc