#15943 #15981 Update special cases for await / yield parsing

This commit is contained in:
Charles Pierce 2017-06-01 20:37:52 -07:00
parent 66b6d69c37
commit 556e268bd0
7 changed files with 140 additions and 4 deletions

View file

@ -2960,7 +2960,7 @@ namespace ts {
// for now we just check if the next token is an identifier. More heuristics
// can be added here later as necessary. We just need to make sure that we
// don't accidentally consume something legal.
return lookAhead(nextTokenIsIdentifierOrKeywordOrNumberOnSameLine);
return lookAhead(nextTokenIsIdentifierOrKeywordOrLiteralOnSameLine);
}
return false;
@ -3478,7 +3478,7 @@ namespace ts {
}
// here we are using similar heuristics as 'isYieldExpression'
return lookAhead(nextTokenIsIdentifierOnSameLine);
return lookAhead(nextTokenIsIdentifierOrKeywordOrLiteralOnSameLine);
}
return false;
@ -4677,9 +4677,9 @@ namespace ts {
return token() === SyntaxKind.FunctionKeyword && !scanner.hasPrecedingLineBreak();
}
function nextTokenIsIdentifierOrKeywordOrNumberOnSameLine() {
function nextTokenIsIdentifierOrKeywordOrLiteralOnSameLine() {
nextToken();
return (tokenIsIdentifierOrKeyword(token()) || token() === SyntaxKind.NumericLiteral) && !scanner.hasPrecedingLineBreak();
return (tokenIsIdentifierOrKeyword(token()) || token() === SyntaxKind.NumericLiteral || token() === SyntaxKind.StringLiteral) && !scanner.hasPrecedingLineBreak();
}
function isDeclaration(): boolean {

View file

@ -0,0 +1,45 @@
tests/cases/compiler/awaitLiteralValues.ts(2,5): error TS1308: 'await' expression is only allowed within an async function.
tests/cases/compiler/awaitLiteralValues.ts(6,5): error TS1308: 'await' expression is only allowed within an async function.
tests/cases/compiler/awaitLiteralValues.ts(10,5): error TS1308: 'await' expression is only allowed within an async function.
tests/cases/compiler/awaitLiteralValues.ts(14,5): error TS1308: 'await' expression is only allowed within an async function.
tests/cases/compiler/awaitLiteralValues.ts(18,5): error TS1308: 'await' expression is only allowed within an async function.
tests/cases/compiler/awaitLiteralValues.ts(22,5): error TS1308: 'await' expression is only allowed within an async function.
==== tests/cases/compiler/awaitLiteralValues.ts (6 errors) ====
function awaitString() {
await 'literal';
~~~~~
!!! error TS1308: 'await' expression is only allowed within an async function.
}
function awaitNumber() {
await 1;
~~~~~
!!! error TS1308: 'await' expression is only allowed within an async function.
}
function awaitTrue() {
await true;
~~~~~
!!! error TS1308: 'await' expression is only allowed within an async function.
}
function awaitFalse() {
await false;
~~~~~
!!! error TS1308: 'await' expression is only allowed within an async function.
}
function awaitNull() {
await null;
~~~~~
!!! error TS1308: 'await' expression is only allowed within an async function.
}
function awaitUndefined() {
await undefined;
~~~~~
!!! error TS1308: 'await' expression is only allowed within an async function.
}

View file

@ -0,0 +1,45 @@
//// [awaitLiteralValues.ts]
function awaitString() {
await 'literal';
}
function awaitNumber() {
await 1;
}
function awaitTrue() {
await true;
}
function awaitFalse() {
await false;
}
function awaitNull() {
await null;
}
function awaitUndefined() {
await undefined;
}
//// [awaitLiteralValues.js]
function awaitString() {
yield 'literal';
}
function awaitNumber() {
yield 1;
}
function awaitTrue() {
yield true;
}
function awaitFalse() {
yield false;
}
function awaitNull() {
yield null;
}
function awaitUndefined() {
yield undefined;
}

View file

@ -0,0 +1,10 @@
tests/cases/compiler/yieldStringLiteral.ts(2,5): error TS1163: A 'yield' expression is only allowed in a generator body.
==== tests/cases/compiler/yieldStringLiteral.ts (1 errors) ====
function yieldString() {
yield 'literal';
~~~~~
!!! error TS1163: A 'yield' expression is only allowed in a generator body.
}

View file

@ -0,0 +1,10 @@
//// [yieldStringLiteral.ts]
function yieldString() {
yield 'literal';
}
//// [yieldStringLiteral.js]
function yieldString() {
yield 'literal';
}

View file

@ -0,0 +1,23 @@
function awaitString() {
await 'literal';
}
function awaitNumber() {
await 1;
}
function awaitTrue() {
await true;
}
function awaitFalse() {
await false;
}
function awaitNull() {
await null;
}
function awaitUndefined() {
await undefined;
}

View file

@ -0,0 +1,3 @@
function yieldString() {
yield 'literal';
}