Skip to content

Commit 3e9206a

Browse files
committed
Consolidating strict mode reserved identifier checking in single method in binder
1 parent 60e855e commit 3e9206a

File tree

5 files changed

+69
-190
lines changed

5 files changed

+69
-190
lines changed

src/compiler/binder.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,23 @@ namespace ts {
560560
bindBlockScopedDeclaration(node, SymbolFlags.BlockScopedVariable, SymbolFlags.BlockScopedVariableExcludes);
561561
}
562562

563+
// The binder visits every node in the syntax tree so it is a convenient place to perform a single localized
564+
// check for reserved words used as identifiers in strict mode code.
565+
function checkStrictModeIdentifier(node: Identifier) {
566+
if (node.parserContextFlags & ParserContextFlags.StrictMode &&
567+
node.originalKeywordKind >= SyntaxKind.FirstFutureReservedWord &&
568+
node.originalKeywordKind <= SyntaxKind.LastFutureReservedWord &&
569+
!isIdentifierName(node)) {
570+
// Report error only if there are no parse errors in file
571+
if (!file.parseDiagnostics.length) {
572+
let message = getAncestor(node, SyntaxKind.ClassDeclaration) || getAncestor(node, SyntaxKind.ClassExpression) ?
573+
Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode :
574+
Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode;
575+
file.bindDiagnostics.push(createDiagnosticForNode(node, message, declarationNameToString(node)));
576+
}
577+
}
578+
}
579+
563580
function getDestructuringParameterName(node: Declaration) {
564581
return "__" + indexOf((<SignatureDeclaration>node.parent).parameters, node);
565582
}
@@ -588,6 +605,8 @@ namespace ts {
588605

589606
function bindWorker(node: Node) {
590607
switch (node.kind) {
608+
case SyntaxKind.Identifier:
609+
return checkStrictModeIdentifier(<Identifier>node);
591610
case SyntaxKind.TypeParameter:
592611
return declareSymbolAndAddToSymbolTable(<Declaration>node, SymbolFlags.TypeParameter, SymbolFlags.TypeParameterExcludes);
593612
case SyntaxKind.Parameter:

0 commit comments

Comments
 (0)