Skip to content

Commit

Permalink
Fix: make YieldExpression throwable (fixes #12880) (#12897)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan committed Feb 14, 2020
1 parent 4293229 commit b5adcaa
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/linter/code-path-analysis/code-path-analyzer.js
Expand Up @@ -518,6 +518,7 @@ function processCodePathToExit(analyzer, node) {
case "ImportExpression":
case "MemberExpression":
case "NewExpression":
case "YieldExpression":
state.makeFirstThrowablePathInTryBlock();
break;

Expand Down
98 changes: 97 additions & 1 deletion tests/lib/rules/no-unreachable.js
Expand Up @@ -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" }] },
Expand Down Expand Up @@ -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
}
]
}
]
});

0 comments on commit b5adcaa

Please sign in to comment.