Skip to content

Commit

Permalink
Don't warn for "missing return statement" in a generator function
Browse files Browse the repository at this point in the history
This fixes some errors seen when moving generator transpilation after CheckMissingReturn.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=191603599
  • Loading branch information
lauraharker committed Apr 4, 2018
1 parent 68e2fd4 commit 38c5d06
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/com/google/javascript/jscomp/CheckMissingReturn.java
Expand Up @@ -98,6 +98,12 @@ public void enterScope(NodeTraversal t) {
return;
}

if (n.isGeneratorFunction()) {
// Generator functions always return a Generator. No need to check return statements.
// TODO(b/73387406): Investigate adding a warning for generators with no yields.
return;
}

if (n.isArrowFunction()) {
Node functionBody = NodeUtil.getFunctionBody(n);
if (!functionBody.isNormalBlock()) {
Expand Down
22 changes: 22 additions & 0 deletions test/com/google/javascript/jscomp/CheckMissingReturnTest.java
Expand Up @@ -300,4 +300,26 @@ public void testArrowFunctions() {
// arrow function return is object literal
testSame("(a) => ({foo: 1});");
}

public void testGeneratorFunctionDoesntWarn() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
testNoWarning("function *gen() {}");

testNoWarning(
lines(
"/** @return {!Generator<number>} */", // no yields is OK
"function *gen() {}"));

testNoWarning(
lines(
"/** @return {!Generator<number>} */", // one yield is OK
"function *gen() {",
" yield 1;",
"}"));

testNoWarning(
lines(
"/** @return {!Object} */", // Return type more vague than Generator is also OK
"function *gen() {}"));
}
}

0 comments on commit 38c5d06

Please sign in to comment.