@@ -46,6 +46,8 @@ module ts {
46
46
47
47
var globals : SymbolTable = { } ;
48
48
49
+ var globalArraySymbol : Symbol ;
50
+
49
51
var globalObjectType : ObjectType ;
50
52
var globalFunctionType : ObjectType ;
51
53
var globalArrayType : ObjectType ;
@@ -616,8 +618,6 @@ module ts {
616
618
members , callSignatures , constructSignatures , stringIndexType , numberIndexType ) ;
617
619
}
618
620
619
- // Takes a VariableDeclaration because it could be an exported var from a module (VariableDeclaration),
620
- // a class or object type property (PropertyDeclaration), or a parameter property (ParameterDeclaration)
621
621
function isOptionalProperty ( propertySymbol : Symbol ) : boolean {
622
622
if ( propertySymbol . flags & SymbolFlags . Prototype ) {
623
623
return false ;
@@ -2069,7 +2069,7 @@ module ts {
2069
2069
return links . resolvedType ;
2070
2070
}
2071
2071
2072
- function getGlobalType ( name : string , arity : number = 0 ) : ObjectType {
2072
+ function getTypeOfGlobalSymbol ( symbol : Symbol , arity : number ) : ObjectType {
2073
2073
2074
2074
function getTypeDeclaration ( symbol : Symbol ) : Declaration {
2075
2075
var declarations = symbol . declarations ;
@@ -2079,14 +2079,11 @@ module ts {
2079
2079
case SyntaxKind . ClassDeclaration :
2080
2080
case SyntaxKind . InterfaceDeclaration :
2081
2081
case SyntaxKind . EnumDeclaration :
2082
- case SyntaxKind . TypeLiteral :
2083
- case SyntaxKind . FunctionDeclaration :
2084
2082
return declaration ;
2085
2083
}
2086
2084
}
2087
2085
}
2088
2086
2089
- var symbol = resolveName ( undefined , name , SymbolFlags . Type , Diagnostics . Cannot_find_global_type_0 , name ) ;
2090
2087
if ( ! symbol ) {
2091
2088
return emptyObjectType ;
2092
2089
}
@@ -2102,29 +2099,26 @@ module ts {
2102
2099
return < ObjectType > type ;
2103
2100
}
2104
2101
2105
- // arrayType argument is used as a backup in case if globalArrayType is not defined
2106
- function createArrayType ( elementType : Type , arrayType ?: ObjectType ) : Type {
2107
- var rootType = globalArrayType || arrayType ;
2108
- return rootType !== emptyObjectType ? createTypeReference ( < GenericType > rootType , [ elementType ] ) : emptyObjectType ;
2102
+ function getGlobalSymbol ( name : string ) : Symbol {
2103
+ return resolveName ( undefined , name , SymbolFlags . Type , Diagnostics . Cannot_find_global_type_0 , name ) ;
2104
+ }
2105
+
2106
+ function getGlobalType ( name : string ) : ObjectType {
2107
+ return getTypeOfGlobalSymbol ( getGlobalSymbol ( name ) , 0 ) ;
2108
+ }
2109
+
2110
+ function createArrayType ( elementType : Type ) : Type {
2111
+ // globalArrayType will be undefined if we get here during creation of the Array type. This for example happens if
2112
+ // user code augments the Array type with call or construct signatures that have an array type as the return type.
2113
+ // We instead use globalArraySymbol to obtain the (not yet fully constructed) Array type.
2114
+ var arrayType = globalArrayType || getDeclaredTypeOfSymbol ( globalArraySymbol ) ;
2115
+ return arrayType !== emptyObjectType ? createTypeReference ( < GenericType > arrayType , [ elementType ] ) : emptyObjectType ;
2109
2116
}
2110
2117
2111
2118
function getTypeFromArrayTypeNode ( node : ArrayTypeNode ) : Type {
2112
2119
var links = getNodeLinks ( node ) ;
2113
2120
if ( ! links . resolvedType ) {
2114
- var arrayType = globalArrayType ;
2115
- if ( ! arrayType ) {
2116
- // if user code contains augmentation for Array type that includes call\construct signatures with arrays as parameter\return types,
2117
- // then we might step here then during initialization of the global Array type when globalArrayType is not yet set.
2118
- // CODE: interface Array<T> { (): number[] }
2119
- // in this case just resolve name 'Array' again and get declared type of symbol.
2120
- // this type is the one that eventually should be set as 'globalArrayType'.
2121
- // NOTE: this is specific to signatures since got signatures we realize parameter\return types.
2122
- var arrayTypeSymbol = resolveName ( node , "Array" , SymbolFlags . Type , /*nameNotFoundMessage*/ undefined , /*nameArg*/ undefined ) ;
2123
- Debug . assert ( arrayTypeSymbol ) ;
2124
- arrayType = getDeclaredTypeOfSymbol ( arrayTypeSymbol ) ;
2125
- Debug . assert ( arrayType ) ;
2126
- }
2127
- links . resolvedType = createArrayType ( getTypeFromTypeNode ( node . elementType ) , arrayType ) ;
2121
+ links . resolvedType = createArrayType ( getTypeFromTypeNode ( node . elementType ) ) ;
2128
2122
}
2129
2123
return links . resolvedType ;
2130
2124
}
@@ -6791,7 +6785,8 @@ module ts {
6791
6785
getSymbolLinks ( unknownSymbol ) . type = unknownType ;
6792
6786
globals [ undefinedSymbol . name ] = undefinedSymbol ;
6793
6787
// Initialize special types
6794
- globalArrayType = getGlobalType ( "Array" , 1 ) ;
6788
+ globalArraySymbol = getGlobalSymbol ( "Array" ) ;
6789
+ globalArrayType = getTypeOfGlobalSymbol ( globalArraySymbol , 1 ) ;
6795
6790
globalObjectType = getGlobalType ( "Object" ) ;
6796
6791
globalFunctionType = getGlobalType ( "Function" ) ;
6797
6792
globalStringType = getGlobalType ( "String" ) ;
0 commit comments