Skip to content

Commit

Permalink
Add exception for generators in "return-undefined" rule (palantir#4370)
Browse files Browse the repository at this point in the history
* Add exception for generators in "return-undefined" rule

* Fix linter error
  • Loading branch information
ulrichb authored and Josh Goldberg committed Dec 12, 2018
1 parent 2729aac commit a21ba7d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/rules/returnUndefinedRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ function getReturnKind(node: FunctionLike, checker: ts.TypeChecker): ReturnKind
return ReturnKind.Value;
}

// Handle generator functions/methods:
if (node.asteriskToken !== undefined) {
return ReturnKind.Void;
}

const contextual =
isFunctionExpressionLike(node) && node.type === undefined
? tryGetReturnType(checker.getContextualType(node), checker)
Expand Down
14 changes: 14 additions & 0 deletions test/rules/return-undefined/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,19 @@ test(async () => {
~~~~~~~~~~~~~~~~~ [VOID]
});


function * generator(returnNothing: boolean) {
if (returnNothing) return;
yield 1;
return yield * [2, 3];
}

class ClassWithGeneratorMethod {
* generatorMethod(returnNothing: boolean) {
if (returnNothing) return;
yield 1;
}
}

[VOID]: `void` function should use `return;`, not `return undefined;`.
[UNDEFINED]: Value-returning function should use `return undefined;`, not just `return;`.

0 comments on commit a21ba7d

Please sign in to comment.