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

feat!: no-unused-vars default caughtErrors to 'all' #18043

Merged
34 changes: 17 additions & 17 deletions docs/src/rules/no-unused-vars.md
Expand Up @@ -132,12 +132,12 @@ var global_var = 42;

This rule takes one argument which can be a string or an object. The string settings are the same as those of the `vars` property (explained below).

By default this rule is enabled with `all` option for variables and `after-used` for arguments.
By default this rule is enabled with `all` option for caught errors and variables, and `after-used` for arguments.

```json
{
"rules": {
"no-unused-vars": ["error", { "vars": "all", "args": "after-used", "caughtErrors": "none", "ignoreRestSiblings": false }]
"no-unused-vars": ["error", { "vars": "all", "args": "after-used", "caughtErrors": "all", "ignoreRestSiblings": false }]
}
}
```
Expand Down Expand Up @@ -283,20 +283,24 @@ The `caughtErrors` option is used for `catch` block arguments validation.

It has two settings:

* `none` - do not check error objects. This is the default setting.
* `all` - all named arguments must be used.
* `all` - all named arguments must be used. This is the default setting.
* `none` - do not check error objects.

#### caughtErrors: none
:::
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

Not specifying this rule is equivalent of assigning it to `none`.
#### caughtErrors: all

Examples of **correct** code for the `{ "caughtErrors": "none" }` option:
Not specifying this rule is equivalent of assigning it to `all`.

::: correct
Examples of **incorrect** code for the `{ "caughtErrors": "all" }` option:

::: incorrect

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

// 1 error
// "err" is defined but never used
try {
//...
} catch (err) {
Expand All @@ -306,26 +310,22 @@ try {

:::

#### caughtErrors: all
#### caughtErrors: none

Examples of **incorrect** code for the `{ "caughtErrors": "all" }` option:
Examples of **correct** code for the `{ "caughtErrors": "none" }` option:

::: incorrect
::: correct

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

// 1 error
// "err" is defined but never used
try {
//...
} catch (err) {
console.error("errors");
}
```

:::

### caughtErrorsIgnorePattern

The `caughtErrorsIgnorePattern` option specifies exceptions not to check for usage: catch arguments whose names match a regexp pattern. For example, variables whose names begin with a string 'ignore'.
Expand Down
28 changes: 28 additions & 0 deletions docs/src/use/migrate-to-9.0.0.md
Expand Up @@ -33,6 +33,7 @@ The lists below are ordered roughly by the number of users each change is expect
* [`no-restricted-imports` now accepts multiple config entries with the same `name`](#no-restricted-imports)
* [`"eslint:recommended"` and `"eslint:all"` strings no longer accepted in flat config](#string-config)
* [`no-inner-declarations` has a new default behavior with a new option](#no-inner-declarations)
* [`no-unused-vars` now defaults `caughtErrors` to `"all"`](#no-unused-vars)
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

As an aside, it would be nice in a separate PR to group all default rule option changes together in this list or a sub-list.


### Breaking changes for plugin developers

Expand Down Expand Up @@ -362,6 +363,33 @@ if (test) {

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

## <a name="no-unused-vars"></a> `no-unused-vars` now defaults `caughtErrors` to `"all"`

ESLint v9.0.0 changes the default value for the `no-unused-vars` rule's `caughtErrors` option.
Previously it defaulted to `"none"` to never check whether caught errors are used.
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
It now defaults to `"all"` to check caught errors for being used.

```js
/*eslint no-unused-vars: "error"*/
try {}
catch (error) {
// 'error' is defined but never used
}
```

**To address:** If you want to allow unused caught errors, such as when writing code that will be directly run in a environment that does not support ES2019 optional catch bindings, set the `caughtErrors` option to `"none"`.
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
Otherwise, delete the unused caught errors.

```js
/*eslint no-unused-vars: "error"*/
try {}
catch {
// no error
}
```

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

## <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
2 changes: 1 addition & 1 deletion lib/rules/no-unused-vars.js
Expand Up @@ -92,7 +92,7 @@ module.exports = {
vars: "all",
args: "after-used",
ignoreRestSiblings: false,
caughtErrors: "none"
caughtErrors: "all"
};

const firstOption = context.options[0];
Expand Down
15 changes: 11 additions & 4 deletions tests/lib/rules/no-unused-vars.js
Expand Up @@ -113,7 +113,6 @@ ruleTester.run("no-unused-vars", rule, {
"myFunc(function foo(){}.toString())",
"function foo(first, second) {\ndoStuff(function() {\nconsole.log(second);});}; foo()",
"(function() { var doSomething = function doSomething() {}; doSomething() }())",
"try {} catch(e) {}",
"/*global a */ a;",
{ code: "var a=10; (function() { alert(a); })();", options: [{ vars: "all" }] },
{ code: "function g(bar, baz) { return baz; }; g();", options: [{ vars: "all" }] },
Expand Down Expand Up @@ -283,13 +282,17 @@ ruleTester.run("no-unused-vars", rule, {
{ code: "let x = 0; foo = (0, x = x + 1);", languageOptions: { ecmaVersion: 6 } },

// caughtErrors
{
code: "try{}catch(err){}",
options: [{ caughtErrors: "none" }]
},
{
code: "try{}catch(err){console.error(err);}",
options: [{ caughtErrors: "all" }]
},
{
code: "try{}catch(err){}",
options: [{ caughtErrors: "none" }]
code: "try{}catch(ignoreErr){}",
options: [{ caughtErrorsIgnorePattern: "^ignore" }]
},
{
code: "try{}catch(ignoreErr){}",
Expand All @@ -299,7 +302,7 @@ ruleTester.run("no-unused-vars", rule, {
// caughtErrors with other combinations
{
code: "try{}catch(err){}",
options: [{ vars: "all", args: "all" }]
options: [{ caughtErrors: "none", vars: "all", args: "all" }]
},

// Using object rest for variable omission
Expand Down Expand Up @@ -1122,6 +1125,10 @@ ruleTester.run("no-unused-vars", rule, {
},

// caughtErrors
{
code: "try{}catch(err){};",
errors: [definedError("err")]
},
{
code: "try{}catch(err){};",
options: [{ caughtErrors: "all" }],
Expand Down