@@ -11,6 +11,18 @@ module ts {
11
11
var nextNodeId = 1 ;
12
12
var nextMergeId = 1 ;
13
13
14
+ export function getDeclarationOfKind ( symbol : Symbol , kind : SyntaxKind ) : Declaration {
15
+ var declarations = symbol . declarations ;
16
+ for ( var i = 0 ; i < declarations . length ; i ++ ) {
17
+ var declaration = declarations [ i ] ;
18
+ if ( declaration . kind === kind ) {
19
+ return declaration ;
20
+ }
21
+ }
22
+
23
+ return undefined ;
24
+ }
25
+
14
26
/// fullTypeCheck denotes if this instance of the typechecker will be used to get semantic diagnostics.
15
27
/// If fullTypeCheck === true - then typechecker should do every possible check to produce all errors
16
28
/// If fullTypeCheck === false - typechecker can shortcut and skip checks that only produce errors.
@@ -49,7 +61,9 @@ module ts {
49
61
getApparentType : getApparentType ,
50
62
typeToString : typeToString ,
51
63
symbolToString : symbolToString ,
52
- getAugmentedPropertiesOfApparentType : getAugmentedPropertiesOfApparentType
64
+ getAugmentedPropertiesOfApparentType : getAugmentedPropertiesOfApparentType ,
65
+ getRootSymbol : getRootSymbol ,
66
+ getContextualType : getContextualType
53
67
} ;
54
68
55
69
var undefinedSymbol = createSymbol ( SymbolFlags . Property | SymbolFlags . Transient , "undefined" ) ;
@@ -568,18 +582,6 @@ module ts {
568
582
return false ;
569
583
}
570
584
571
- function getDeclarationOfKind ( symbol : Symbol , kind : SyntaxKind ) : Declaration {
572
- var declarations = symbol . declarations ;
573
- for ( var i = 0 ; i < declarations . length ; i ++ ) {
574
- var declaration = declarations [ i ] ;
575
- if ( declaration . kind === kind ) {
576
- return declaration ;
577
- }
578
- }
579
-
580
- return undefined ;
581
- }
582
-
583
585
function findConstructorDeclaration ( node : ClassDeclaration ) : ConstructorDeclaration {
584
586
var members = node . members ;
585
587
for ( var i = 0 ; i < members . length ; i ++ ) {
@@ -906,7 +908,7 @@ module ts {
906
908
// Get qualified name
907
909
if ( enclosingDeclaration &&
908
910
// Properties/methods/Signatures/Constructors/TypeParameters do not need qualification
909
- ! ( symbol . flags & SymbolFlags . PropertyOrAccessor & SymbolFlags . Signature & SymbolFlags . Constructor & SymbolFlags . Method & SymbolFlags . TypeParameter ) ) {
911
+ ! ( symbol . flags & ( SymbolFlags . PropertyOrAccessor | SymbolFlags . Signature | SymbolFlags . Constructor | SymbolFlags . Method | SymbolFlags . TypeParameter ) ) ) {
910
912
var symbolName : string ;
911
913
while ( symbol ) {
912
914
var isFirstName = ! symbolName ;
@@ -2248,13 +2250,12 @@ module ts {
2248
2250
return emptyObjectType ;
2249
2251
}
2250
2252
var type = getDeclaredTypeOfSymbol ( symbol ) ;
2251
- var name = symbol . name ;
2252
2253
if ( ! ( type . flags & TypeFlags . ObjectType ) ) {
2253
- error ( getTypeDeclaration ( symbol ) , Diagnostics . Global_type_0_must_be_a_class_or_interface_type , name ) ;
2254
+ error ( getTypeDeclaration ( symbol ) , Diagnostics . Global_type_0_must_be_a_class_or_interface_type , symbol . name ) ;
2254
2255
return emptyObjectType ;
2255
2256
}
2256
2257
if ( ( ( < InterfaceType > type ) . typeParameters ? ( < InterfaceType > type ) . typeParameters . length : 0 ) !== arity ) {
2257
- error ( getTypeDeclaration ( symbol ) , Diagnostics . Global_type_0_must_have_1_type_parameter_s , name , arity ) ;
2258
+ error ( getTypeDeclaration ( symbol ) , Diagnostics . Global_type_0_must_have_1_type_parameter_s , symbol . name , arity ) ;
2258
2259
return emptyObjectType ;
2259
2260
}
2260
2261
return < ObjectType > type ;
@@ -3156,6 +3157,7 @@ module ts {
3156
3157
symbol . declarations = p . declarations ;
3157
3158
symbol . parent = p . parent ;
3158
3159
symbol . type = widenedTypes [ index ++ ] ;
3160
+ symbol . target = p ;
3159
3161
if ( p . valueDeclaration ) symbol . valueDeclaration = p . valueDeclaration ;
3160
3162
members [ symbol . name ] = symbol ;
3161
3163
} ) ;
@@ -3552,7 +3554,8 @@ module ts {
3552
3554
return false ;
3553
3555
}
3554
3556
3555
- function checkSuperExpression ( node : Node , isCallExpression : boolean ) : Type {
3557
+ function checkSuperExpression ( node : Node ) : Type {
3558
+ var isCallExpression = node . parent . kind === SyntaxKind . CallExpression && ( < CallExpression > node . parent ) . func === node ;
3556
3559
var enclosingClass = < ClassDeclaration > getAncestor ( node , SyntaxKind . ClassDeclaration ) ;
3557
3560
var baseClass : Type ;
3558
3561
if ( enclosingClass && enclosingClass . baseType ) {
@@ -3828,6 +3831,7 @@ module ts {
3828
3831
prop . parent = member . parent ;
3829
3832
if ( member . valueDeclaration ) prop . valueDeclaration = member . valueDeclaration ;
3830
3833
prop . type = type ;
3834
+ prop . target = member ;
3831
3835
member = prop ;
3832
3836
}
3833
3837
else {
@@ -4186,7 +4190,7 @@ module ts {
4186
4190
4187
4191
function resolveCallExpression ( node : CallExpression ) : Signature {
4188
4192
if ( node . func . kind === SyntaxKind . SuperKeyword ) {
4189
- var superType = checkSuperExpression ( node . func , true ) ;
4193
+ var superType = checkSuperExpression ( node . func ) ;
4190
4194
if ( superType !== unknownType ) {
4191
4195
return resolveCall ( node , getSignaturesOfType ( superType , SignatureKind . Construct ) ) ;
4192
4196
}
@@ -4834,7 +4838,7 @@ module ts {
4834
4838
case SyntaxKind . ThisKeyword :
4835
4839
return checkThisExpression ( node ) ;
4836
4840
case SyntaxKind . SuperKeyword :
4837
- return checkSuperExpression ( node , false ) ;
4841
+ return checkSuperExpression ( node ) ;
4838
4842
case SyntaxKind . NullKeyword :
4839
4843
return nullType ;
4840
4844
case SyntaxKind . TrueKeyword :
@@ -6591,24 +6595,6 @@ module ts {
6591
6595
( < Declaration > name . parent ) . name === name ;
6592
6596
}
6593
6597
6594
- // True if the given identifier, string literal, or number literal is the name of a declaration node
6595
- function isDeclarationOrFunctionExpressionOrCatchVariableName ( name : Node ) : boolean {
6596
- if ( name . kind !== SyntaxKind . Identifier && name . kind !== SyntaxKind . StringLiteral && name . kind !== SyntaxKind . NumericLiteral ) {
6597
- return false ;
6598
- }
6599
-
6600
- var parent = name . parent ;
6601
- if ( isDeclaration ( parent ) || parent . kind === SyntaxKind . FunctionExpression ) {
6602
- return ( < Declaration > parent ) . name === name ;
6603
- }
6604
-
6605
- if ( parent . kind === SyntaxKind . CatchBlock ) {
6606
- return ( < CatchBlock > parent ) . variable === name ;
6607
- }
6608
-
6609
- return false ;
6610
- }
6611
-
6612
6598
function isTypeDeclaration ( node : Node ) : boolean {
6613
6599
switch ( node . kind ) {
6614
6600
case SyntaxKind . TypeParameter :
@@ -6619,28 +6605,6 @@ module ts {
6619
6605
}
6620
6606
}
6621
6607
6622
- function isDeclaration ( node : Node ) : boolean {
6623
- switch ( node . kind ) {
6624
- case SyntaxKind . TypeParameter :
6625
- case SyntaxKind . Parameter :
6626
- case SyntaxKind . VariableDeclaration :
6627
- case SyntaxKind . Property :
6628
- case SyntaxKind . PropertyAssignment :
6629
- case SyntaxKind . EnumMember :
6630
- case SyntaxKind . Method :
6631
- case SyntaxKind . FunctionDeclaration :
6632
- case SyntaxKind . GetAccessor :
6633
- case SyntaxKind . SetAccessor :
6634
- case SyntaxKind . ClassDeclaration :
6635
- case SyntaxKind . InterfaceDeclaration :
6636
- case SyntaxKind . EnumDeclaration :
6637
- case SyntaxKind . ModuleDeclaration :
6638
- case SyntaxKind . ImportDeclaration :
6639
- return true ;
6640
- }
6641
- return false ;
6642
- }
6643
-
6644
6608
// True if the given identifier is part of a type reference
6645
6609
function isTypeReferenceIdentifier ( entityName : EntityName ) : boolean {
6646
6610
var node : Node = entityName ;
@@ -6688,6 +6652,7 @@ module ts {
6688
6652
case SyntaxKind . Parameter :
6689
6653
case SyntaxKind . Property :
6690
6654
case SyntaxKind . EnumMember :
6655
+ case SyntaxKind . PropertyAssignment :
6691
6656
return ( < VariableDeclaration > parent ) . initializer === node ;
6692
6657
case SyntaxKind . ExpressionStatement :
6693
6658
case SyntaxKind . IfStatement :
@@ -6817,6 +6782,11 @@ module ts {
6817
6782
/*all meanings*/ SymbolFlags . Value | SymbolFlags . Type | SymbolFlags . Namespace | SymbolFlags . Import ) ;
6818
6783
}
6819
6784
6785
+ if ( isInRightSideOfImportOrExportAssignment ( entityName ) ) {
6786
+ // Since we already checked for ExportAssignment, this really could only be an Import
6787
+ return getSymbolOfPartOfRightHandSideOfImport ( entityName ) ;
6788
+ }
6789
+
6820
6790
if ( isRightSideOfQualifiedNameOrPropertyAccess ( entityName ) ) {
6821
6791
entityName = entityName . parent ;
6822
6792
}
@@ -6853,6 +6823,17 @@ module ts {
6853
6823
}
6854
6824
6855
6825
function getSymbolInfo ( node : Node ) {
6826
+ if ( isDeclarationOrFunctionExpressionOrCatchVariableName ( node ) ) {
6827
+ // This is a declaration, call getSymbolOfNode
6828
+ return getSymbolOfNode ( node . parent ) ;
6829
+ }
6830
+
6831
+ if ( node . kind === SyntaxKind . Identifier && isInRightSideOfImportOrExportAssignment ( node ) ) {
6832
+ return node . parent . kind === SyntaxKind . ExportAssignment
6833
+ ? getSymbolOfEntityName ( < Identifier > node )
6834
+ : getSymbolOfPartOfRightHandSideOfImport ( node ) ;
6835
+ }
6836
+
6856
6837
switch ( node . kind ) {
6857
6838
case SyntaxKind . Identifier :
6858
6839
case SyntaxKind . PropertyAccess :
@@ -6863,7 +6844,7 @@ module ts {
6863
6844
case SyntaxKind . SuperKeyword :
6864
6845
var type = checkExpression ( node ) ;
6865
6846
return type . symbol ;
6866
-
6847
+
6867
6848
case SyntaxKind . ConstructorKeyword :
6868
6849
// constructor keyword for an overload, should take us to the definition if it exist
6869
6850
var constructorDeclaration = node . parent ;
@@ -6873,23 +6854,22 @@ module ts {
6873
6854
return undefined ;
6874
6855
6875
6856
case SyntaxKind . StringLiteral :
6876
- // Property access
6877
- if ( node . parent . kind === SyntaxKind . IndexedAccess && ( < IndexedAccess > node . parent ) . index === node ) {
6878
- var objectType = checkExpression ( ( < IndexedAccess > node . parent ) . object ) ;
6879
- if ( objectType === unknownType ) return undefined ;
6880
- var apparentType = getApparentType ( objectType ) ;
6881
- if ( < Type > apparentType === unknownType ) return undefined ;
6882
- return getPropertyOfApparentType ( apparentType , ( < LiteralExpression > node ) . text ) ;
6883
- }
6884
6857
// External module name in an import declaration
6885
- else if ( node . parent . kind === SyntaxKind . ImportDeclaration && ( < ImportDeclaration > node . parent ) . externalModuleName === node ) {
6858
+ if ( node . parent . kind === SyntaxKind . ImportDeclaration && ( < ImportDeclaration > node . parent ) . externalModuleName === node ) {
6886
6859
var importSymbol = getSymbolOfNode ( node . parent ) ;
6887
6860
var moduleType = getTypeOfSymbol ( importSymbol ) ;
6888
6861
return moduleType ? moduleType . symbol : undefined ;
6889
6862
}
6890
- // External module name in an ambient declaration
6891
- else if ( node . parent . kind === SyntaxKind . ModuleDeclaration ) {
6892
- return getSymbolOfNode ( node . parent ) ;
6863
+
6864
+ // Intentinal fallthrough
6865
+ case SyntaxKind . NumericLiteral :
6866
+ // index access
6867
+ if ( node . parent . kind == SyntaxKind . IndexedAccess && ( < IndexedAccess > node . parent ) . index === node ) {
6868
+ var objectType = checkExpression ( ( < IndexedAccess > node . parent ) . object ) ;
6869
+ if ( objectType === unknownType ) return undefined ;
6870
+ var apparentType = getApparentType ( objectType ) ;
6871
+ if ( < Type > apparentType === unknownType ) return undefined ;
6872
+ return getPropertyOfApparentType ( apparentType , ( < LiteralExpression > node ) . text ) ;
6893
6873
}
6894
6874
break ;
6895
6875
}
@@ -6927,11 +6907,7 @@ module ts {
6927
6907
}
6928
6908
6929
6909
if ( isInRightSideOfImportOrExportAssignment ( node ) ) {
6930
- var symbol : Symbol ;
6931
- symbol = node . parent . kind === SyntaxKind . ExportAssignment
6932
- ? getSymbolInfo ( node )
6933
- : getSymbolOfPartOfRightHandSideOfImport ( node ) ;
6934
-
6910
+ var symbol = getSymbolInfo ( node ) ;
6935
6911
var declaredType = getDeclaredTypeOfSymbol ( symbol ) ;
6936
6912
return declaredType !== unknownType ? declaredType : getTypeOfSymbol ( symbol ) ;
6937
6913
}
@@ -6983,6 +6959,10 @@ module ts {
6983
6959
}
6984
6960
}
6985
6961
6962
+ function getRootSymbol ( symbol : Symbol ) {
6963
+ return ( symbol . flags & SymbolFlags . Transient ) ? getSymbolLinks ( symbol ) . target : symbol ;
6964
+ }
6965
+
6986
6966
// Emitter support
6987
6967
6988
6968
function isExternalModuleSymbol ( symbol : Symbol ) : boolean {
@@ -7101,7 +7081,20 @@ module ts {
7101
7081
function isImplementationOfOverload ( node : FunctionDeclaration ) {
7102
7082
if ( node . body ) {
7103
7083
var symbol = getSymbolOfNode ( node ) ;
7104
- return getSignaturesOfSymbol ( symbol ) . length > 1 ;
7084
+ var signaturesOfSymbol = getSignaturesOfSymbol ( symbol ) ;
7085
+ // If this function body corresponds to function with multiple signature, it is implementation of overload
7086
+ // eg: function foo(a: string): string;
7087
+ // function foo(a: number): number;
7088
+ // function foo(a: any) { // This is implementation of the overloads
7089
+ // return a;
7090
+ // }
7091
+ return signaturesOfSymbol . length > 1 ||
7092
+ // If there is single signature for the symbol, it is overload if that signature isnt coming from the node
7093
+ // eg: function foo(a: string): string;
7094
+ // function foo(a: any) { // This is implementation of the overloads
7095
+ // return a;
7096
+ // }
7097
+ ( signaturesOfSymbol . length === 1 && signaturesOfSymbol [ 0 ] . declaration !== node ) ;
7105
7098
}
7106
7099
return false ;
7107
7100
}
0 commit comments