Skip to content

Commit 14e9f81

Browse files
authored
fix: destructuring in catch clause in no-unused-vars (#18636)
* fix: destructuring in catch clause in `no-unused-vars` * revert changes in `lib/rules/no-unused-vars.js` * temporarily use repo branch for eslint-scope * Update package.json
1 parent 7bcda76 commit 14e9f81

3 files changed

Lines changed: 75 additions & 2 deletions

File tree

lib/rules/no-unused-vars.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ module.exports = {
340340
/**
341341
* Determines if a variable has a sibling rest property
342342
* @param {Variable} variable eslint-scope variable object.
343-
* @returns {boolean} True if the variable is exported, false if not.
343+
* @returns {boolean} True if the variable has a sibling rest property, false if not.
344344
* @private
345345
*/
346346
function hasRestSpreadSibling(variable) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"cross-spawn": "^7.0.2",
8080
"debug": "^4.3.2",
8181
"escape-string-regexp": "^4.0.0",
82-
"eslint-scope": "^8.0.1",
82+
"eslint-scope": "^8.0.2",
8383
"eslint-visitor-keys": "^4.0.0",
8484
"espree": "^10.1.0",
8585
"esquery": "^1.5.0",

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ ruleTester.run("no-unused-vars", rule, {
270270
options: [{ destructuredArrayIgnorePattern: "^_", ignoreRestSiblings: true }],
271271
languageOptions: { ecmaVersion: 2018 }
272272
},
273+
{
274+
code: "try {} catch ([firstError]) {}",
275+
options: [{ destructuredArrayIgnorePattern: "Error$" }],
276+
languageOptions: { ecmaVersion: 2015 }
277+
},
273278

274279
// for-in loops (see #2342)
275280
"(function(obj) { var name; for ( name in obj ) return; })({});",
@@ -316,6 +321,16 @@ ruleTester.run("no-unused-vars", rule, {
316321
code: "try{}catch(ignoreErr){}",
317322
options: [{ caughtErrors: "all", caughtErrorsIgnorePattern: "^ignore" }]
318323
},
324+
{
325+
code: "try {} catch ({ message, stack }) {}",
326+
options: [{ caughtErrorsIgnorePattern: "message|stack" }],
327+
languageOptions: { ecmaVersion: 2015 }
328+
},
329+
{
330+
code: "try {} catch ({ errors: [firstError] }) {}",
331+
options: [{ caughtErrorsIgnorePattern: "Error$" }],
332+
languageOptions: { ecmaVersion: 2015 }
333+
},
319334

320335
// caughtErrors with other combinations
321336
{
@@ -329,6 +344,11 @@ ruleTester.run("no-unused-vars", rule, {
329344
options: [{ ignoreRestSiblings: true }],
330345
languageOptions: { ecmaVersion: 2018 }
331346
},
347+
{
348+
code: "try {} catch ({ foo, ...bar }) { console.log(bar); }",
349+
options: [{ ignoreRestSiblings: true }],
350+
languageOptions: { ecmaVersion: 2018 }
351+
},
332352

333353
// https://github.com/eslint/eslint/issues/6348
334354
"var a = 0, b; b = a = a + 1; foo(b);",
@@ -1674,6 +1694,30 @@ c = foo1`,
16741694
options: [{ caughtErrors: "all", caughtErrorsIgnorePattern: "^_", reportUsedIgnorePattern: true }],
16751695
errors: [usedIgnoredError("_err", ". Used caught errors must not match /^_/u")]
16761696
},
1697+
{
1698+
code: "try {} catch ({ message }) { console.error(message); }",
1699+
options: [{ caughtErrorsIgnorePattern: "message", reportUsedIgnorePattern: true }],
1700+
languageOptions: { ecmaVersion: 2015 },
1701+
errors: [usedIgnoredError("message", ". Used caught errors must not match /message/u")]
1702+
},
1703+
{
1704+
code: "try {} catch ([_a, _b]) { doSomething(_a, _b); }",
1705+
options: [{ caughtErrorsIgnorePattern: "^_", reportUsedIgnorePattern: true }],
1706+
languageOptions: { ecmaVersion: 6 },
1707+
errors: [
1708+
usedIgnoredError("_a", ". Used caught errors must not match /^_/u"),
1709+
usedIgnoredError("_b", ". Used caught errors must not match /^_/u")
1710+
]
1711+
},
1712+
{
1713+
code: "try {} catch ([_a, _b]) { doSomething(_a, _b); }",
1714+
options: [{ destructuredArrayIgnorePattern: "^_", reportUsedIgnorePattern: true }],
1715+
languageOptions: { ecmaVersion: 6 },
1716+
errors: [
1717+
usedIgnoredError("_a", ". Used elements of array destructuring must not match /^_/u"),
1718+
usedIgnoredError("_b", ". Used elements of array destructuring must not match /^_/u")
1719+
]
1720+
},
16771721
{
16781722
code: `
16791723
try {
@@ -1705,6 +1749,35 @@ try {
17051749
}
17061750
]
17071751
},
1752+
{
1753+
code: "try {} catch ({ message, errors: [firstError] }) {}",
1754+
options: [{ caughtErrorsIgnorePattern: "foo" }],
1755+
languageOptions: { ecmaVersion: 2015 },
1756+
errors: [
1757+
{
1758+
message: "'message' is defined but never used. Allowed unused caught errors must match /foo/u.",
1759+
column: 17,
1760+
endColumn: 24
1761+
},
1762+
{
1763+
message: "'firstError' is defined but never used. Allowed unused caught errors must match /foo/u.",
1764+
column: 35,
1765+
endColumn: 45
1766+
}
1767+
]
1768+
},
1769+
{
1770+
code: "try {} catch ({ stack: $ }) { $ = 'Something broke: ' + $; }",
1771+
options: [{ caughtErrorsIgnorePattern: "\\w" }],
1772+
languageOptions: { ecmaVersion: 2015 },
1773+
errors: [
1774+
{
1775+
message: "'$' is assigned a value but never used. Allowed unused caught errors must match /\\w/u.",
1776+
column: 31,
1777+
endColumn: 32
1778+
}
1779+
]
1780+
},
17081781
{
17091782
code: `
17101783
_ => { _ = _ + 1 };

0 commit comments

Comments
 (0)