Skip to content

Commit 5a8a59c

Browse files
committed
warn when generator function without any yield expression (microsoft#13847)
1 parent ceba507 commit 5a8a59c

File tree

81 files changed

+293
-143
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+293
-143
lines changed

src/compiler/checker.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -18714,6 +18714,18 @@ namespace ts {
1871418714
}
1871518715
}
1871618716

18717+
function checkGeneratorWithReturnStatementMustHaveYield (func: FunctionLikeDeclaration) {
18718+
let hasReturnStmt = false;
18719+
forEachReturnStatement(<Block>func.body, _ => hasReturnStmt = true);
18720+
if (hasReturnStmt) {
18721+
let hasYieldExpr = false;
18722+
forEachYieldExpression(<Block>func.body, _ => hasYieldExpr = true);
18723+
if (!hasYieldExpr) {
18724+
error(func, Diagnostics.generator_with_return_statement_must_not_have_zero_yield_return_statements);
18725+
}
18726+
}
18727+
}
18728+
1871718729
function checkSignatureDeclaration(node: SignatureDeclaration) {
1871818730
// Grammar checking
1871918731
if (node.kind === SyntaxKind.IndexSignature) {
@@ -24968,7 +24980,10 @@ namespace ts {
2496824980
if (isInAmbientContext(node)) {
2496924981
return grammarErrorOnNode(node.asteriskToken, Diagnostics.Generators_are_not_allowed_in_an_ambient_context);
2497024982
}
24971-
if (!node.body) {
24983+
if (node.body) {
24984+
checkGeneratorWithReturnStatementMustHaveYield(node);
24985+
}
24986+
else {
2497224987
return grammarErrorOnNode(node.asteriskToken, Diagnostics.An_overload_signature_cannot_be_declared_as_a_generator);
2497324988
}
2497424989
}

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,10 @@
911911
"category": "Error",
912912
"code": 1329
913913
},
914+
"generator with 'return statement' must not have zero 'yield return' statements.": {
915+
"category": "Message",
916+
"code": 1330
917+
},
914918

915919
"Duplicate identifier '{0}'.": {
916920
"category": "Error",

tests/baselines/reference/emitter.asyncGenerators.classMethods.es2015.js

-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class C6 {
3838
//// [C7.ts]
3939
class C7 {
4040
async * f() {
41-
return 1;
4241
}
4342
}
4443
//// [C8.ts]
@@ -215,7 +214,6 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
215214
class C7 {
216215
f() {
217216
return __asyncGenerator(this, arguments, function* f_1() {
218-
return 1;
219217
});
220218
}
221219
}

tests/baselines/reference/emitter.asyncGenerators.classMethods.es2015.symbols

-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ class C7 {
6767

6868
async * f() {
6969
>f : Symbol(C7.f, Decl(C7.ts, 0, 10))
70-
71-
return 1;
7270
}
7371
}
7472
=== tests/cases/conformance/emitter/es2015/asyncGenerators/C8.ts ===

tests/baselines/reference/emitter.asyncGenerators.classMethods.es2015.types

+1-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ class C7 {
8080
>C7 : C7
8181

8282
async * f() {
83-
>f : () => AsyncIterableIterator<1>
84-
85-
return 1;
86-
>1 : 1
83+
>f : () => AsyncIterableIterator<any>
8784
}
8885
}
8986
=== tests/cases/conformance/emitter/es2015/asyncGenerators/C8.ts ===

tests/baselines/reference/emitter.asyncGenerators.classMethods.es5.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class C6 {
3838
//// [C7.ts]
3939
class C7 {
4040
async * f() {
41-
return 1;
4241
}
4342
}
4443
//// [C8.ts]
@@ -497,7 +496,7 @@ var C7 = /** @class */ (function () {
497496
C7.prototype.f = function () {
498497
return __asyncGenerator(this, arguments, function f_1() {
499498
return __generator(this, function (_a) {
500-
return [2 /*return*/, 1];
499+
return [2 /*return*/];
501500
});
502501
});
503502
};

tests/baselines/reference/emitter.asyncGenerators.classMethods.es5.symbols

-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ class C7 {
6767

6868
async * f() {
6969
>f : Symbol(C7.f, Decl(C7.ts, 0, 10))
70-
71-
return 1;
7270
}
7371
}
7472
=== tests/cases/conformance/emitter/es5/asyncGenerators/C8.ts ===

tests/baselines/reference/emitter.asyncGenerators.classMethods.es5.types

+1-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ class C7 {
8080
>C7 : C7
8181

8282
async * f() {
83-
>f : () => AsyncIterableIterator<1>
84-
85-
return 1;
86-
>1 : 1
83+
>f : () => AsyncIterableIterator<any>
8784
}
8885
}
8986
=== tests/cases/conformance/emitter/es5/asyncGenerators/C8.ts ===

tests/baselines/reference/emitter.asyncGenerators.classMethods.esnext.js

-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class C6 {
3838
//// [C7.ts]
3939
class C7 {
4040
async * f() {
41-
return 1;
4241
}
4342
}
4443
//// [C8.ts]
@@ -98,7 +97,6 @@ class C6 {
9897
//// [C7.js]
9998
class C7 {
10099
async *f() {
101-
return 1;
102100
}
103101
}
104102
//// [C8.js]

tests/baselines/reference/emitter.asyncGenerators.classMethods.esnext.symbols

-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ class C7 {
6767

6868
async * f() {
6969
>f : Symbol(C7.f, Decl(C7.ts, 0, 10))
70-
71-
return 1;
7270
}
7371
}
7472
=== tests/cases/conformance/emitter/esnext/asyncGenerators/C8.ts ===

tests/baselines/reference/emitter.asyncGenerators.classMethods.esnext.types

+1-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ class C7 {
8080
>C7 : C7
8181

8282
async * f() {
83-
>f : () => AsyncIterableIterator<1>
84-
85-
return 1;
86-
>1 : 1
83+
>f : () => AsyncIterableIterator<any>
8784
}
8885
}
8986
=== tests/cases/conformance/emitter/esnext/asyncGenerators/C8.ts ===

tests/baselines/reference/emitter.asyncGenerators.functionDeclarations.es2015.js

-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ async function * f6() {
2525
}
2626
//// [F7.ts]
2727
async function * f7() {
28-
return 1;
2928
}
3029

3130

@@ -171,6 +170,5 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
171170
};
172171
function f7() {
173172
return __asyncGenerator(this, arguments, function* f7_1() {
174-
return 1;
175173
});
176174
}

tests/baselines/reference/emitter.asyncGenerators.functionDeclarations.es2015.symbols

-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,5 @@ async function * f6() {
4040
=== tests/cases/conformance/emitter/es2015/asyncGenerators/F7.ts ===
4141
async function * f7() {
4242
>f7 : Symbol(f7, Decl(F7.ts, 0, 0))
43-
44-
return 1;
4543
}
4644

tests/baselines/reference/emitter.asyncGenerators.functionDeclarations.es2015.types

+1-4
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ async function * f6() {
5353
}
5454
=== tests/cases/conformance/emitter/es2015/asyncGenerators/F7.ts ===
5555
async function * f7() {
56-
>f7 : () => AsyncIterableIterator<1>
57-
58-
return 1;
59-
>1 : 1
56+
>f7 : () => AsyncIterableIterator<any>
6057
}
6158

tests/baselines/reference/emitter.asyncGenerators.functionDeclarations.es5.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ async function * f6() {
2525
}
2626
//// [F7.ts]
2727
async function * f7() {
28-
return 1;
2928
}
3029

3130

@@ -433,7 +432,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
433432
function f7() {
434433
return __asyncGenerator(this, arguments, function f7_1() {
435434
return __generator(this, function (_a) {
436-
return [2 /*return*/, 1];
435+
return [2 /*return*/];
437436
});
438437
});
439438
}

tests/baselines/reference/emitter.asyncGenerators.functionDeclarations.es5.symbols

-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,5 @@ async function * f6() {
4040
=== tests/cases/conformance/emitter/es5/asyncGenerators/F7.ts ===
4141
async function * f7() {
4242
>f7 : Symbol(f7, Decl(F7.ts, 0, 0))
43-
44-
return 1;
4543
}
4644

tests/baselines/reference/emitter.asyncGenerators.functionDeclarations.es5.types

+1-4
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ async function * f6() {
5353
}
5454
=== tests/cases/conformance/emitter/es5/asyncGenerators/F7.ts ===
5555
async function * f7() {
56-
>f7 : () => AsyncIterableIterator<1>
57-
58-
return 1;
59-
>1 : 1
56+
>f7 : () => AsyncIterableIterator<any>
6057
}
6158

tests/baselines/reference/emitter.asyncGenerators.functionDeclarations.esnext.js

-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ async function * f6() {
2525
}
2626
//// [F7.ts]
2727
async function * f7() {
28-
return 1;
2928
}
3029

3130

@@ -54,5 +53,4 @@ async function* f6() {
5453
}
5554
//// [F7.js]
5655
async function* f7() {
57-
return 1;
5856
}

tests/baselines/reference/emitter.asyncGenerators.functionDeclarations.esnext.symbols

-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,5 @@ async function * f6() {
4040
=== tests/cases/conformance/emitter/esnext/asyncGenerators/F7.ts ===
4141
async function * f7() {
4242
>f7 : Symbol(f7, Decl(F7.ts, 0, 0))
43-
44-
return 1;
4543
}
4644

tests/baselines/reference/emitter.asyncGenerators.functionDeclarations.esnext.types

+1-4
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ async function * f6() {
5353
}
5454
=== tests/cases/conformance/emitter/esnext/asyncGenerators/F7.ts ===
5555
async function * f7() {
56-
>f7 : () => AsyncIterableIterator<1>
57-
58-
return 1;
59-
>1 : 1
56+
>f7 : () => AsyncIterableIterator<any>
6057
}
6158

tests/baselines/reference/emitter.asyncGenerators.functionExpressions.es2015.js

-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const f6 = async function * () {
2525
}
2626
//// [F7.ts]
2727
const f7 = async function * () {
28-
return 1;
2928
}
3029

3130

@@ -171,6 +170,5 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
171170
};
172171
const f7 = function () {
173172
return __asyncGenerator(this, arguments, function* () {
174-
return 1;
175173
});
176174
};

tests/baselines/reference/emitter.asyncGenerators.functionExpressions.es2015.symbols

-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,5 @@ const f6 = async function * () {
4040
=== tests/cases/conformance/emitter/es2015/asyncGenerators/F7.ts ===
4141
const f7 = async function * () {
4242
>f7 : Symbol(f7, Decl(F7.ts, 0, 5))
43-
44-
return 1;
4543
}
4644

tests/baselines/reference/emitter.asyncGenerators.functionExpressions.es2015.types

+2-5
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ const f6 = async function * () {
5959
}
6060
=== tests/cases/conformance/emitter/es2015/asyncGenerators/F7.ts ===
6161
const f7 = async function * () {
62-
>f7 : () => AsyncIterableIterator<1>
63-
>async function * () { return 1;} : () => AsyncIterableIterator<1>
64-
65-
return 1;
66-
>1 : 1
62+
>f7 : () => AsyncIterableIterator<any>
63+
>async function * () {} : () => AsyncIterableIterator<any>
6764
}
6865

tests/baselines/reference/emitter.asyncGenerators.functionExpressions.es5.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const f6 = async function * () {
2525
}
2626
//// [F7.ts]
2727
const f7 = async function * () {
28-
return 1;
2928
}
3029

3130

@@ -433,7 +432,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
433432
var f7 = function () {
434433
return __asyncGenerator(this, arguments, function () {
435434
return __generator(this, function (_a) {
436-
return [2 /*return*/, 1];
435+
return [2 /*return*/];
437436
});
438437
});
439438
};

tests/baselines/reference/emitter.asyncGenerators.functionExpressions.es5.symbols

-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,5 @@ const f6 = async function * () {
4040
=== tests/cases/conformance/emitter/es5/asyncGenerators/F7.ts ===
4141
const f7 = async function * () {
4242
>f7 : Symbol(f7, Decl(F7.ts, 0, 5))
43-
44-
return 1;
4543
}
4644

tests/baselines/reference/emitter.asyncGenerators.functionExpressions.es5.types

+2-5
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ const f6 = async function * () {
5959
}
6060
=== tests/cases/conformance/emitter/es5/asyncGenerators/F7.ts ===
6161
const f7 = async function * () {
62-
>f7 : () => AsyncIterableIterator<1>
63-
>async function * () { return 1;} : () => AsyncIterableIterator<1>
64-
65-
return 1;
66-
>1 : 1
62+
>f7 : () => AsyncIterableIterator<any>
63+
>async function * () {} : () => AsyncIterableIterator<any>
6764
}
6865

tests/baselines/reference/emitter.asyncGenerators.functionExpressions.esnext.js

-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const f6 = async function * () {
2525
}
2626
//// [F7.ts]
2727
const f7 = async function * () {
28-
return 1;
2928
}
3029

3130

@@ -54,5 +53,4 @@ const f6 = async function* () {
5453
};
5554
//// [F7.js]
5655
const f7 = async function* () {
57-
return 1;
5856
};

tests/baselines/reference/emitter.asyncGenerators.functionExpressions.esnext.symbols

-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,5 @@ const f6 = async function * () {
4040
=== tests/cases/conformance/emitter/esnext/asyncGenerators/F7.ts ===
4141
const f7 = async function * () {
4242
>f7 : Symbol(f7, Decl(F7.ts, 0, 5))
43-
44-
return 1;
4543
}
4644

tests/baselines/reference/emitter.asyncGenerators.functionExpressions.esnext.types

+2-5
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ const f6 = async function * () {
5959
}
6060
=== tests/cases/conformance/emitter/esnext/asyncGenerators/F7.ts ===
6161
const f7 = async function * () {
62-
>f7 : () => AsyncIterableIterator<1>
63-
>async function * () { return 1;} : () => AsyncIterableIterator<1>
64-
65-
return 1;
66-
>1 : 1
62+
>f7 : () => AsyncIterableIterator<any>
63+
>async function * () {} : () => AsyncIterableIterator<any>
6764
}
6865

tests/baselines/reference/emitter.asyncGenerators.objectLiteralMethods.es2015.js

-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const o6 = {
3838
//// [O7.ts]
3939
const o7 = {
4040
async * f() {
41-
return 1;
4241
}
4342
}
4443

@@ -198,7 +197,6 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
198197
const o7 = {
199198
f() {
200199
return __asyncGenerator(this, arguments, function* f_1() {
201-
return 1;
202200
});
203201
}
204202
};

tests/baselines/reference/emitter.asyncGenerators.objectLiteralMethods.es2015.symbols

-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ const o7 = {
6767

6868
async * f() {
6969
>f : Symbol(f, Decl(O7.ts, 0, 12))
70-
71-
return 1;
7270
}
7371
}
7472

0 commit comments

Comments
 (0)