From 30ec664ce4ef22f2ff284425dfc863a882f2b1b4 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 27 Jan 2024 09:47:12 -0500 Subject: [PATCH 1/6] feat!: `no-unused-vars` default caughtErrors to 'all' --- lib/rules/no-unused-vars.js | 2 +- tests/lib/rules/no-unused-vars.js | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/rules/no-unused-vars.js b/lib/rules/no-unused-vars.js index 5ccbff7617b..74a664089ed 100644 --- a/lib/rules/no-unused-vars.js +++ b/lib/rules/no-unused-vars.js @@ -92,7 +92,7 @@ module.exports = { vars: "all", args: "after-used", ignoreRestSiblings: false, - caughtErrors: "none" + caughtErrors: "all" }; const firstOption = context.options[0]; diff --git a/tests/lib/rules/no-unused-vars.js b/tests/lib/rules/no-unused-vars.js index 01118f2e48e..0d8e7c33b89 100644 --- a/tests/lib/rules/no-unused-vars.js +++ b/tests/lib/rules/no-unused-vars.js @@ -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" }] }, @@ -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){}", @@ -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 @@ -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" }], From 1b965cdf94872d2974956bf38c5db08635840087 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 28 Jan 2024 02:28:52 -0500 Subject: [PATCH 2/6] docs: update no-unused-vars.md --- docs/src/rules/no-unused-vars.md | 34 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/src/rules/no-unused-vars.md b/docs/src/rules/no-unused-vars.md index bca9c06d791..26fc390865d 100644 --- a/docs/src/rules/no-unused-vars.md +++ b/docs/src/rules/no-unused-vars.md @@ -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 }] } } ``` @@ -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 +::: -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) { @@ -306,17 +310,15 @@ 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) { @@ -324,8 +326,6 @@ try { } ``` -::: - ### 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'. From 87e809f225c93a91d740c7cca3656f7fd9164b71 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 28 Jan 2024 02:53:23 -0500 Subject: [PATCH 3/6] docs: mention caughtErrors in migrate-to-9.0.0.md --- docs/src/use/migrate-to-9.0.0.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/docs/src/use/migrate-to-9.0.0.md b/docs/src/use/migrate-to-9.0.0.md index 7e0616c0d1a..f1f5ac64a3c 100644 --- a/docs/src/use/migrate-to-9.0.0.md +++ b/docs/src/use/migrate-to-9.0.0.md @@ -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) ### Breaking changes for plugin developers @@ -362,6 +363,33 @@ if (test) { **Related issue(s):** [#15576](https://github.com/eslint/eslint/issues/15576) +## `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. +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"`. +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) + ## Removed multiple `context` methods ESLint v9.0.0 removes multiple deprecated methods from the `context` object and moves them onto the `SourceCode` object: From 78653832349572b63aa256ed60616e42aa07af3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Tue, 30 Jan 2024 14:24:52 -0500 Subject: [PATCH 4/6] chore: typo fix in docs/src/use/migrate-to-9.0.0.md Co-authored-by: Milos Djermanovic --- docs/src/use/migrate-to-9.0.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/use/migrate-to-9.0.0.md b/docs/src/use/migrate-to-9.0.0.md index f1f5ac64a3c..a0b8d4bedcc 100644 --- a/docs/src/use/migrate-to-9.0.0.md +++ b/docs/src/use/migrate-to-9.0.0.md @@ -377,7 +377,7 @@ catch (error) { } ``` -**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"`. +**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"`. Otherwise, delete the unused caught errors. ```js From 9875cbbcac4e5ef9a00261a2baf19f91dbb5b32b Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 30 Jan 2024 14:27:40 -0500 Subject: [PATCH 5/6] chore: fix formatting in no-unused-vars.md --- docs/src/rules/no-unused-vars.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/rules/no-unused-vars.md b/docs/src/rules/no-unused-vars.md index 26fc390865d..d924f02d657 100644 --- a/docs/src/rules/no-unused-vars.md +++ b/docs/src/rules/no-unused-vars.md @@ -286,11 +286,9 @@ It has two settings: * `all` - all named arguments must be used. This is the default setting. * `none` - do not check error objects. -::: - #### caughtErrors: all -Not specifying this rule is equivalent of assigning it to `all`. +Not specifying this option is equivalent of assigning it to `all`. Examples of **incorrect** code for the `{ "caughtErrors": "all" }` option: @@ -326,6 +324,8 @@ try { } ``` +::: + ### 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'. From 0b6d01cb88149ee5597245f49810020ac2422f2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Wed, 31 Jan 2024 18:51:25 -0500 Subject: [PATCH 6/6] fix: 'are'/'were' in docs/src/use/migrate-to-9.0.0.md Co-authored-by: Amaresh S M --- docs/src/use/migrate-to-9.0.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/use/migrate-to-9.0.0.md b/docs/src/use/migrate-to-9.0.0.md index a0b8d4bedcc..7f45ddf2492 100644 --- a/docs/src/use/migrate-to-9.0.0.md +++ b/docs/src/use/migrate-to-9.0.0.md @@ -366,7 +366,7 @@ if (test) { ## `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. +Previously it defaulted to `"none"` to never check whether caught errors were used. It now defaults to `"all"` to check caught errors for being used. ```js