@@ -2542,11 +2542,14 @@ module ts {
25422542 }
25432543
25442544 // STATEMENTS
2545+ function parseStatementAllowingLetDeclaration ( ) {
2546+ return parseStatement ( /*allowLetDeclarations*/ true ) ;
2547+ }
25452548
25462549 function parseBlock ( ignoreMissingOpenBrace : boolean , checkForStrictMode : boolean ) : Block {
25472550 var node = < Block > createNode ( SyntaxKind . Block ) ;
25482551 if ( parseExpected ( SyntaxKind . OpenBraceToken ) || ignoreMissingOpenBrace ) {
2549- node . statements = parseList ( ParsingContext . BlockStatements , checkForStrictMode , parseStatement ) ;
2552+ node . statements = parseList ( ParsingContext . BlockStatements , checkForStrictMode , parseStatementAllowingLetDeclaration ) ;
25502553 parseExpected ( SyntaxKind . CloseBraceToken ) ;
25512554 }
25522555 else {
@@ -2592,8 +2595,8 @@ module ts {
25922595 parseExpected ( SyntaxKind . OpenParenToken ) ;
25932596 node . expression = parseExpression ( ) ;
25942597 parseExpected ( SyntaxKind . CloseParenToken ) ;
2595- node . thenStatement = parseStatement ( ) ;
2596- node . elseStatement = parseOptional ( SyntaxKind . ElseKeyword ) ? parseStatement ( ) : undefined ;
2598+ node . thenStatement = parseStatement ( /*allowLetDeclarations*/ false ) ;
2599+ node . elseStatement = parseOptional ( SyntaxKind . ElseKeyword ) ? parseStatement ( /*allowLetDeclarations*/ false ) : undefined ;
25972600 return finishNode ( node ) ;
25982601 }
25992602
@@ -2603,7 +2606,7 @@ module ts {
26032606
26042607 var saveInIterationStatement = inIterationStatement ;
26052608 inIterationStatement = ControlBlockContext . Nested ;
2606- node . statement = parseStatement ( ) ;
2609+ node . statement = parseStatement ( /*allowLetDeclarations*/ false ) ;
26072610 inIterationStatement = saveInIterationStatement ;
26082611
26092612 parseExpected ( SyntaxKind . WhileKeyword ) ;
@@ -2628,7 +2631,7 @@ module ts {
26282631
26292632 var saveInIterationStatement = inIterationStatement ;
26302633 inIterationStatement = ControlBlockContext . Nested ;
2631- node . statement = parseStatement ( ) ;
2634+ node . statement = parseStatement ( /*allowLetDeclarations*/ false ) ;
26322635 inIterationStatement = saveInIterationStatement ;
26332636
26342637 return finishNode ( node ) ;
@@ -2692,7 +2695,7 @@ module ts {
26922695
26932696 var saveInIterationStatement = inIterationStatement ;
26942697 inIterationStatement = ControlBlockContext . Nested ;
2695- forOrForInStatement . statement = parseStatement ( ) ;
2698+ forOrForInStatement . statement = parseStatement ( /*allowLetDeclarations*/ false ) ;
26962699 inIterationStatement = saveInIterationStatement ;
26972700
26982701 return finishNode ( forOrForInStatement ) ;
@@ -2803,7 +2806,7 @@ module ts {
28032806 parseExpected ( SyntaxKind . OpenParenToken ) ;
28042807 node . expression = parseExpression ( ) ;
28052808 parseExpected ( SyntaxKind . CloseParenToken ) ;
2806- node . statement = parseStatement ( ) ;
2809+ node . statement = parseStatement ( /*allowLetDeclarations*/ false ) ;
28072810 node = finishNode ( node ) ;
28082811 if ( isInStrictMode ) {
28092812 // Strict mode code may not include a WithStatement. The occurrence of a WithStatement in such
@@ -2818,15 +2821,15 @@ module ts {
28182821 parseExpected ( SyntaxKind . CaseKeyword ) ;
28192822 node . expression = parseExpression ( ) ;
28202823 parseExpected ( SyntaxKind . ColonToken ) ;
2821- node . statements = parseList ( ParsingContext . SwitchClauseStatements , /*checkForStrictMode*/ false , parseStatement ) ;
2824+ node . statements = parseList ( ParsingContext . SwitchClauseStatements , /*checkForStrictMode*/ false , parseStatementAllowingLetDeclaration ) ;
28222825 return finishNode ( node ) ;
28232826 }
28242827
28252828 function parseDefaultClause ( ) : CaseOrDefaultClause {
28262829 var node = < CaseOrDefaultClause > createNode ( SyntaxKind . DefaultClause ) ;
28272830 parseExpected ( SyntaxKind . DefaultKeyword ) ;
28282831 parseExpected ( SyntaxKind . ColonToken ) ;
2829- node . statements = parseList ( ParsingContext . SwitchClauseStatements , /*checkForStrictMode*/ false , parseStatement ) ;
2832+ node . statements = parseList ( ParsingContext . SwitchClauseStatements , /*checkForStrictMode*/ false , parseStatementAllowingLetDeclaration ) ;
28302833 return finishNode ( node ) ;
28312834 }
28322835
@@ -2933,9 +2936,9 @@ module ts {
29332936 return token === SyntaxKind . WhileKeyword || token === SyntaxKind . DoKeyword || token === SyntaxKind . ForKeyword ;
29342937 }
29352938
2936- function parseStatementWithLabelSet ( ) : Statement {
2939+ function parseStatementWithLabelSet ( allowLetDeclarations : boolean ) : Statement {
29372940 labelledStatementInfo . pushCurrentLabelSet ( isIterationStatementStart ( ) ) ;
2938- var statement = parseStatement ( ) ;
2941+ var statement = parseStatement ( allowLetDeclarations ) ;
29392942 labelledStatementInfo . pop ( ) ;
29402943 return statement ;
29412944 }
@@ -2944,7 +2947,7 @@ module ts {
29442947 return isIdentifier ( ) && lookAhead ( ( ) => nextToken ( ) === SyntaxKind . ColonToken ) ;
29452948 }
29462949
2947- function parseLabelledStatement ( ) : LabeledStatement {
2950+ function parseLabelledStatement ( allowLetDeclarations : boolean ) : LabeledStatement {
29482951 var node = < LabeledStatement > createNode ( SyntaxKind . LabeledStatement ) ;
29492952 node . label = parseIdentifier ( ) ;
29502953 parseExpected ( SyntaxKind . ColonToken ) ;
@@ -2956,7 +2959,7 @@ module ts {
29562959
29572960 // We only want to call parseStatementWithLabelSet when the label set is complete
29582961 // Therefore, keep parsing labels until we know we're done.
2959- node . statement = isLabel ( ) ? parseLabelledStatement ( ) : parseStatementWithLabelSet ( ) ;
2962+ node . statement = isLabel ( ) ? parseLabelledStatement ( allowLetDeclarations ) : parseStatementWithLabelSet ( allowLetDeclarations ) ;
29602963 return finishNode ( node ) ;
29612964 }
29622965
@@ -3022,14 +3025,14 @@ module ts {
30223025 }
30233026 }
30243027
3025- function parseStatement ( ) : Statement {
3028+ function parseStatement ( allowLetDeclarations : boolean ) : Statement {
30263029 switch ( token ) {
30273030 case SyntaxKind . OpenBraceToken :
30283031 return parseBlock ( /* ignoreMissingOpenBrace */ false , /*checkForStrictMode*/ false ) ;
30293032 case SyntaxKind . VarKeyword :
30303033 case SyntaxKind . LetKeyword :
30313034 case SyntaxKind . ConstKeyword :
3032- return parseVariableStatement ( ) ;
3035+ return parseVariableStatement ( allowLetDeclarations ) ;
30333036 case SyntaxKind . FunctionKeyword :
30343037 return parseFunctionDeclaration ( ) ;
30353038 case SyntaxKind . SemicolonToken :
@@ -3063,16 +3066,12 @@ module ts {
30633066 return parseDebuggerStatement ( ) ;
30643067 default :
30653068 if ( isLabel ( ) ) {
3066- return parseLabelledStatement ( ) ;
3069+ return parseLabelledStatement ( allowLetDeclarations ) ;
30673070 }
30683071 return parseExpressionStatement ( ) ;
30693072 }
30703073 }
30713074
3072- function parseStatementOrFunction ( ) : Statement {
3073- return token === SyntaxKind . FunctionKeyword ? parseFunctionDeclaration ( ) : parseStatement ( ) ;
3074- }
3075-
30763075 function parseAndCheckFunctionBody ( isConstructor : boolean ) : Block {
30773076 var initialPosition = scanner . getTokenPos ( ) ;
30783077 var errorCountBeforeBody = file . syntacticErrors . length ;
@@ -3109,7 +3108,7 @@ module ts {
31093108 grammarErrorAtPos ( initializerStart , initializerFirstTokenLength , Diagnostics . Initializers_are_not_allowed_in_ambient_contexts ) ;
31103109 }
31113110 if ( ! inAmbientContext && ! node . initializer && flags & NodeFlags . Const ) {
3112- grammarErrorOnNode ( node , Diagnostics . Const_must_be_intialized ) ;
3111+ grammarErrorOnNode ( node , Diagnostics . const_must_be_intialized ) ;
31133112 }
31143113 if ( isInStrictMode && isEvalOrArgumentsIdentifier ( node . name ) ) {
31153114 // It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code
@@ -3124,7 +3123,7 @@ module ts {
31243123 ( ) => parseVariableDeclaration ( flags , noIn ) , /*allowTrailingComma*/ false ) ;
31253124 }
31263125
3127- function parseVariableStatement ( pos ?: number , flags ?: NodeFlags ) : VariableStatement {
3126+ function parseVariableStatement ( allowLetDeclarations : boolean , pos ?: number , flags ?: NodeFlags ) : VariableStatement {
31283127 var node = < VariableStatement > createNode ( SyntaxKind . VariableStatement , pos ) ;
31293128 if ( flags ) node . flags = flags ;
31303129 var errorCountBeforeVarStatement = file . syntacticErrors . length ;
@@ -3152,6 +3151,14 @@ module ts {
31523151 grammarErrorOnNode ( node , Diagnostics . const_variable_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher ) ;
31533152 }
31543153 }
3154+ else if ( ! allowLetDeclarations ) {
3155+ if ( node . flags & NodeFlags . Let ) {
3156+ grammarErrorOnNode ( node , Diagnostics . let_must_be_declared_inside_a_block ) ;
3157+ }
3158+ else if ( node . flags & NodeFlags . Const ) {
3159+ grammarErrorOnNode ( node , Diagnostics . const_must_be_declared_inside_a_block ) ;
3160+ }
3161+ }
31553162 return node ;
31563163 }
31573164
@@ -3784,7 +3791,7 @@ module ts {
37843791 case SyntaxKind . VarKeyword :
37853792 case SyntaxKind . LetKeyword :
37863793 case SyntaxKind . ConstKeyword :
3787- result = parseVariableStatement ( pos , flags ) ;
3794+ result = parseVariableStatement ( /*allowLetDeclarations*/ true , pos , flags ) ;
37883795 break ;
37893796 case SyntaxKind . FunctionKeyword :
37903797 result = parseFunctionDeclaration ( pos , flags ) ;
@@ -3832,7 +3839,7 @@ module ts {
38323839 var statementStart = scanner . getTokenPos ( ) ;
38333840 var statementFirstTokenLength = scanner . getTextPos ( ) - statementStart ;
38343841 var errorCountBeforeStatement = file . syntacticErrors . length ;
3835- var statement = parseStatement ( ) ;
3842+ var statement = parseStatement ( /*allowLetDeclarations*/ false ) ;
38363843
38373844 if ( inAmbientContext && file . syntacticErrors . length === errorCountBeforeStatement ) {
38383845 grammarErrorAtPos ( statementStart , statementFirstTokenLength , Diagnostics . Statements_are_not_allowed_in_ambient_contexts ) ;
0 commit comments