Skip to content

Commit 556e268

Browse files
committed
#15943 #15981 Update special cases for await / yield parsing
1 parent 66b6d69 commit 556e268

File tree

7 files changed

+140
-4
lines changed

7 files changed

+140
-4
lines changed

src/compiler/parser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2960,7 +2960,7 @@ namespace ts {
29602960
// for now we just check if the next token is an identifier. More heuristics
29612961
// can be added here later as necessary. We just need to make sure that we
29622962
// don't accidentally consume something legal.
2963-
return lookAhead(nextTokenIsIdentifierOrKeywordOrNumberOnSameLine);
2963+
return lookAhead(nextTokenIsIdentifierOrKeywordOrLiteralOnSameLine);
29642964
}
29652965

29662966
return false;
@@ -3478,7 +3478,7 @@ namespace ts {
34783478
}
34793479

34803480
// here we are using similar heuristics as 'isYieldExpression'
3481-
return lookAhead(nextTokenIsIdentifierOnSameLine);
3481+
return lookAhead(nextTokenIsIdentifierOrKeywordOrLiteralOnSameLine);
34823482
}
34833483

34843484
return false;
@@ -4677,9 +4677,9 @@ namespace ts {
46774677
return token() === SyntaxKind.FunctionKeyword && !scanner.hasPrecedingLineBreak();
46784678
}
46794679

4680-
function nextTokenIsIdentifierOrKeywordOrNumberOnSameLine() {
4680+
function nextTokenIsIdentifierOrKeywordOrLiteralOnSameLine() {
46814681
nextToken();
4682-
return (tokenIsIdentifierOrKeyword(token()) || token() === SyntaxKind.NumericLiteral) && !scanner.hasPrecedingLineBreak();
4682+
return (tokenIsIdentifierOrKeyword(token()) || token() === SyntaxKind.NumericLiteral || token() === SyntaxKind.StringLiteral) && !scanner.hasPrecedingLineBreak();
46834683
}
46844684

46854685
function isDeclaration(): boolean {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
tests/cases/compiler/awaitLiteralValues.ts(2,5): error TS1308: 'await' expression is only allowed within an async function.
2+
tests/cases/compiler/awaitLiteralValues.ts(6,5): error TS1308: 'await' expression is only allowed within an async function.
3+
tests/cases/compiler/awaitLiteralValues.ts(10,5): error TS1308: 'await' expression is only allowed within an async function.
4+
tests/cases/compiler/awaitLiteralValues.ts(14,5): error TS1308: 'await' expression is only allowed within an async function.
5+
tests/cases/compiler/awaitLiteralValues.ts(18,5): error TS1308: 'await' expression is only allowed within an async function.
6+
tests/cases/compiler/awaitLiteralValues.ts(22,5): error TS1308: 'await' expression is only allowed within an async function.
7+
8+
9+
==== tests/cases/compiler/awaitLiteralValues.ts (6 errors) ====
10+
function awaitString() {
11+
await 'literal';
12+
~~~~~
13+
!!! error TS1308: 'await' expression is only allowed within an async function.
14+
}
15+
16+
function awaitNumber() {
17+
await 1;
18+
~~~~~
19+
!!! error TS1308: 'await' expression is only allowed within an async function.
20+
}
21+
22+
function awaitTrue() {
23+
await true;
24+
~~~~~
25+
!!! error TS1308: 'await' expression is only allowed within an async function.
26+
}
27+
28+
function awaitFalse() {
29+
await false;
30+
~~~~~
31+
!!! error TS1308: 'await' expression is only allowed within an async function.
32+
}
33+
34+
function awaitNull() {
35+
await null;
36+
~~~~~
37+
!!! error TS1308: 'await' expression is only allowed within an async function.
38+
}
39+
40+
function awaitUndefined() {
41+
await undefined;
42+
~~~~~
43+
!!! error TS1308: 'await' expression is only allowed within an async function.
44+
}
45+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [awaitLiteralValues.ts]
2+
function awaitString() {
3+
await 'literal';
4+
}
5+
6+
function awaitNumber() {
7+
await 1;
8+
}
9+
10+
function awaitTrue() {
11+
await true;
12+
}
13+
14+
function awaitFalse() {
15+
await false;
16+
}
17+
18+
function awaitNull() {
19+
await null;
20+
}
21+
22+
function awaitUndefined() {
23+
await undefined;
24+
}
25+
26+
27+
//// [awaitLiteralValues.js]
28+
function awaitString() {
29+
yield 'literal';
30+
}
31+
function awaitNumber() {
32+
yield 1;
33+
}
34+
function awaitTrue() {
35+
yield true;
36+
}
37+
function awaitFalse() {
38+
yield false;
39+
}
40+
function awaitNull() {
41+
yield null;
42+
}
43+
function awaitUndefined() {
44+
yield undefined;
45+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/yieldStringLiteral.ts(2,5): error TS1163: A 'yield' expression is only allowed in a generator body.
2+
3+
4+
==== tests/cases/compiler/yieldStringLiteral.ts (1 errors) ====
5+
function yieldString() {
6+
yield 'literal';
7+
~~~~~
8+
!!! error TS1163: A 'yield' expression is only allowed in a generator body.
9+
}
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [yieldStringLiteral.ts]
2+
function yieldString() {
3+
yield 'literal';
4+
}
5+
6+
7+
//// [yieldStringLiteral.js]
8+
function yieldString() {
9+
yield 'literal';
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function awaitString() {
2+
await 'literal';
3+
}
4+
5+
function awaitNumber() {
6+
await 1;
7+
}
8+
9+
function awaitTrue() {
10+
await true;
11+
}
12+
13+
function awaitFalse() {
14+
await false;
15+
}
16+
17+
function awaitNull() {
18+
await null;
19+
}
20+
21+
function awaitUndefined() {
22+
await undefined;
23+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function yieldString() {
2+
yield 'literal';
3+
}

0 commit comments

Comments
 (0)