Skip to content

Commit 9ffa3cd

Browse files
author
Yui T
committed
Check grammar for let/const declaration for all targets
1 parent 90391fe commit 9ffa3cd

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

src/compiler/checker.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15973,18 +15973,27 @@ namespace ts {
1597315973
: Diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_of_statement;
1597415974
return grammarErrorOnFirstToken(variableList.declarations[1], diagnostic);
1597515975
}
15976-
const firstDeclaration = variableList.declarations[0];
15977-
if (firstDeclaration.initializer) {
15978-
const diagnostic = forInOrOfStatement.kind === SyntaxKind.ForInStatement
15979-
? Diagnostics.The_variable_declaration_of_a_for_in_statement_cannot_have_an_initializer
15980-
: Diagnostics.The_variable_declaration_of_a_for_of_statement_cannot_have_an_initializer;
15981-
return grammarErrorOnNode(firstDeclaration.name, diagnostic);
15982-
}
15983-
if (firstDeclaration.type) {
15984-
const diagnostic = forInOrOfStatement.kind === SyntaxKind.ForInStatement
15985-
? Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_use_a_type_annotation
15986-
: Diagnostics.The_left_hand_side_of_a_for_of_statement_cannot_use_a_type_annotation;
15987-
return grammarErrorOnNode(firstDeclaration, diagnostic);
15976+
const firstDeclaration = variableList.declarations[0]
15977+
15978+
// firstDeclaration can be undefined if there is variable declaration in for-of or for-in
15979+
// See http://www.ecma-international.org/ecma-262/6.0/#sec-for-in-and-for-of-statements for details
15980+
// For example:
15981+
// var let = 10;
15982+
// for (let of [1,2,3]) {} // this is invalid ES6 syntax
15983+
// for (let in [1,2,3]) {} // this is invalid ES6 syntax
15984+
if (firstDeclaration) {
15985+
if (firstDeclaration.initializer) {
15986+
const diagnostic = forInOrOfStatement.kind === SyntaxKind.ForInStatement
15987+
? Diagnostics.The_variable_declaration_of_a_for_in_statement_cannot_have_an_initializer
15988+
: Diagnostics.The_variable_declaration_of_a_for_of_statement_cannot_have_an_initializer;
15989+
return grammarErrorOnNode(firstDeclaration.name, diagnostic);
15990+
}
15991+
if (firstDeclaration.type) {
15992+
const diagnostic = forInOrOfStatement.kind === SyntaxKind.ForInStatement
15993+
? Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_use_a_type_annotation
15994+
: Diagnostics.The_left_hand_side_of_a_for_of_statement_cannot_use_a_type_annotation;
15995+
return grammarErrorOnNode(firstDeclaration, diagnostic);
15996+
}
1598815997
}
1598915998
}
1599015999
}
@@ -16172,7 +16181,7 @@ namespace ts {
1617216181
}
1617316182
}
1617416183

16175-
const checkLetConstNames = languageVersion >= ScriptTarget.ES6 && (isLet(node) || isConst(node));
16184+
const checkLetConstNames = (isLet(node) || isConst(node));
1617616185

1617716186
// 1. LexicalDeclaration : LetOrConst BindingList ;
1617816187
// It is a Syntax Error if the BoundNames of BindingList contains "let".
@@ -16186,7 +16195,7 @@ namespace ts {
1618616195

1618716196
function checkGrammarNameInLetOrConstDeclarations(name: Identifier | BindingPattern): boolean {
1618816197
if (name.kind === SyntaxKind.Identifier) {
16189-
if ((<Identifier>name).text === "let") {
16198+
if ((<Identifier>name).originalKeywordKind === SyntaxKind.LetKeyword) {
1619016199
return grammarErrorOnNode(name, Diagnostics.let_is_not_allowed_to_be_used_as_a_name_in_let_or_const_declarations);
1619116200
}
1619216201
}

0 commit comments

Comments
 (0)