diff --git a/src/services/syntax/parser.ts b/src/services/syntax/parser.ts index f55577dded1..b96e0ef35c9 100644 --- a/src/services/syntax/parser.ts +++ b/src/services/syntax/parser.ts @@ -2034,7 +2034,7 @@ module TypeScript.Parser { // grammar walker. var initializer = tokenKind === SyntaxKind.SemicolonToken ? undefined - : tokenKind === SyntaxKind.VarKeyword + : isVariableDeclaration(tokenKind) ? disallowInAnd(parseVariableDeclaration) : disallowInAnd(parseExpression) @@ -2354,7 +2354,26 @@ module TypeScript.Parser { } function isVariableStatement(modifierCount: number): boolean { - return peekToken(modifierCount).kind === SyntaxKind.VarKeyword; + return isVariableDeclaration(peekToken(modifierCount).kind); + } + + function isVariableDeclaration(tokenKind: SyntaxKind) { + // 'var' and 'const' are keywords. So we always know they start a variable declaration. + if (tokenKind === SyntaxKind.VarKeyword || tokenKind === SyntaxKind.ConstKeyword) { + return true; + } + + // 'let' is future reserved strict keyword. So it's only a keyword if we're in strict + // mode, or if we see an identifier following. + if (tokenKind === SyntaxKind.LetKeyword) { + if (inStrictModeContext()) { + return true; + } + + return isIdentifier(peekToken(1)); + } + + return false; } function parseVariableStatement(): VariableStatementSyntax { @@ -2362,14 +2381,16 @@ module TypeScript.Parser { // var VariableDeclarationList[In, ?Yield]; return new VariableStatementSyntax(contextFlags, - parseModifiers(), allowInAnd(parseVariableDeclaration), eatExplicitOrAutomaticSemicolon(/*allowWithoutNewline:*/ false)); + parseModifiers(), + allowInAnd(parseVariableDeclaration), + eatExplicitOrAutomaticSemicolon(/*allowWithoutNewline:*/ false)); } function parseVariableDeclaration(): VariableDeclarationSyntax { // Debug.assert(currentToken().kind === SyntaxKind.VarKeyword); return new VariableDeclarationSyntax(contextFlags, - eatToken(SyntaxKind.VarKeyword), + consumeToken(currentToken()), parseSeparatedSyntaxList(ListParsingState.VariableDeclaration_VariableDeclarators)); } diff --git a/src/services/syntax/syntaxGenerator.ts b/src/services/syntax/syntaxGenerator.ts index 798d5962a7f..924aaec7e6e 100644 --- a/src/services/syntax/syntaxGenerator.ts +++ b/src/services/syntax/syntaxGenerator.ts @@ -200,7 +200,7 @@ var definitions:ITypeDefinition[] = [ name: 'VariableDeclarationSyntax', baseType: 'ISyntaxNode', children: [ - { name: 'varKeyword', isToken: true }, + { name: 'varConstOrLetKeyword', isToken: true }, { name: 'variableDeclarators', isSeparatedList: true, requiresAtLeastOneItem: true, elementType: 'VariableDeclaratorSyntax' } ] },