Skip to content

Simplifying createArrayType recursion check. #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ module ts {

var globals: SymbolTable = {};

var globalArraySymbol: Symbol;

var globalObjectType: ObjectType;
var globalFunctionType: ObjectType;
var globalArrayType: ObjectType;
Expand Down Expand Up @@ -616,8 +618,6 @@ module ts {
members, callSignatures, constructSignatures, stringIndexType, numberIndexType);
}

// Takes a VariableDeclaration because it could be an exported var from a module (VariableDeclaration),
// a class or object type property (PropertyDeclaration), or a parameter property (ParameterDeclaration)
function isOptionalProperty(propertySymbol: Symbol): boolean {
if (propertySymbol.flags & SymbolFlags.Prototype) {
return false;
Expand Down Expand Up @@ -2069,7 +2069,7 @@ module ts {
return links.resolvedType;
}

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

function getTypeDeclaration(symbol: Symbol): Declaration {
var declarations = symbol.declarations;
Expand All @@ -2079,14 +2079,11 @@ module ts {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.TypeLiteral:
case SyntaxKind.FunctionDeclaration:
return declaration;
}
}
}

var symbol = resolveName(undefined, name, SymbolFlags.Type, Diagnostics.Cannot_find_global_type_0, name);
if (!symbol) {
return emptyObjectType;
}
Expand All @@ -2102,29 +2099,26 @@ module ts {
return <ObjectType>type;
}

// arrayType argument is used as a backup in case if globalArrayType is not defined
function createArrayType(elementType: Type, arrayType?: ObjectType): Type {
var rootType = globalArrayType || arrayType;
return rootType !== emptyObjectType ? createTypeReference(<GenericType>rootType, [elementType]) : emptyObjectType;
function getGlobalSymbol(name: string): Symbol {
return resolveName(undefined, name, SymbolFlags.Type, Diagnostics.Cannot_find_global_type_0, name);
}

function getGlobalType(name: string): ObjectType {
return getTypeOfGlobalSymbol(getGlobalSymbol(name), 0);
}

function createArrayType(elementType: Type): Type {
// globalArrayType will be undefined if we get here during creation of the Array type. This for example happens if
// user code augments the Array type with call or construct signatures that have an array type as the return type.
// We instead use globalArraySymbol to obtain the (not yet fully constructed) Array type.
var arrayType = globalArrayType || getDeclaredTypeOfSymbol(globalArraySymbol);
return arrayType !== emptyObjectType ? createTypeReference(<GenericType>arrayType, [elementType]) : emptyObjectType;
}

function getTypeFromArrayTypeNode(node: ArrayTypeNode): Type {
var links = getNodeLinks(node);
if (!links.resolvedType) {
var arrayType = globalArrayType;
if (!arrayType) {
// if user code contains augmentation for Array type that includes call\construct signatures with arrays as parameter\return types,
// then we might step here then during initialization of the global Array type when globalArrayType is not yet set.
// CODE: interface Array<T> { (): number[] }
// in this case just resolve name 'Array' again and get declared type of symbol.
// this type is the one that eventually should be set as 'globalArrayType'.
// NOTE: this is specific to signatures since got signatures we realize parameter\return types.
var arrayTypeSymbol = resolveName(node, "Array", SymbolFlags.Type, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
Debug.assert(arrayTypeSymbol);
arrayType = getDeclaredTypeOfSymbol(arrayTypeSymbol);
Debug.assert(arrayType);
}
links.resolvedType = createArrayType(getTypeFromTypeNode(node.elementType), arrayType);
links.resolvedType = createArrayType(getTypeFromTypeNode(node.elementType));
}
return links.resolvedType;
}
Expand Down Expand Up @@ -6791,7 +6785,8 @@ module ts {
getSymbolLinks(unknownSymbol).type = unknownType;
globals[undefinedSymbol.name] = undefinedSymbol;
// Initialize special types
globalArrayType = getGlobalType("Array", 1);
globalArraySymbol = getGlobalSymbol("Array");
globalArrayType = getTypeOfGlobalSymbol(globalArraySymbol, 1);
globalObjectType = getGlobalType("Object");
globalFunctionType = getGlobalType("Function");
globalStringType = getGlobalType("String");
Expand Down