Skip to content

Commit e4576ed

Browse files
committed
warn when generator function without any yield expression(microsoft#13847)
1 parent 8528667 commit e4576ed

File tree

81 files changed

+284
-144
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

+284
-144
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17283,7 +17283,12 @@ namespace ts {
1728317283
else {
1728417284
let types: Type[];
1728517285
if (functionFlags & FunctionFlags.Generator) { // Generator or AsyncGenerator function
17286-
types = concatenate(checkAndAggregateYieldOperandTypes(func, checkMode), checkAndAggregateReturnExpressionTypes(func, checkMode));
17286+
const aggregatedYield = checkAndAggregateYieldOperandTypes(func, checkMode);
17287+
const aggregatedReturn = checkAndAggregateReturnExpressionTypes(func, checkMode)
17288+
if (aggregatedReturn.length && aggregatedYield.length === 0) {
17289+
error(func, Diagnostics.A_generator_cannot_have_a_return_statement_and_no_yield_statements);
17290+
}
17291+
types = concatenate(aggregatedYield, aggregatedReturn);
1728717292
if (!types || types.length === 0) {
1728817293
const iterableIteratorAny = functionFlags & FunctionFlags.Async
1728917294
? createAsyncIterableIteratorType(anyType) // AsyncGenerator function
@@ -25071,7 +25076,7 @@ namespace ts {
2507125076
if (isInAmbientContext(node)) {
2507225077
return grammarErrorOnNode(node.asteriskToken, Diagnostics.Generators_are_not_allowed_in_an_ambient_context);
2507325078
}
25074-
if (!node.body) {
25079+
else {
2507525080
return grammarErrorOnNode(node.asteriskToken, Diagnostics.An_overload_signature_cannot_be_declared_as_a_generator);
2507625081
}
2507725082
}

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,10 @@
911911
"category": "Error",
912912
"code": 1329
913913
},
914+
"A generator cannot have a return statement and no yield statements.": {
915+
"category": "Error",
916+
"code": 1330
917+
},
914918

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

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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 2 additions & 5 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 2 additions & 5 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 2 additions & 5 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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)