Skip to content

Commit 1ea1809

Browse files
authored
Properly disallow yield in bodyless arrows (#61976)
1 parent b70d4d7 commit 1ea1809

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

src/compiler/parser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5559,12 +5559,15 @@ namespace Parser {
55595559
return parseFunctionBlock(SignatureFlags.IgnoreMissingOpenBrace | (isAsync ? SignatureFlags.Await : SignatureFlags.None));
55605560
}
55615561

5562+
const savedYieldContext = inYieldContext();
5563+
setYieldContext(false);
55625564
const savedTopLevel = topLevel;
55635565
topLevel = false;
55645566
const node = isAsync
55655567
? doInAwaitContext(() => parseAssignmentExpressionOrHigher(allowReturnTypeInArrowFunction))
55665568
: doOutsideOfAwaitContext(() => parseAssignmentExpressionOrHigher(allowReturnTypeInArrowFunction));
55675569
topLevel = savedTopLevel;
5570+
setYieldContext(savedYieldContext);
55685571
return node;
55695572
}
55705573

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
YieldExpression20_es6.ts(3,8): error TS1163: A 'yield' expression is only allowed in a generator body.
2+
3+
4+
==== YieldExpression20_es6.ts (1 errors) ====
5+
function* test() {
6+
return () => ({
7+
b: yield 2, // error
8+
~~~~~
9+
!!! error TS1163: A 'yield' expression is only allowed in a generator body.
10+
});
11+
}
12+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/conformance/es6/yieldExpressions/YieldExpression20_es6.ts] ////
2+
3+
//// [YieldExpression20_es6.ts]
4+
function* test() {
5+
return () => ({
6+
b: yield 2, // error
7+
});
8+
}
9+
10+
11+
//// [YieldExpression20_es6.js]
12+
function* test() {
13+
return () => ({
14+
b: yield 2, // error
15+
});
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [tests/cases/conformance/es6/yieldExpressions/YieldExpression20_es6.ts] ////
2+
3+
=== YieldExpression20_es6.ts ===
4+
function* test() {
5+
>test : Symbol(test, Decl(YieldExpression20_es6.ts, 0, 0))
6+
7+
return () => ({
8+
b: yield 2, // error
9+
>b : Symbol(b, Decl(YieldExpression20_es6.ts, 1, 17))
10+
11+
});
12+
}
13+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [tests/cases/conformance/es6/yieldExpressions/YieldExpression20_es6.ts] ////
2+
3+
=== YieldExpression20_es6.ts ===
4+
function* test() {
5+
>test : () => Generator<never, () => { b: any; }, unknown>
6+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7+
8+
return () => ({
9+
>() => ({ b: yield 2, // error }) : () => { b: any; }
10+
> : ^^^^^^^^^^^^^^^^^
11+
>({ b: yield 2, // error }) : { b: any; }
12+
> : ^^^^^^^^^^^
13+
>{ b: yield 2, // error } : { b: any; }
14+
> : ^^^^^^^^^^^
15+
16+
b: yield 2, // error
17+
>b : any
18+
> : ^^^
19+
>yield 2 : any
20+
> : ^^^
21+
>2 : 2
22+
> : ^
23+
24+
});
25+
}
26+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @target: es6
2+
3+
function* test() {
4+
return () => ({
5+
b: yield 2, // error
6+
});
7+
}

0 commit comments

Comments
 (0)