@@ -102,6 +102,7 @@ module ts {
102
102
// This list is a work in progress. Add missing node kinds to improve their error
103
103
// spans.
104
104
case SyntaxKind . VariableDeclaration :
105
+ case SyntaxKind . BindingElement :
105
106
case SyntaxKind . ClassDeclaration :
106
107
case SyntaxKind . InterfaceDeclaration :
107
108
case SyntaxKind . ModuleDeclaration :
@@ -216,13 +217,14 @@ module ts {
216
217
case SyntaxKind . PropertyAssignment :
217
218
case SyntaxKind . ShorthandPropertyAssignment :
218
219
case SyntaxKind . VariableDeclaration :
220
+ case SyntaxKind . BindingElement :
219
221
return children ( node . modifiers ) ||
220
- child ( ( < VariableDeclaration > node ) . propertyName ) ||
221
- child ( ( < VariableDeclaration > node ) . dotDotDotToken ) ||
222
- child ( ( < VariableDeclaration > node ) . name ) ||
223
- child ( ( < VariableDeclaration > node ) . questionToken ) ||
224
- child ( ( < VariableDeclaration > node ) . type ) ||
225
- child ( ( < VariableDeclaration > node ) . initializer ) ;
222
+ child ( ( < VariableLikeDeclaration > node ) . propertyName ) ||
223
+ child ( ( < VariableLikeDeclaration > node ) . dotDotDotToken ) ||
224
+ child ( ( < VariableLikeDeclaration > node ) . name ) ||
225
+ child ( ( < VariableLikeDeclaration > node ) . questionToken ) ||
226
+ child ( ( < VariableLikeDeclaration > node ) . type ) ||
227
+ child ( ( < VariableLikeDeclaration > node ) . initializer ) ;
226
228
case SyntaxKind . FunctionType :
227
229
case SyntaxKind . ConstructorType :
228
230
case SyntaxKind . CallSignature :
@@ -580,7 +582,8 @@ module ts {
580
582
case SyntaxKind . Property :
581
583
case SyntaxKind . EnumMember :
582
584
case SyntaxKind . PropertyAssignment :
583
- return ( < VariableDeclaration > parent ) . initializer === node ;
585
+ case SyntaxKind . BindingElement :
586
+ return ( < VariableLikeDeclaration > parent ) . initializer === node ;
584
587
case SyntaxKind . ExpressionStatement :
585
588
case SyntaxKind . IfStatement :
586
589
case SyntaxKind . DoStatement :
@@ -679,6 +682,7 @@ module ts {
679
682
case SyntaxKind . TypeParameter :
680
683
case SyntaxKind . Parameter :
681
684
case SyntaxKind . VariableDeclaration :
685
+ case SyntaxKind . BindingElement :
682
686
case SyntaxKind . Property :
683
687
case SyntaxKind . PropertyAssignment :
684
688
case SyntaxKind . ShorthandPropertyAssignment :
@@ -1892,12 +1896,7 @@ module ts {
1892
1896
// [+GeneratorParameter]BindingIdentifier[Yield]Initializer[In]opt
1893
1897
// [~GeneratorParameter]BindingIdentifier[?Yield]Initializer[In, ?Yield]opt
1894
1898
1895
- var savedYieldContext = inYieldContext ( ) ;
1896
- if ( inGeneratorParameterContext ( ) ) {
1897
- setYieldContext ( true ) ;
1898
- }
1899
- node . name = parseIdentifierOrPattern ( SyntaxKind . Parameter ) ;
1900
- setYieldContext ( savedYieldContext ) ;
1899
+ node . name = inGeneratorParameterContext ( ) ? doInYieldContext ( parseIdentifierOrPattern ) : parseIdentifierOrPattern ( ) ;
1901
1900
1902
1901
if ( getFullWidth ( node . name ) === 0 && node . flags === 0 && isModifier ( token ) ) {
1903
1902
// in cases like
@@ -1913,9 +1912,7 @@ module ts {
1913
1912
1914
1913
node . questionToken = parseOptionalToken ( SyntaxKind . QuestionToken ) ;
1915
1914
node . type = parseParameterType ( ) ;
1916
- node . initializer = inGeneratorParameterContext ( )
1917
- ? doOutsideOfYieldContext ( parseParameterInitializer )
1918
- : parseParameterInitializer ( ) ;
1915
+ node . initializer = inGeneratorParameterContext ( ) ? doOutsideOfYieldContext ( parseParameterInitializer ) : parseParameterInitializer ( ) ;
1919
1916
1920
1917
// Do not check for initializers in an ambient context for parameters. This is not
1921
1918
// a grammar error because the grammar allows arbitrary call signatures in
@@ -3691,11 +3688,11 @@ module ts {
3691
3688
3692
3689
// DECLARATIONS
3693
3690
3694
- function parseBindingElement ( kind : SyntaxKind , context : ParsingContext ) : BindingElement {
3691
+ function parseBindingElement ( context : ParsingContext ) : BindingElement {
3695
3692
if ( context === ParsingContext . ArrayBindingElements && token === SyntaxKind . CommaToken ) {
3696
3693
return < BindingElement > createNode ( SyntaxKind . OmittedExpression ) ;
3697
3694
}
3698
- var node = < BindingElement > createNode ( kind ) ;
3695
+ var node = < BindingElement > createNode ( SyntaxKind . BindingElement ) ;
3699
3696
if ( context === ParsingContext . ObjectBindingElements ) {
3700
3697
// TODO(andersh): Handle computed properties
3701
3698
var id = parsePropertyName ( ) ;
@@ -3705,32 +3702,32 @@ module ts {
3705
3702
else {
3706
3703
parseExpected ( SyntaxKind . ColonToken ) ;
3707
3704
node . propertyName = < Identifier > id ;
3708
- node . name = parseIdentifierOrPattern ( kind ) ;
3705
+ node . name = parseIdentifierOrPattern ( ) ;
3709
3706
}
3710
3707
}
3711
3708
else {
3712
- node . name = parseIdentifierOrPattern ( kind ) ;
3709
+ node . name = parseIdentifierOrPattern ( ) ;
3713
3710
}
3714
3711
node . initializer = parseInitializer ( /*inParameter*/ false ) ;
3715
3712
return finishNode ( node ) ;
3716
3713
}
3717
3714
3718
- function parseBindingList ( kind : SyntaxKind , context : ParsingContext ) : NodeArray < BindingElement > {
3719
- return parseDelimitedList ( context , ( ) => parseBindingElement ( kind , context ) ) ;
3715
+ function parseBindingList ( context : ParsingContext ) : NodeArray < BindingElement > {
3716
+ return parseDelimitedList ( context , ( ) => parseBindingElement ( context ) ) ;
3720
3717
}
3721
3718
3722
- function parseObjectBindingPattern ( kind : SyntaxKind ) : BindingPattern {
3719
+ function parseObjectBindingPattern ( ) : BindingPattern {
3723
3720
var node = < BindingPattern > createNode ( SyntaxKind . ObjectBindingPattern ) ;
3724
3721
parseExpected ( SyntaxKind . OpenBraceToken ) ;
3725
- node . elements = parseBindingList ( kind , ParsingContext . ObjectBindingElements ) ;
3722
+ node . elements = parseBindingList ( ParsingContext . ObjectBindingElements ) ;
3726
3723
parseExpected ( SyntaxKind . CloseBraceToken ) ;
3727
3724
return finishNode ( node ) ;
3728
3725
}
3729
3726
3730
- function parseArrayBindingPattern ( kind : SyntaxKind ) : BindingPattern {
3727
+ function parseArrayBindingPattern ( ) : BindingPattern {
3731
3728
var node = < BindingPattern > createNode ( SyntaxKind . ArrayBindingPattern ) ;
3732
3729
parseExpected ( SyntaxKind . OpenBracketToken ) ;
3733
- node . elements = parseBindingList ( kind , ParsingContext . ArrayBindingElements ) ;
3730
+ node . elements = parseBindingList ( ParsingContext . ArrayBindingElements ) ;
3734
3731
parseExpected ( SyntaxKind . CloseBracketToken ) ;
3735
3732
return finishNode ( node ) ;
3736
3733
}
@@ -3739,19 +3736,19 @@ module ts {
3739
3736
return token === SyntaxKind . OpenBraceToken || token === SyntaxKind . OpenBracketToken || isIdentifier ( ) ;
3740
3737
}
3741
3738
3742
- function parseIdentifierOrPattern ( kind : SyntaxKind ) : Identifier | BindingPattern {
3739
+ function parseIdentifierOrPattern ( ) : Identifier | BindingPattern {
3743
3740
if ( token === SyntaxKind . OpenBracketToken ) {
3744
- return parseArrayBindingPattern ( kind ) ;
3741
+ return parseArrayBindingPattern ( ) ;
3745
3742
}
3746
3743
if ( token === SyntaxKind . OpenBraceToken ) {
3747
- return parseObjectBindingPattern ( kind ) ;
3744
+ return parseObjectBindingPattern ( ) ;
3748
3745
}
3749
3746
return parseIdentifier ( ) ;
3750
3747
}
3751
3748
3752
3749
function parseVariableDeclaration ( ) : VariableDeclaration {
3753
3750
var node = < VariableDeclaration > createNode ( SyntaxKind . VariableDeclaration ) ;
3754
- node . name = parseIdentifierOrPattern ( SyntaxKind . VariableDeclaration ) ;
3751
+ node . name = parseIdentifierOrPattern ( ) ;
3755
3752
node . type = parseTypeAnnotation ( ) ;
3756
3753
node . initializer = parseInitializer ( /*inParameter*/ false ) ;
3757
3754
return finishNode ( node ) ;
@@ -4492,6 +4489,7 @@ module ts {
4492
4489
4493
4490
case SyntaxKind . EnumDeclaration : return checkEnumDeclaration ( < EnumDeclaration > node ) ;
4494
4491
case SyntaxKind . BinaryExpression : return checkBinaryExpression ( < BinaryExpression > node ) ;
4492
+ case SyntaxKind . BindingElement : return checkBindingElement ( < BindingElement > node ) ;
4495
4493
case SyntaxKind . CatchClause : return checkCatchClause ( < CatchClause > node ) ;
4496
4494
case SyntaxKind . ClassDeclaration : return checkClassDeclaration ( < ClassDeclaration > node ) ;
4497
4495
case SyntaxKind . ComputedPropertyName : return checkComputedPropertyName ( < ComputedPropertyName > node ) ;
@@ -5594,14 +5592,22 @@ module ts {
5594
5592
return checkTypeArguments ( node . typeArguments ) ;
5595
5593
}
5596
5594
5595
+ function checkBindingElement ( node : BindingElement ) {
5596
+ if ( node . parserContextFlags & ParserContextFlags . StrictMode && isEvalOrArgumentsIdentifier ( node . name ) ) {
5597
+ // It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code
5598
+ // and its Identifier is eval or arguments
5599
+ return reportInvalidUseInStrictMode ( < Identifier > node . name ) ;
5600
+ }
5601
+ }
5602
+
5597
5603
function checkVariableDeclaration ( node : VariableDeclaration ) {
5598
5604
if ( inAmbientContext ) {
5599
5605
if ( isBindingPattern ( node . name ) ) {
5600
5606
return grammarErrorOnNode ( node , Diagnostics . Destructuring_declarations_are_not_allowed_in_ambient_contexts ) ;
5601
5607
}
5602
5608
if ( node . initializer ) {
5603
- var equalsPos = node . type ? skipTrivia ( sourceText , node . type . end ) : skipTrivia ( sourceText , node . name . end ) ;
5604
- return grammarErrorAtPos ( equalsPos , "=" . length , Diagnostics . Initializers_are_not_allowed_in_ambient_contexts ) ;
5609
+ // Error on equals token which immediate precedes the initializer
5610
+ return grammarErrorAtPos ( node . initializer . pos - 1 , 1 , Diagnostics . Initializers_are_not_allowed_in_ambient_contexts ) ;
5605
5611
}
5606
5612
}
5607
5613
else {
0 commit comments