Skip to content

Commit 11492cb

Browse files
committed
Simplifying createArrayType recursion check.
I made these changes during some unrelated experiments but I'd like to keep them.
1 parent 39046fa commit 11492cb

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ module ts {
4646

4747
var globals: SymbolTable = {};
4848

49+
var globalArraySymbol: Symbol;
50+
4951
var globalObjectType: ObjectType;
5052
var globalFunctionType: ObjectType;
5153
var globalArrayType: ObjectType;
@@ -616,8 +618,6 @@ module ts {
616618
members, callSignatures, constructSignatures, stringIndexType, numberIndexType);
617619
}
618620

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)
621621
function isOptionalProperty(propertySymbol: Symbol): boolean {
622622
if (propertySymbol.flags & SymbolFlags.Prototype) {
623623
return false;
@@ -2069,7 +2069,7 @@ module ts {
20692069
return links.resolvedType;
20702070
}
20712071

2072-
function getGlobalType(name: string, arity: number = 0): ObjectType {
2072+
function getTypeOfGlobalSymbol(symbol: Symbol, arity: number): ObjectType {
20732073

20742074
function getTypeDeclaration(symbol: Symbol): Declaration {
20752075
var declarations = symbol.declarations;
@@ -2079,14 +2079,11 @@ module ts {
20792079
case SyntaxKind.ClassDeclaration:
20802080
case SyntaxKind.InterfaceDeclaration:
20812081
case SyntaxKind.EnumDeclaration:
2082-
case SyntaxKind.TypeLiteral:
2083-
case SyntaxKind.FunctionDeclaration:
20842082
return declaration;
20852083
}
20862084
}
20872085
}
20882086

2089-
var symbol = resolveName(undefined, name, SymbolFlags.Type, Diagnostics.Cannot_find_global_type_0, name);
20902087
if (!symbol) {
20912088
return emptyObjectType;
20922089
}
@@ -2102,29 +2099,26 @@ module ts {
21022099
return <ObjectType>type;
21032100
}
21042101

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;
21092116
}
21102117

21112118
function getTypeFromArrayTypeNode(node: ArrayTypeNode): Type {
21122119
var links = getNodeLinks(node);
21132120
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));
21282122
}
21292123
return links.resolvedType;
21302124
}
@@ -6791,7 +6785,8 @@ module ts {
67916785
getSymbolLinks(unknownSymbol).type = unknownType;
67926786
globals[undefinedSymbol.name] = undefinedSymbol;
67936787
// Initialize special types
6794-
globalArrayType = getGlobalType("Array", 1);
6788+
globalArraySymbol = getGlobalSymbol("Array");
6789+
globalArrayType = getTypeOfGlobalSymbol(globalArraySymbol, 1);
67956790
globalObjectType = getGlobalType("Object");
67966791
globalFunctionType = getGlobalType("Function");
67976792
globalStringType = getGlobalType("String");

0 commit comments

Comments
 (0)