ZiiMakc
ZiiMakc

Reputation: 36826

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'],

Upvotes: 169

Views: 98224

Answers (4)

Ninjaxor
Ninjaxor

Reputation: 998

Because unused parameters are useful for educations purposes and reduce research time involved in writing code, I've adopted this simple rule that fixes the squigly lines in parameters, but leaves them for variables I just finished typing.

{
    rules: {
        "@typescript-eslint/no-unused-vars": [
            "error",
            {
                "args": "none"
            }
        ]
    }
}

Upvotes: -1

theberzi
theberzi

Reputation: 2645

In the spirit of Nathan Gouy's answer, here's how to ignore unused symbols starting with exactly one underscore, but still warn if they start with more than one:

{
  rules: {
    // Allow unused variables starting with exactly one underscore.
    "no-unused-vars": "off",
    "@typescript-eslint/no-unused-vars": [
      "warn",
      {
        "argsIgnorePattern": "^_[^_].*$|^_$",
        "varsIgnorePattern": "^_[^_].*$|^_$",
        "caughtErrorsIgnorePattern": "^_[^_].*$|^_$"
      }
    ]
  }
}

How it works

The pattern here ignores anything that starts with _ and continues with something that is not another _ (_test, for example, but not __test nor even __), and additionally ignores the exact pattern _.

Upvotes: 6

Nathan Gouy
Nathan Gouy

Reputation: 1412

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": "^_"
      }
    ]
  }
}

Upvotes: 8

ZiiMakc
ZiiMakc

Reputation: 36826

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'

Upvotes: 387

Related Questions