Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: no-unused-vars varsIgnorePattern behavior with catch arguments #17932

Merged
merged 5 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/src/rules/no-unused-vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ By default this rule is enabled with `all` option for variables and `after-used`
```json
{
"rules": {
"no-unused-vars": ["error", { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }]
"no-unused-vars": ["error", { "vars": "all", "args": "after-used", "caughtErrors": "none", "ignoreRestSiblings": false }]
}
}
```
Expand All @@ -144,7 +144,7 @@ By default this rule is enabled with `all` option for variables and `after-used`

The `vars` option has two settings:

* `all` checks all variables for usage, including those in the global scope. This is the default setting.
* `all` checks all variables for usage, including those in the global scope. However, it excludes variables targeted by other options like `args` and `caughtErrors`. This is the default setting.
* `local` checks only that locally-declared variables are used but will allow global variables to be unused.

#### vars: local
Expand All @@ -164,7 +164,7 @@ some_unused_var = 42;

### varsIgnorePattern

The `varsIgnorePattern` option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain `ignored` or `Ignored`.
The `varsIgnorePattern` option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain `ignored` or `Ignored`. However, it excludes variables targeted by other options like `argsIgnorePattern` and `caughtErrorsIgnorePattern`.

Examples of **correct** code for the `{ "varsIgnorePattern": "[iI]gnored" }` option:

Expand Down Expand Up @@ -333,7 +333,7 @@ Examples of **correct** code for the `{ "caughtErrorsIgnorePattern": "^ignore" }
::: correct

```js
/*eslint no-unused-vars: ["error", { "caughtErrorsIgnorePattern": "^ignore" }]*/
/*eslint no-unused-vars: ["error", { "caughtErrors": "all", "caughtErrorsIgnorePattern": "^ignore" }]*/
snitin315 marked this conversation as resolved.
Show resolved Hide resolved

try {
//...
Expand Down
20 changes: 20 additions & 0 deletions docs/src/use/migrate-to-9.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The lists below are ordered roughly by the number of users each change is expect
* [Case-sensitive flags in `no-invalid-regexp`](#no-invalid-regexp)
* [Stricter `/* exported */` parsing](#exported-parsing)
* [`"eslint:recommended"` and `"eslint:all"` strings no longer accepted in flat config](#string-config)
* [`varsIgnorePattern` option of `no-unused-vars` no longer applies to catch arguments](#vars-ignore-pattern)

### Breaking changes for plugin developers

Expand Down Expand Up @@ -236,6 +237,25 @@ export default [

**Related issue(s):** [#17488](https://github.com/eslint/eslint/issues/17488)

## <a name="vars-ignore-pattern"></a> `varsIgnorePattern` option of `no-unused-vars` no longer applies to catch arguments

In previous versions of ESLint, the `varsIgnorePattern` option of `no-unused-vars` incorrectly ignored errors specified in a `catch` clause. In ESLint v9.0.0, `varsIgnorePattern` no longer applies to errors in `catch` clauses. For example:

```js
/*eslint no-unused-vars: ["error", { "caughtErrors": "all", "varsIgnorePattern": "^err" }]*/

try {
//...
} catch (err) { // 'err' will be reported.
console.error("errors");
}

```

**To address:** If you want to specify ignore patterns for `catch` clause variable names, use the `caughtErrorsIgnorePattern` option in addition to `varsIgnorePattern`.

**Related issue(s):** [#17540](https://github.com/eslint/eslint/issues/17540)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**Related issue(s):** [#17540](https://github.com/eslint/eslint/issues/17540)
**To address:** If you want to specify ignore patterns for `catch` clause variable names, use the `caughtErrorsIgnorePattern` option in addition to `varsIgnorePattern`.
**Related issue(s):** [#17540](https://github.com/eslint/eslint/issues/17540)


## <a name="removed-context-methods"></a> Removed multiple `context` methods

ESLint v9.0.0 removes multiple deprecated methods from the `context` object and moves them onto the `SourceCode` object:
Expand Down
6 changes: 2 additions & 4 deletions lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ module.exports = {
} else if (defType === "Parameter" && config.argsIgnorePattern) {
type = "args";
pattern = config.argsIgnorePattern.toString();
} else if (defType !== "Parameter" && config.varsIgnorePattern) {
} else if (defType !== "Parameter" && defType !== "CatchClause" && config.varsIgnorePattern) {
type = "vars";
pattern = config.varsIgnorePattern.toString();
}
Expand Down Expand Up @@ -623,9 +623,7 @@ module.exports = {
if (config.caughtErrorsIgnorePattern && config.caughtErrorsIgnorePattern.test(def.name.name)) {
continue;
}
}

if (type === "Parameter") {
} else if (type === "Parameter") {
snitin315 marked this conversation as resolved.
Show resolved Hide resolved

// skip any setter argument
if ((def.node.parent.type === "Property" || def.node.parent.type === "MethodDefinition") && def.node.parent.kind === "set") {
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,16 @@ ruleTester.run("no-unused-vars", rule, {
options: [{ caughtErrors: "all", caughtErrorsIgnorePattern: "^ignore" }],
errors: [definedError("err", ". Allowed unused args must match /^ignore/u")]
},
{
code: "try{}catch(err){};",
options: [{ caughtErrors: "all", varsIgnorePattern: "^err" }],
errors: [definedError("err")]
},
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
{
code: "try{}catch(err){};",
options: [{ caughtErrors: "all", varsIgnorePattern: "^." }],
errors: [definedError("err")]
},

// multiple try catch with one success
{
Expand Down