Skip to content

Commit 245809a

Browse files
rbucktontypescript-bot
authored andcommitted
Cherry-pick PR microsoft#39216 into release-4.0
Component commits: 6298d84 Leverage syntax cursor as part of reparse
1 parent edb72ea commit 245809a

File tree

95 files changed

+815
-966
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+815
-966
lines changed

src/compiler/binder.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,20 +2109,39 @@ namespace ts {
21092109
}
21102110

21112111
// The binder visits every node in the syntax tree so it is a convenient place to perform a single localized
2112-
// check for reserved words used as identifiers in strict mode code.
2113-
function checkStrictModeIdentifier(node: Identifier) {
2114-
if (inStrictMode &&
2115-
node.originalKeywordKind! >= SyntaxKind.FirstFutureReservedWord &&
2116-
node.originalKeywordKind! <= SyntaxKind.LastFutureReservedWord &&
2117-
!isIdentifierName(node) &&
2112+
// check for reserved words used as identifiers in strict mode code, as well as `yield` or `await` in
2113+
// [Yield] or [Await] contexts, respectively.
2114+
function checkContextualIdentifier(node: Identifier) {
2115+
// Report error only if there are no parse errors in file
2116+
if (!file.parseDiagnostics.length &&
21182117
!(node.flags & NodeFlags.Ambient) &&
2119-
!(node.flags & NodeFlags.JSDoc)) {
2118+
!(node.flags & NodeFlags.JSDoc) &&
2119+
!isIdentifierName(node)) {
21202120

2121-
// Report error only if there are no parse errors in file
2122-
if (!file.parseDiagnostics.length) {
2121+
// strict mode identifiers
2122+
if (inStrictMode &&
2123+
node.originalKeywordKind! >= SyntaxKind.FirstFutureReservedWord &&
2124+
node.originalKeywordKind! <= SyntaxKind.LastFutureReservedWord) {
21232125
file.bindDiagnostics.push(createDiagnosticForNode(node,
21242126
getStrictModeIdentifierMessage(node), declarationNameToString(node)));
21252127
}
2128+
else if (node.originalKeywordKind === SyntaxKind.AwaitKeyword) {
2129+
if (isExternalModule(file) && isInTopLevelContext(node)) {
2130+
file.bindDiagnostics.push(createDiagnosticForNode(node,
2131+
Diagnostics.Identifier_expected_0_is_a_reserved_word_at_the_top_level_of_a_module,
2132+
declarationNameToString(node)));
2133+
}
2134+
else if (node.flags & NodeFlags.AwaitContext) {
2135+
file.bindDiagnostics.push(createDiagnosticForNode(node,
2136+
Diagnostics.Identifier_expected_0_is_a_reserved_word_that_cannot_be_used_here,
2137+
declarationNameToString(node)));
2138+
}
2139+
}
2140+
else if (node.originalKeywordKind === SyntaxKind.YieldKeyword && node.flags & NodeFlags.YieldContext) {
2141+
file.bindDiagnostics.push(createDiagnosticForNode(node,
2142+
Diagnostics.Identifier_expected_0_is_a_reserved_word_that_cannot_be_used_here,
2143+
declarationNameToString(node)));
2144+
}
21262145
}
21272146
}
21282147

@@ -2423,7 +2442,7 @@ namespace ts {
24232442
if (currentFlow && (isExpression(node) || parent.kind === SyntaxKind.ShorthandPropertyAssignment)) {
24242443
node.flowNode = currentFlow;
24252444
}
2426-
return checkStrictModeIdentifier(<Identifier>node);
2445+
return checkContextualIdentifier(<Identifier>node);
24272446
case SyntaxKind.SuperKeyword:
24282447
node.flowNode = currentFlow;
24292448
break;

src/compiler/checker.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28557,11 +28557,6 @@ namespace ts {
2855728557
return undefinedWideningType;
2855828558
}
2855928559

28560-
function isInTopLevelContext(node: Node) {
28561-
const container = getThisContainer(node, /*includeArrowFunctions*/ true);
28562-
return isSourceFile(container);
28563-
}
28564-
2856528560
function checkAwaitExpression(node: AwaitExpression): Type {
2856628561
// Grammar checking
2856728562
if (produceDiagnostics) {
@@ -35168,7 +35163,6 @@ namespace ts {
3516835163
}
3516935164

3517035165
function checkImportBinding(node: ImportEqualsDeclaration | ImportClause | NamespaceImport | ImportSpecifier) {
35171-
checkGrammarAwaitIdentifier(node.name);
3517235166
checkCollisionWithRequireExportsInGeneratedCode(node, node.name!);
3517335167
checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name!);
3517435168
checkAliasSymbol(node);
@@ -37934,31 +37928,19 @@ namespace ts {
3793437928
return false;
3793537929
}
3793637930

37937-
function checkGrammarAwaitIdentifier(name: Identifier | undefined): boolean {
37938-
if (name && isIdentifier(name) && name.originalKeywordKind === SyntaxKind.AwaitKeyword && isInTopLevelContext(name.parent)) {
37939-
const file = getSourceFileOfNode(name);
37940-
if (!file.isDeclarationFile && isExternalModule(file)) {
37941-
return grammarErrorOnNode(name, Diagnostics.Identifier_expected_0_is_a_reserved_word_at_the_top_level_of_a_module, idText(name));
37942-
}
37943-
}
37944-
return false;
37945-
}
37946-
3794737931
function checkGrammarFunctionLikeDeclaration(node: FunctionLikeDeclaration | MethodSignature): boolean {
3794837932
// Prevent cascading error by short-circuit
3794937933
const file = getSourceFileOfNode(node);
3795037934
return checkGrammarDecoratorsAndModifiers(node) ||
3795137935
checkGrammarTypeParameterList(node.typeParameters, file) ||
37952-
(isFunctionDeclaration(node) && checkGrammarAwaitIdentifier(node.name)) ||
3795337936
checkGrammarParameterList(node.parameters) ||
3795437937
checkGrammarArrowFunction(node, file) ||
3795537938
(isFunctionLikeDeclaration(node) && checkGrammarForUseStrictSimpleParameterList(node));
3795637939
}
3795737940

3795837941
function checkGrammarClassLikeDeclaration(node: ClassLikeDeclaration): boolean {
3795937942
const file = getSourceFileOfNode(node);
37960-
return (isClassDeclaration(node) && checkGrammarAwaitIdentifier(node.name)) ||
37961-
checkGrammarClassDeclarationHeritageClauses(node) ||
37943+
return checkGrammarClassDeclarationHeritageClauses(node) ||
3796237944
checkGrammarTypeParameterList(node.typeParameters, file);
3796337945
}
3796437946

@@ -38601,10 +38583,6 @@ namespace ts {
3860138583
}
3860238584
}
3860338585

38604-
if (isIdentifier(node.name) && checkGrammarAwaitIdentifier(node.name)) {
38605-
return true;
38606-
}
38607-
3860838586
if (node.dotDotDotToken && node.initializer) {
3860938587
// Error on equals token which immediately precedes the initializer
3861038588
return grammarErrorAtPos(node, node.initializer.pos - 1, 1, Diagnostics.A_rest_element_cannot_have_an_initializer);
@@ -38668,9 +38646,6 @@ namespace ts {
3866838646
}
3866938647
}
3867038648
}
38671-
if (isIdentifier(node.name) && checkGrammarAwaitIdentifier(node.name)) {
38672-
return true;
38673-
}
3867438649

3867538650
if (node.exclamationToken && (node.parent.parent.kind !== SyntaxKind.VariableStatement || !node.type || node.initializer || node.flags & NodeFlags.Ambient)) {
3867638651
return grammarErrorOnNode(node.exclamationToken, Diagnostics.Definite_assignment_assertions_can_only_be_used_along_with_a_type_annotation);

0 commit comments

Comments
 (0)