Skip to content

warn when generator function without any yield expression (#13847) #19413

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

Closed
Closed
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
9 changes: 7 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17283,7 +17283,12 @@ namespace ts {
else {
let types: Type[];
if (functionFlags & FunctionFlags.Generator) { // Generator or AsyncGenerator function
types = concatenate(checkAndAggregateYieldOperandTypes(func, checkMode), checkAndAggregateReturnExpressionTypes(func, checkMode));
const aggregatedYield = checkAndAggregateYieldOperandTypes(func, checkMode);
const aggregatedReturn = checkAndAggregateReturnExpressionTypes(func, checkMode)
if (aggregatedReturn.length && aggregatedYield.length === 0) {
error(func, Diagnostics.A_generator_cannot_have_a_return_statement_and_no_yield_statements);
}
types = concatenate(aggregatedYield, aggregatedReturn);
if (!types || types.length === 0) {
const iterableIteratorAny = functionFlags & FunctionFlags.Async
? createAsyncIterableIteratorType(anyType) // AsyncGenerator function
Expand Down Expand Up @@ -25071,7 +25076,7 @@ namespace ts {
if (isInAmbientContext(node)) {
return grammarErrorOnNode(node.asteriskToken, Diagnostics.Generators_are_not_allowed_in_an_ambient_context);
}
if (!node.body) {
else {
return grammarErrorOnNode(node.asteriskToken, Diagnostics.An_overload_signature_cannot_be_declared_as_a_generator);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,10 @@
"category": "Error",
"code": 1329
},
"A generator cannot have a return statement and no yield statements.": {
"category": "Error",
"code": 1330
},

"Duplicate identifier '{0}'.": {
"category": "Error",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class C6 {
//// [C7.ts]
class C7 {
async * f() {
return 1;
}
}
//// [C8.ts]
Expand Down Expand Up @@ -215,7 +214,6 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
class C7 {
f() {
return __asyncGenerator(this, arguments, function* f_1() {
return 1;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ class C7 {

async * f() {
>f : Symbol(C7.f, Decl(C7.ts, 0, 10))

return 1;
}
}
=== tests/cases/conformance/emitter/es2015/asyncGenerators/C8.ts ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ class C7 {
>C7 : C7

async * f() {
>f : () => AsyncIterableIterator<1>

return 1;
>1 : 1
>f : () => AsyncIterableIterator<any>
}
}
=== tests/cases/conformance/emitter/es2015/asyncGenerators/C8.ts ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class C6 {
//// [C7.ts]
class C7 {
async * f() {
return 1;
}
}
//// [C8.ts]
Expand Down Expand Up @@ -497,7 +496,7 @@ var C7 = /** @class */ (function () {
C7.prototype.f = function () {
return __asyncGenerator(this, arguments, function f_1() {
return __generator(this, function (_a) {
return [2 /*return*/, 1];
return [2 /*return*/];
});
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ class C7 {

async * f() {
>f : Symbol(C7.f, Decl(C7.ts, 0, 10))

return 1;
}
}
=== tests/cases/conformance/emitter/es5/asyncGenerators/C8.ts ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ class C7 {
>C7 : C7

async * f() {
>f : () => AsyncIterableIterator<1>

return 1;
>1 : 1
>f : () => AsyncIterableIterator<any>
}
}
=== tests/cases/conformance/emitter/es5/asyncGenerators/C8.ts ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class C6 {
//// [C7.ts]
class C7 {
async * f() {
return 1;
}
}
//// [C8.ts]
Expand Down Expand Up @@ -98,7 +97,6 @@ class C6 {
//// [C7.js]
class C7 {
async *f() {
return 1;
}
}
//// [C8.js]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ class C7 {

async * f() {
>f : Symbol(C7.f, Decl(C7.ts, 0, 10))

return 1;
}
}
=== tests/cases/conformance/emitter/esnext/asyncGenerators/C8.ts ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ class C7 {
>C7 : C7

async * f() {
>f : () => AsyncIterableIterator<1>

return 1;
>1 : 1
>f : () => AsyncIterableIterator<any>
}
}
=== tests/cases/conformance/emitter/esnext/asyncGenerators/C8.ts ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ async function * f6() {
}
//// [F7.ts]
async function * f7() {
return 1;
}


Expand Down Expand Up @@ -171,6 +170,5 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
};
function f7() {
return __asyncGenerator(this, arguments, function* f7_1() {
return 1;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,5 @@ async function * f6() {
=== tests/cases/conformance/emitter/es2015/asyncGenerators/F7.ts ===
async function * f7() {
>f7 : Symbol(f7, Decl(F7.ts, 0, 0))

return 1;
}

Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ async function * f6() {
}
=== tests/cases/conformance/emitter/es2015/asyncGenerators/F7.ts ===
async function * f7() {
>f7 : () => AsyncIterableIterator<1>

return 1;
>1 : 1
>f7 : () => AsyncIterableIterator<any>
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ async function * f6() {
}
//// [F7.ts]
async function * f7() {
return 1;
}


Expand Down Expand Up @@ -433,7 +432,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
function f7() {
return __asyncGenerator(this, arguments, function f7_1() {
return __generator(this, function (_a) {
return [2 /*return*/, 1];
return [2 /*return*/];
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,5 @@ async function * f6() {
=== tests/cases/conformance/emitter/es5/asyncGenerators/F7.ts ===
async function * f7() {
>f7 : Symbol(f7, Decl(F7.ts, 0, 0))

return 1;
}

Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ async function * f6() {
}
=== tests/cases/conformance/emitter/es5/asyncGenerators/F7.ts ===
async function * f7() {
>f7 : () => AsyncIterableIterator<1>

return 1;
>1 : 1
>f7 : () => AsyncIterableIterator<any>
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ async function * f6() {
}
//// [F7.ts]
async function * f7() {
return 1;
}


Expand Down Expand Up @@ -54,5 +53,4 @@ async function* f6() {
}
//// [F7.js]
async function* f7() {
return 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,5 @@ async function * f6() {
=== tests/cases/conformance/emitter/esnext/asyncGenerators/F7.ts ===
async function * f7() {
>f7 : Symbol(f7, Decl(F7.ts, 0, 0))

return 1;
}

Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ async function * f6() {
}
=== tests/cases/conformance/emitter/esnext/asyncGenerators/F7.ts ===
async function * f7() {
>f7 : () => AsyncIterableIterator<1>

return 1;
>1 : 1
>f7 : () => AsyncIterableIterator<any>
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const f6 = async function * () {
}
//// [F7.ts]
const f7 = async function * () {
return 1;
}


Expand Down Expand Up @@ -171,6 +170,5 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
};
const f7 = function () {
return __asyncGenerator(this, arguments, function* () {
return 1;
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,5 @@ const f6 = async function * () {
=== tests/cases/conformance/emitter/es2015/asyncGenerators/F7.ts ===
const f7 = async function * () {
>f7 : Symbol(f7, Decl(F7.ts, 0, 5))

return 1;
}

Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ const f6 = async function * () {
}
=== tests/cases/conformance/emitter/es2015/asyncGenerators/F7.ts ===
const f7 = async function * () {
>f7 : () => AsyncIterableIterator<1>
>async function * () { return 1;} : () => AsyncIterableIterator<1>

return 1;
>1 : 1
>f7 : () => AsyncIterableIterator<any>
>async function * () {} : () => AsyncIterableIterator<any>
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const f6 = async function * () {
}
//// [F7.ts]
const f7 = async function * () {
return 1;
}


Expand Down Expand Up @@ -433,7 +432,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
var f7 = function () {
return __asyncGenerator(this, arguments, function () {
return __generator(this, function (_a) {
return [2 /*return*/, 1];
return [2 /*return*/];
});
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,5 @@ const f6 = async function * () {
=== tests/cases/conformance/emitter/es5/asyncGenerators/F7.ts ===
const f7 = async function * () {
>f7 : Symbol(f7, Decl(F7.ts, 0, 5))

return 1;
}

Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ const f6 = async function * () {
}
=== tests/cases/conformance/emitter/es5/asyncGenerators/F7.ts ===
const f7 = async function * () {
>f7 : () => AsyncIterableIterator<1>
>async function * () { return 1;} : () => AsyncIterableIterator<1>

return 1;
>1 : 1
>f7 : () => AsyncIterableIterator<any>
>async function * () {} : () => AsyncIterableIterator<any>
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const f6 = async function * () {
}
//// [F7.ts]
const f7 = async function * () {
return 1;
}


Expand Down Expand Up @@ -54,5 +53,4 @@ const f6 = async function* () {
};
//// [F7.js]
const f7 = async function* () {
return 1;
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,5 @@ const f6 = async function * () {
=== tests/cases/conformance/emitter/esnext/asyncGenerators/F7.ts ===
const f7 = async function * () {
>f7 : Symbol(f7, Decl(F7.ts, 0, 5))

return 1;
}

Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ const f6 = async function * () {
}
=== tests/cases/conformance/emitter/esnext/asyncGenerators/F7.ts ===
const f7 = async function * () {
>f7 : () => AsyncIterableIterator<1>
>async function * () { return 1;} : () => AsyncIterableIterator<1>

return 1;
>1 : 1
>f7 : () => AsyncIterableIterator<any>
>async function * () {} : () => AsyncIterableIterator<any>
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const o6 = {
//// [O7.ts]
const o7 = {
async * f() {
return 1;
}
}

Expand Down Expand Up @@ -198,7 +197,6 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
const o7 = {
f() {
return __asyncGenerator(this, arguments, function* f_1() {
return 1;
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ const o7 = {

async * f() {
>f : Symbol(f, Decl(O7.ts, 0, 12))

return 1;
}
}

Loading