Skip to content

fix noImplicitReturns check when strictNullChecks is false #20326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22054,17 +22054,16 @@ namespace ts {
if (func) {
const signature = getSignatureFromDeclaration(func);
const returnType = getReturnTypeOfSignature(signature);
const functionFlags = getFunctionFlags(func);
if (functionFlags & FunctionFlags.Generator) { // AsyncGenerator function or Generator function
// A generator does not need its return expressions checked against its return type.
// Instead, the yield expressions are checked against the element type.
// TODO: Check return expressions of generators when return type tracking is added
// for generators.
return;
}
if (strictNullChecks || node.expression || returnType.flags & TypeFlags.Never) {
const exprType = node.expression ? checkExpressionCached(node.expression) : undefinedType;
const functionFlags = getFunctionFlags(func);
if (functionFlags & FunctionFlags.Generator) { // AsyncGenerator function or Generator function
// A generator does not need its return expressions checked against its return type.
// Instead, the yield expressions are checked against the element type.
// TODO: Check return expressions of generators when return type tracking is added
// for generators.
return;
}

if (func.kind === SyntaxKind.SetAccessor) {
if (node.expression) {
error(node, Diagnostics.Setters_cannot_return_a_value);
Expand Down
17 changes: 17 additions & 0 deletions tests/baselines/reference/generatorNoImplicitReturns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [generatorNoImplicitReturns.ts]

function* testGenerator () {
if (Math.random() > 0.5) {
return;
}
yield 'hello';
}


//// [generatorNoImplicitReturns.js]
function* testGenerator() {
if (Math.random() > 0.5) {
return;
}
yield 'hello';
}
15 changes: 15 additions & 0 deletions tests/baselines/reference/generatorNoImplicitReturns.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== tests/cases/conformance/es6/yieldExpressions/generatorNoImplicitReturns.ts ===

function* testGenerator () {
>testGenerator : Symbol(testGenerator, Decl(generatorNoImplicitReturns.ts, 0, 0))

if (Math.random() > 0.5) {
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

return;
}
yield 'hello';
}

20 changes: 20 additions & 0 deletions tests/baselines/reference/generatorNoImplicitReturns.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/conformance/es6/yieldExpressions/generatorNoImplicitReturns.ts ===

function* testGenerator () {
>testGenerator : () => IterableIterator<string>

if (Math.random() > 0.5) {
>Math.random() > 0.5 : boolean
>Math.random() : number
>Math.random : () => number
>Math : Math
>random : () => number
>0.5 : 0.5

return;
}
yield 'hello';
>yield 'hello' : any
>'hello' : "hello"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @target: esnext
// @noImplicitReturns: true
// @strictNullChecks: false

function* testGenerator () {
if (Math.random() > 0.5) {
return;
}
yield 'hello';
}