From b5adcaab93f388f1d8e9d35d6f5e8c2994240850 Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Sat, 15 Feb 2020 00:54:34 +0900 Subject: [PATCH] Fix: make YieldExpression throwable (fixes #12880) (#12897) --- .../code-path-analysis/code-path-analyzer.js | 1 + tests/lib/rules/no-unreachable.js | 98 ++++++++++++++++++- 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/lib/linter/code-path-analysis/code-path-analyzer.js b/lib/linter/code-path-analysis/code-path-analyzer.js index 6822ae2be0a..8a623e33ea0 100644 --- a/lib/linter/code-path-analysis/code-path-analyzer.js +++ b/lib/linter/code-path-analysis/code-path-analyzer.js @@ -518,6 +518,7 @@ function processCodePathToExit(analyzer, node) { case "ImportExpression": case "MemberExpression": case "NewExpression": + case "YieldExpression": state.makeFirstThrowablePathInTryBlock(); break; diff --git a/tests/lib/rules/no-unreachable.js b/tests/lib/rules/no-unreachable.js index 1e3f7116126..e1609c8a811 100644 --- a/tests/lib/rules/no-unreachable.js +++ b/tests/lib/rules/no-unreachable.js @@ -47,7 +47,25 @@ ruleTester.run("no-unreachable", rule, { "function foo() { var x = 1; for (x in {}) { return; } x = 2; }", "function foo() { var x = 1; try { return; } finally { x = 2; } }", "function foo() { var x = 1; for (;;) { if (x) break; } x = 2; }", - "A: { break A; } foo()" + "A: { break A; } foo()", + { + code: "function* foo() { try { yield 1; return; } catch (err) { return err; } }", + parserOptions: { + ecmaVersion: 6 + } + }, + { + code: "function foo() { try { bar(); return; } catch (err) { return err; } }", + parserOptions: { + ecmaVersion: 6 + } + }, + { + code: "function foo() { try { a.b.c = 1; return; } catch (err) { return err; } }", + parserOptions: { + ecmaVersion: 6 + } + } ], invalid: [ { code: "function foo() { return x; var x = 1; }", errors: [{ messageId: "unreachableCode", type: "VariableDeclaration" }] }, @@ -206,6 +224,84 @@ ruleTester.run("no-unreachable", rule, { endColumn: 25 } ] + }, + { + code: ` + function* foo() { + try { + return; + } catch (err) { + return err; + } + }`, + parserOptions: { + ecmaVersion: 6 + }, + errors: [ + { + messageId: "unreachableCode", + type: "BlockStatement", + line: 5, + column: 35, + endLine: 7, + endColumn: 22 + } + ] + }, + { + code: ` + function foo() { + try { + return; + } catch (err) { + return err; + } + }`, + parserOptions: { + ecmaVersion: 6 + }, + errors: [ + { + messageId: "unreachableCode", + type: "BlockStatement", + line: 5, + column: 35, + endLine: 7, + endColumn: 22 + } + ] + }, + { + code: ` + function foo() { + try { + return; + let a = 1; + } catch (err) { + return err; + } + }`, + parserOptions: { + ecmaVersion: 6 + }, + errors: [ + { + messageId: "unreachableCode", + type: "VariableDeclaration", + line: 5, + column: 25, + endLine: 5, + endColumn: 35 + }, + { + messageId: "unreachableCode", + type: "BlockStatement", + line: 6, + column: 35, + endLine: 8, + endColumn: 22 + } + ] } ] });