Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a94589

Browse files
JoshuaKGoldbergmdjermanovicamareshsm
authored
feat!: no-unused-vars default caughtErrors to 'all' (#18043)
* feat!: `no-unused-vars` default caughtErrors to 'all' * docs: update no-unused-vars.md * docs: mention caughtErrors in migrate-to-9.0.0.md * chore: typo fix in docs/src/use/migrate-to-9.0.0.md Co-authored-by: Milos Djermanovic <[email protected]> * chore: fix formatting in no-unused-vars.md * fix: 'are'/'were' in docs/src/use/migrate-to-9.0.0.md Co-authored-by: Amaresh S M <[email protected]> --------- Co-authored-by: Milos Djermanovic <[email protected]> Co-authored-by: Amaresh S M <[email protected]>
1 parent 857e242 commit 1a94589

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

docs/src/rules/no-unused-vars.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ var global_var = 42;
132132

133133
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).
134134

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

137137
```json
138138
{
139139
"rules": {
140-
"no-unused-vars": ["error", { "vars": "all", "args": "after-used", "caughtErrors": "none", "ignoreRestSiblings": false }]
140+
"no-unused-vars": ["error", { "vars": "all", "args": "after-used", "caughtErrors": "all", "ignoreRestSiblings": false }]
141141
}
142142
}
143143
```
@@ -283,20 +283,22 @@ The `caughtErrors` option is used for `catch` block arguments validation.
283283

284284
It has two settings:
285285

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

289-
#### caughtErrors: none
289+
#### caughtErrors: all
290290

291-
Not specifying this rule is equivalent of assigning it to `none`.
291+
Not specifying this option is equivalent of assigning it to `all`.
292292

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

295-
::: correct
295+
::: incorrect
296296

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

300+
// 1 error
301+
// "err" is defined but never used
300302
try {
301303
//...
302304
} catch (err) {
@@ -306,17 +308,15 @@ try {
306308

307309
:::
308310

309-
#### caughtErrors: all
311+
#### caughtErrors: none
310312

311-
Examples of **incorrect** code for the `{ "caughtErrors": "all" }` option:
313+
Examples of **correct** code for the `{ "caughtErrors": "none" }` option:
312314

313-
::: incorrect
315+
::: correct
314316

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

318-
// 1 error
319-
// "err" is defined but never used
320320
try {
321321
//...
322322
} catch (err) {

docs/src/use/migrate-to-9.0.0.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ The lists below are ordered roughly by the number of users each change is expect
3333
* [`no-restricted-imports` now accepts multiple config entries with the same `name`](#no-restricted-imports)
3434
* [`"eslint:recommended"` and `"eslint:all"` strings no longer accepted in flat config](#string-config)
3535
* [`no-inner-declarations` has a new default behavior with a new option](#no-inner-declarations)
36+
* [`no-unused-vars` now defaults `caughtErrors` to `"all"`](#no-unused-vars)
3637

3738
### Breaking changes for plugin developers
3839

@@ -362,6 +363,33 @@ if (test) {
362363

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

366+
## <a name="no-unused-vars"></a> `no-unused-vars` now defaults `caughtErrors` to `"all"`
367+
368+
ESLint v9.0.0 changes the default value for the `no-unused-vars` rule's `caughtErrors` option.
369+
Previously it defaulted to `"none"` to never check whether caught errors were used.
370+
It now defaults to `"all"` to check caught errors for being used.
371+
372+
```js
373+
/*eslint no-unused-vars: "error"*/
374+
try {}
375+
catch (error) {
376+
// 'error' is defined but never used
377+
}
378+
```
379+
380+
**To address:** If you want to allow unused caught errors, such as when writing code that will be directly run in an environment that does not support ES2019 optional catch bindings, set the `caughtErrors` option to `"none"`.
381+
Otherwise, delete the unused caught errors.
382+
383+
```js
384+
/*eslint no-unused-vars: "error"*/
385+
try {}
386+
catch {
387+
// no error
388+
}
389+
```
390+
391+
**Related issue(s):** [#17974](https://github.com/eslint/eslint/issues/17974)
392+
365393
## <a name="removed-context-methods"></a> Removed multiple `context` methods
366394

367395
ESLint v9.0.0 removes multiple deprecated methods from the `context` object and moves them onto the `SourceCode` object:

lib/rules/no-unused-vars.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module.exports = {
9292
vars: "all",
9393
args: "after-used",
9494
ignoreRestSiblings: false,
95-
caughtErrors: "none"
95+
caughtErrors: "all"
9696
};
9797

9898
const firstOption = context.options[0];

tests/lib/rules/no-unused-vars.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ ruleTester.run("no-unused-vars", rule, {
113113
"myFunc(function foo(){}.toString())",
114114
"function foo(first, second) {\ndoStuff(function() {\nconsole.log(second);});}; foo()",
115115
"(function() { var doSomething = function doSomething() {}; doSomething() }())",
116-
"try {} catch(e) {}",
117116
"/*global a */ a;",
118117
{ code: "var a=10; (function() { alert(a); })();", options: [{ vars: "all" }] },
119118
{ code: "function g(bar, baz) { return baz; }; g();", options: [{ vars: "all" }] },
@@ -283,13 +282,17 @@ ruleTester.run("no-unused-vars", rule, {
283282
{ code: "let x = 0; foo = (0, x = x + 1);", languageOptions: { ecmaVersion: 6 } },
284283

285284
// caughtErrors
285+
{
286+
code: "try{}catch(err){}",
287+
options: [{ caughtErrors: "none" }]
288+
},
286289
{
287290
code: "try{}catch(err){console.error(err);}",
288291
options: [{ caughtErrors: "all" }]
289292
},
290293
{
291-
code: "try{}catch(err){}",
292-
options: [{ caughtErrors: "none" }]
294+
code: "try{}catch(ignoreErr){}",
295+
options: [{ caughtErrorsIgnorePattern: "^ignore" }]
293296
},
294297
{
295298
code: "try{}catch(ignoreErr){}",
@@ -299,7 +302,7 @@ ruleTester.run("no-unused-vars", rule, {
299302
// caughtErrors with other combinations
300303
{
301304
code: "try{}catch(err){}",
302-
options: [{ vars: "all", args: "all" }]
305+
options: [{ caughtErrors: "none", vars: "all", args: "all" }]
303306
},
304307

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

11241127
// caughtErrors
1128+
{
1129+
code: "try{}catch(err){};",
1130+
errors: [definedError("err")]
1131+
},
11251132
{
11261133
code: "try{}catch(err){};",
11271134
options: [{ caughtErrors: "all" }],

0 commit comments

Comments
 (0)