@@ -2542,11 +2542,14 @@ module ts {
2542
2542
}
2543
2543
2544
2544
// STATEMENTS
2545
+ function parseStatementAllowingLetDeclaration ( ) {
2546
+ return parseStatement ( /*allowLetDeclarations*/ true ) ;
2547
+ }
2545
2548
2546
2549
function parseBlock ( ignoreMissingOpenBrace : boolean , checkForStrictMode : boolean ) : Block {
2547
2550
var node = < Block > createNode ( SyntaxKind . Block ) ;
2548
2551
if ( parseExpected ( SyntaxKind . OpenBraceToken ) || ignoreMissingOpenBrace ) {
2549
- node . statements = parseList ( ParsingContext . BlockStatements , checkForStrictMode , parseStatement ) ;
2552
+ node . statements = parseList ( ParsingContext . BlockStatements , checkForStrictMode , parseStatementAllowingLetDeclaration ) ;
2550
2553
parseExpected ( SyntaxKind . CloseBraceToken ) ;
2551
2554
}
2552
2555
else {
@@ -2592,8 +2595,8 @@ module ts {
2592
2595
parseExpected ( SyntaxKind . OpenParenToken ) ;
2593
2596
node . expression = parseExpression ( ) ;
2594
2597
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 ;
2597
2600
return finishNode ( node ) ;
2598
2601
}
2599
2602
@@ -2603,7 +2606,7 @@ module ts {
2603
2606
2604
2607
var saveInIterationStatement = inIterationStatement ;
2605
2608
inIterationStatement = ControlBlockContext . Nested ;
2606
- node . statement = parseStatement ( ) ;
2609
+ node . statement = parseStatement ( /*allowLetDeclarations*/ false ) ;
2607
2610
inIterationStatement = saveInIterationStatement ;
2608
2611
2609
2612
parseExpected ( SyntaxKind . WhileKeyword ) ;
@@ -2628,7 +2631,7 @@ module ts {
2628
2631
2629
2632
var saveInIterationStatement = inIterationStatement ;
2630
2633
inIterationStatement = ControlBlockContext . Nested ;
2631
- node . statement = parseStatement ( ) ;
2634
+ node . statement = parseStatement ( /*allowLetDeclarations*/ false ) ;
2632
2635
inIterationStatement = saveInIterationStatement ;
2633
2636
2634
2637
return finishNode ( node ) ;
@@ -2692,7 +2695,7 @@ module ts {
2692
2695
2693
2696
var saveInIterationStatement = inIterationStatement ;
2694
2697
inIterationStatement = ControlBlockContext . Nested ;
2695
- forOrForInStatement . statement = parseStatement ( ) ;
2698
+ forOrForInStatement . statement = parseStatement ( /*allowLetDeclarations*/ false ) ;
2696
2699
inIterationStatement = saveInIterationStatement ;
2697
2700
2698
2701
return finishNode ( forOrForInStatement ) ;
@@ -2803,7 +2806,7 @@ module ts {
2803
2806
parseExpected ( SyntaxKind . OpenParenToken ) ;
2804
2807
node . expression = parseExpression ( ) ;
2805
2808
parseExpected ( SyntaxKind . CloseParenToken ) ;
2806
- node . statement = parseStatement ( ) ;
2809
+ node . statement = parseStatement ( /*allowLetDeclarations*/ false ) ;
2807
2810
node = finishNode ( node ) ;
2808
2811
if ( isInStrictMode ) {
2809
2812
// Strict mode code may not include a WithStatement. The occurrence of a WithStatement in such
@@ -2818,15 +2821,15 @@ module ts {
2818
2821
parseExpected ( SyntaxKind . CaseKeyword ) ;
2819
2822
node . expression = parseExpression ( ) ;
2820
2823
parseExpected ( SyntaxKind . ColonToken ) ;
2821
- node . statements = parseList ( ParsingContext . SwitchClauseStatements , /*checkForStrictMode*/ false , parseStatement ) ;
2824
+ node . statements = parseList ( ParsingContext . SwitchClauseStatements , /*checkForStrictMode*/ false , parseStatementAllowingLetDeclaration ) ;
2822
2825
return finishNode ( node ) ;
2823
2826
}
2824
2827
2825
2828
function parseDefaultClause ( ) : CaseOrDefaultClause {
2826
2829
var node = < CaseOrDefaultClause > createNode ( SyntaxKind . DefaultClause ) ;
2827
2830
parseExpected ( SyntaxKind . DefaultKeyword ) ;
2828
2831
parseExpected ( SyntaxKind . ColonToken ) ;
2829
- node . statements = parseList ( ParsingContext . SwitchClauseStatements , /*checkForStrictMode*/ false , parseStatement ) ;
2832
+ node . statements = parseList ( ParsingContext . SwitchClauseStatements , /*checkForStrictMode*/ false , parseStatementAllowingLetDeclaration ) ;
2830
2833
return finishNode ( node ) ;
2831
2834
}
2832
2835
@@ -2933,9 +2936,9 @@ module ts {
2933
2936
return token === SyntaxKind . WhileKeyword || token === SyntaxKind . DoKeyword || token === SyntaxKind . ForKeyword ;
2934
2937
}
2935
2938
2936
- function parseStatementWithLabelSet ( ) : Statement {
2939
+ function parseStatementWithLabelSet ( allowLetDeclarations : boolean ) : Statement {
2937
2940
labelledStatementInfo . pushCurrentLabelSet ( isIterationStatementStart ( ) ) ;
2938
- var statement = parseStatement ( ) ;
2941
+ var statement = parseStatement ( allowLetDeclarations ) ;
2939
2942
labelledStatementInfo . pop ( ) ;
2940
2943
return statement ;
2941
2944
}
@@ -2944,7 +2947,7 @@ module ts {
2944
2947
return isIdentifier ( ) && lookAhead ( ( ) => nextToken ( ) === SyntaxKind . ColonToken ) ;
2945
2948
}
2946
2949
2947
- function parseLabelledStatement ( ) : LabeledStatement {
2950
+ function parseLabelledStatement ( allowLetDeclarations : boolean ) : LabeledStatement {
2948
2951
var node = < LabeledStatement > createNode ( SyntaxKind . LabeledStatement ) ;
2949
2952
node . label = parseIdentifier ( ) ;
2950
2953
parseExpected ( SyntaxKind . ColonToken ) ;
@@ -2956,7 +2959,7 @@ module ts {
2956
2959
2957
2960
// We only want to call parseStatementWithLabelSet when the label set is complete
2958
2961
// Therefore, keep parsing labels until we know we're done.
2959
- node . statement = isLabel ( ) ? parseLabelledStatement ( ) : parseStatementWithLabelSet ( ) ;
2962
+ node . statement = isLabel ( ) ? parseLabelledStatement ( allowLetDeclarations ) : parseStatementWithLabelSet ( allowLetDeclarations ) ;
2960
2963
return finishNode ( node ) ;
2961
2964
}
2962
2965
@@ -3022,14 +3025,14 @@ module ts {
3022
3025
}
3023
3026
}
3024
3027
3025
- function parseStatement ( ) : Statement {
3028
+ function parseStatement ( allowLetDeclarations : boolean ) : Statement {
3026
3029
switch ( token ) {
3027
3030
case SyntaxKind . OpenBraceToken :
3028
3031
return parseBlock ( /* ignoreMissingOpenBrace */ false , /*checkForStrictMode*/ false ) ;
3029
3032
case SyntaxKind . VarKeyword :
3030
3033
case SyntaxKind . LetKeyword :
3031
3034
case SyntaxKind . ConstKeyword :
3032
- return parseVariableStatement ( ) ;
3035
+ return parseVariableStatement ( allowLetDeclarations ) ;
3033
3036
case SyntaxKind . FunctionKeyword :
3034
3037
return parseFunctionDeclaration ( ) ;
3035
3038
case SyntaxKind . SemicolonToken :
@@ -3063,16 +3066,12 @@ module ts {
3063
3066
return parseDebuggerStatement ( ) ;
3064
3067
default :
3065
3068
if ( isLabel ( ) ) {
3066
- return parseLabelledStatement ( ) ;
3069
+ return parseLabelledStatement ( allowLetDeclarations ) ;
3067
3070
}
3068
3071
return parseExpressionStatement ( ) ;
3069
3072
}
3070
3073
}
3071
3074
3072
- function parseStatementOrFunction ( ) : Statement {
3073
- return token === SyntaxKind . FunctionKeyword ? parseFunctionDeclaration ( ) : parseStatement ( ) ;
3074
- }
3075
-
3076
3075
function parseAndCheckFunctionBody ( isConstructor : boolean ) : Block {
3077
3076
var initialPosition = scanner . getTokenPos ( ) ;
3078
3077
var errorCountBeforeBody = file . syntacticErrors . length ;
@@ -3109,7 +3108,7 @@ module ts {
3109
3108
grammarErrorAtPos ( initializerStart , initializerFirstTokenLength , Diagnostics . Initializers_are_not_allowed_in_ambient_contexts ) ;
3110
3109
}
3111
3110
if ( ! inAmbientContext && ! node . initializer && flags & NodeFlags . Const ) {
3112
- grammarErrorOnNode ( node , Diagnostics . Const_must_be_intialized ) ;
3111
+ grammarErrorOnNode ( node , Diagnostics . const_must_be_intialized ) ;
3113
3112
}
3114
3113
if ( isInStrictMode && isEvalOrArgumentsIdentifier ( node . name ) ) {
3115
3114
// It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code
@@ -3124,7 +3123,7 @@ module ts {
3124
3123
( ) => parseVariableDeclaration ( flags , noIn ) , /*allowTrailingComma*/ false ) ;
3125
3124
}
3126
3125
3127
- function parseVariableStatement ( pos ?: number , flags ?: NodeFlags ) : VariableStatement {
3126
+ function parseVariableStatement ( allowLetDeclarations : boolean , pos ?: number , flags ?: NodeFlags ) : VariableStatement {
3128
3127
var node = < VariableStatement > createNode ( SyntaxKind . VariableStatement , pos ) ;
3129
3128
if ( flags ) node . flags = flags ;
3130
3129
var errorCountBeforeVarStatement = file . syntacticErrors . length ;
@@ -3152,6 +3151,14 @@ module ts {
3152
3151
grammarErrorOnNode ( node , Diagnostics . const_variable_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher ) ;
3153
3152
}
3154
3153
}
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
+ }
3155
3162
return node ;
3156
3163
}
3157
3164
@@ -3784,7 +3791,7 @@ module ts {
3784
3791
case SyntaxKind . VarKeyword :
3785
3792
case SyntaxKind . LetKeyword :
3786
3793
case SyntaxKind . ConstKeyword :
3787
- result = parseVariableStatement ( pos , flags ) ;
3794
+ result = parseVariableStatement ( /*allowLetDeclarations*/ true , pos , flags ) ;
3788
3795
break ;
3789
3796
case SyntaxKind . FunctionKeyword :
3790
3797
result = parseFunctionDeclaration ( pos , flags ) ;
@@ -3832,7 +3839,7 @@ module ts {
3832
3839
var statementStart = scanner . getTokenPos ( ) ;
3833
3840
var statementFirstTokenLength = scanner . getTextPos ( ) - statementStart ;
3834
3841
var errorCountBeforeStatement = file . syntacticErrors . length ;
3835
- var statement = parseStatement ( ) ;
3842
+ var statement = parseStatement ( /*allowLetDeclarations*/ false ) ;
3836
3843
3837
3844
if ( inAmbientContext && file . syntacticErrors . length === errorCountBeforeStatement ) {
3838
3845
grammarErrorAtPos ( statementStart , statementFirstTokenLength , Diagnostics . Statements_are_not_allowed_in_ambient_contexts ) ;
0 commit comments