From de79f1026b7035f0296d7876f1db64f225cca1b8 Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Tue, 25 Dec 2018 14:59:31 -0500 Subject: [PATCH] Fix: handle optional catch bindings in no-useless-catch (#11205) This commit prevents a crash in the no-useless-catch rule that occurs when optional catch bindings are used. --- lib/rules/no-useless-catch.js | 1 + tests/lib/rules/no-useless-catch.js | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/rules/no-useless-catch.js b/lib/rules/no-useless-catch.js index 3211ed2c736..e4284cfb4f4 100644 --- a/lib/rules/no-useless-catch.js +++ b/lib/rules/no-useless-catch.js @@ -27,6 +27,7 @@ module.exports = { return { CatchClause(node) { if ( + node.param && node.param.type === "Identifier" && node.body.body.length && node.body.body[0].type === "ThrowStatement" && diff --git a/tests/lib/rules/no-useless-catch.js b/tests/lib/rules/no-useless-catch.js index 8c1a7118e20..93989a40352 100644 --- a/tests/lib/rules/no-useless-catch.js +++ b/tests/lib/rules/no-useless-catch.js @@ -103,6 +103,16 @@ ruleTester.run("no-useless-catch", rule, { } `, parserOptions: { ecmaVersion: 8 } + }, + { + code: ` + try { + throw new Error('foo'); + } catch { + throw new Error('foo'); + } + `, + parserOptions: { ecmaVersion: 2019 } } ],