Skip to content

Commit 5b12a04

Browse files
Merge pull request #16213 from charlespierce/await_yield_literals
Update special cases for await / yield expression parsing
2 parents 26ab0cd + 556e268 commit 5b12a04

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
@@ -2972,7 +2972,7 @@ namespace ts {
29722972
// for now we just check if the next token is an identifier. More heuristics
29732973
// can be added here later as necessary. We just need to make sure that we
29742974
// don't accidentally consume something legal.
2975-
return lookAhead(nextTokenIsIdentifierOrKeywordOrNumberOnSameLine);
2975+
return lookAhead(nextTokenIsIdentifierOrKeywordOrLiteralOnSameLine);
29762976
}
29772977

29782978
return false;
@@ -3490,7 +3490,7 @@ namespace ts {
34903490
}
34913491

34923492
// here we are using similar heuristics as 'isYieldExpression'
3493-
return lookAhead(nextTokenIsIdentifierOnSameLine);
3493+
return lookAhead(nextTokenIsIdentifierOrKeywordOrLiteralOnSameLine);
34943494
}
34953495

34963496
return false;
@@ -4699,9 +4699,9 @@ namespace ts {
46994699
return token() === SyntaxKind.FunctionKeyword && !scanner.hasPrecedingLineBreak();
47004700
}
47014701

4702-
function nextTokenIsIdentifierOrKeywordOrNumberOnSameLine() {
4702+
function nextTokenIsIdentifierOrKeywordOrLiteralOnSameLine() {
47034703
nextToken();
4704-
return (tokenIsIdentifierOrKeyword(token()) || token() === SyntaxKind.NumericLiteral) && !scanner.hasPrecedingLineBreak();
4704+
return (tokenIsIdentifierOrKeyword(token()) || token() === SyntaxKind.NumericLiteral || token() === SyntaxKind.StringLiteral) && !scanner.hasPrecedingLineBreak();
47054705
}
47064706

47074707
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)