Skip to content

Commit 6298d84

Browse files
committed
Leverage syntax cursor as part of reparse
1 parent d10beaa commit 6298d84

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
@@ -28255,11 +28255,6 @@ namespace ts {
2825528255
return undefinedWideningType;
2825628256
}
2825728257

28258-
function isInTopLevelContext(node: Node) {
28259-
const container = getThisContainer(node, /*includeArrowFunctions*/ true);
28260-
return isSourceFile(container);
28261-
}
28262-
2826328258
function checkAwaitExpression(node: AwaitExpression): Type {
2826428259
// Grammar checking
2826528260
if (produceDiagnostics) {
@@ -34861,7 +34856,6 @@ namespace ts {
3486134856
}
3486234857

3486334858
function checkImportBinding(node: ImportEqualsDeclaration | ImportClause | NamespaceImport | ImportSpecifier) {
34864-
checkGrammarAwaitIdentifier(node.name);
3486534859
checkCollisionWithRequireExportsInGeneratedCode(node, node.name!);
3486634860
checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name!);
3486734861
checkAliasSymbol(node);
@@ -37627,31 +37621,19 @@ namespace ts {
3762737621
return false;
3762837622
}
3762937623

37630-
function checkGrammarAwaitIdentifier(name: Identifier | undefined): boolean {
37631-
if (name && isIdentifier(name) && name.originalKeywordKind === SyntaxKind.AwaitKeyword && isInTopLevelContext(name.parent)) {
37632-
const file = getSourceFileOfNode(name);
37633-
if (!file.isDeclarationFile && isExternalModule(file)) {
37634-
return grammarErrorOnNode(name, Diagnostics.Identifier_expected_0_is_a_reserved_word_at_the_top_level_of_a_module, idText(name));
37635-
}
37636-
}
37637-
return false;
37638-
}
37639-
3764037624
function checkGrammarFunctionLikeDeclaration(node: FunctionLikeDeclaration | MethodSignature): boolean {
3764137625
// Prevent cascading error by short-circuit
3764237626
const file = getSourceFileOfNode(node);
3764337627
return checkGrammarDecoratorsAndModifiers(node) ||
3764437628
checkGrammarTypeParameterList(node.typeParameters, file) ||
37645-
(isFunctionDeclaration(node) && checkGrammarAwaitIdentifier(node.name)) ||
3764637629
checkGrammarParameterList(node.parameters) ||
3764737630
checkGrammarArrowFunction(node, file) ||
3764837631
(isFunctionLikeDeclaration(node) && checkGrammarForUseStrictSimpleParameterList(node));
3764937632
}
3765037633

3765137634
function checkGrammarClassLikeDeclaration(node: ClassLikeDeclaration): boolean {
3765237635
const file = getSourceFileOfNode(node);
37653-
return (isClassDeclaration(node) && checkGrammarAwaitIdentifier(node.name)) ||
37654-
checkGrammarClassDeclarationHeritageClauses(node) ||
37636+
return checkGrammarClassDeclarationHeritageClauses(node) ||
3765537637
checkGrammarTypeParameterList(node.typeParameters, file);
3765637638
}
3765737639

@@ -38294,10 +38276,6 @@ namespace ts {
3829438276
}
3829538277
}
3829638278

38297-
if (isIdentifier(node.name) && checkGrammarAwaitIdentifier(node.name)) {
38298-
return true;
38299-
}
38300-
3830138279
if (node.dotDotDotToken && node.initializer) {
3830238280
// Error on equals token which immediately precedes the initializer
3830338281
return grammarErrorAtPos(node, node.initializer.pos - 1, 1, Diagnostics.A_rest_element_cannot_have_an_initializer);
@@ -38361,9 +38339,6 @@ namespace ts {
3836138339
}
3836238340
}
3836338341
}
38364-
if (isIdentifier(node.name) && checkGrammarAwaitIdentifier(node.name)) {
38365-
return true;
38366-
}
3836738342

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

0 commit comments

Comments
 (0)