-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Instantiate anonymous types less #7138
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
Changes from 5 commits
6c0b0ef
7a48fb2
2fecf2b
f6cadc9
5774f14
b138d4a
f7c4b3a
a5a4b59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,6 +134,10 @@ namespace ts { | |
const anySignature = createSignature(undefined, undefined, emptyArray, anyType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false); | ||
const unknownSignature = createSignature(undefined, undefined, emptyArray, unknownType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false); | ||
|
||
|
||
const identityMapper: TypeMapper = <TypeMapper>((t: TypeParameter) => <Type>t); | ||
identityMapper.mapsType = sym => false; | ||
|
||
const enumNumberIndexInfo = createIndexInfo(stringType, /*isReadonly*/ true); | ||
|
||
const globals: SymbolTable = {}; | ||
|
@@ -4954,54 +4958,66 @@ namespace ts { | |
} | ||
|
||
function createUnaryTypeMapper(source: Type, target: Type): TypeMapper { | ||
return t => t === source ? target : t; | ||
const mapper = <TypeMapper>(t => t === source ? target : t); | ||
mapper.mapsType = sym => sym.name === source.symbol.name; | ||
return mapper; | ||
} | ||
|
||
function createBinaryTypeMapper(source1: Type, target1: Type, source2: Type, target2: Type): TypeMapper { | ||
return t => t === source1 ? target1 : t === source2 ? target2 : t; | ||
const mapper = <TypeMapper>(t => t === source1 ? target1 : t === source2 ? target2 : t); | ||
mapper.mapsType = sym => sym.name === source1.symbol.name || sym.name === source2.symbol.name; | ||
return mapper; | ||
} | ||
|
||
function createTypeMapper(sources: Type[], targets: Type[]): TypeMapper { | ||
switch (sources.length) { | ||
case 1: return createUnaryTypeMapper(sources[0], targets[0]); | ||
case 2: return createBinaryTypeMapper(sources[0], targets[0], sources[1], targets[1]); | ||
} | ||
return t => { | ||
const mapper = <TypeMapper>(t => { | ||
for (let i = 0; i < sources.length; i++) { | ||
if (t === sources[i]) { | ||
return targets[i]; | ||
} | ||
} | ||
return t; | ||
}; | ||
}); | ||
mapper.mapsType = sym => forEach(sources, source => sym.name === source.symbol.name); | ||
return mapper; | ||
} | ||
|
||
function createUnaryTypeEraser(source: Type): TypeMapper { | ||
return t => t === source ? anyType : t; | ||
const eraser = <TypeMapper>(t => t === source ? <Type>anyType : <Type>t); | ||
eraser.mapsType = sym => sym.name === source.symbol.name; | ||
return eraser; | ||
} | ||
|
||
function createBinaryTypeEraser(source1: Type, source2: Type): TypeMapper { | ||
return t => t === source1 || t === source2 ? anyType : t; | ||
const eraser = <TypeMapper>(t => t === source1 || t === source2 ? <Type>anyType : <Type>t); | ||
eraser.mapsType = sym => sym.name === source1.symbol.name || sym.name === source2.symbol.name; | ||
return eraser; | ||
} | ||
|
||
function createTypeEraser(sources: Type[]): TypeMapper { | ||
switch (sources.length) { | ||
case 1: return createUnaryTypeEraser(sources[0]); | ||
case 2: return createBinaryTypeEraser(sources[0], sources[1]); | ||
} | ||
return t => { | ||
const mapper = <TypeMapper>(t => { | ||
for (const source of sources) { | ||
if (t === source) { | ||
return anyType; | ||
return <Type>anyType; | ||
} | ||
} | ||
return t; | ||
}; | ||
return <Type>t; | ||
}); | ||
mapper.mapsType = sym => forEach(sources, source => source.symbol.name === sym.name); | ||
return mapper; | ||
} | ||
|
||
function getInferenceMapper(context: InferenceContext): TypeMapper { | ||
if (!context.mapper) { | ||
const mapper: TypeMapper = t => { | ||
const mapper = <TypeMapper>(t => { | ||
const typeParameters = context.typeParameters; | ||
for (let i = 0; i < typeParameters.length; i++) { | ||
if (t === typeParameters[i]) { | ||
|
@@ -5010,19 +5026,18 @@ namespace ts { | |
} | ||
} | ||
return t; | ||
}; | ||
}); | ||
mapper.context = context; | ||
mapper.mapsType = sym => forEach(context.typeParameters, param => param.symbol.name === sym.name); | ||
context.mapper = mapper; | ||
} | ||
return context.mapper; | ||
} | ||
|
||
function identityMapper(type: Type): Type { | ||
return type; | ||
} | ||
|
||
function combineTypeMappers(mapper1: TypeMapper, mapper2: TypeMapper): TypeMapper { | ||
return t => instantiateType(mapper1(t), mapper2); | ||
const mapper = <TypeMapper>(t => instantiateType(mapper1(t), mapper2)); | ||
mapper.mapsType = sym => mapper1.mapsType(sym) || mapper2.mapsType(sym); | ||
return mapper; | ||
} | ||
|
||
function cloneTypeParameter(typeParameter: TypeParameter): TypeParameter { | ||
|
@@ -5123,7 +5138,9 @@ namespace ts { | |
return mapper(<TypeParameter>type); | ||
} | ||
if (type.flags & TypeFlags.Anonymous) { | ||
return type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) ? | ||
return type.symbol && | ||
type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) && | ||
hasTypeParametersInScope(type, mapper) ? | ||
instantiateAnonymousType(<AnonymousType>type, mapper) : type; | ||
} | ||
if (type.flags & TypeFlags.Reference) { | ||
|
@@ -5142,6 +5159,44 @@ namespace ts { | |
return type; | ||
} | ||
|
||
function hasTypeParametersInScope(type: Type, mapper: TypeMapper) { | ||
for (const original of type.symbol.declarations) { | ||
let node: Node = original; | ||
while (node !== undefined) { | ||
if (node.kind === SyntaxKind.FunctionType || | ||
node.kind === SyntaxKind.ConstructorType || | ||
node.kind === SyntaxKind.FunctionDeclaration || | ||
node.kind === SyntaxKind.MethodDeclaration || | ||
node.kind === SyntaxKind.MethodSignature || | ||
node.kind === SyntaxKind.Constructor || | ||
node.kind === SyntaxKind.CallSignature || | ||
node.kind === SyntaxKind.ConstructSignature || | ||
node.kind === SyntaxKind.IndexSignature || | ||
node.kind === SyntaxKind.GetAccessor || | ||
node.kind === SyntaxKind.SetAccessor || | ||
node.kind === SyntaxKind.FunctionExpression || | ||
node.kind === SyntaxKind.ArrowFunction || | ||
node.kind === SyntaxKind.JSDocFunctionType || | ||
node.kind === SyntaxKind.ClassDeclaration || | ||
node.kind === SyntaxKind.ClassExpression || | ||
node.kind === SyntaxKind.TypeAliasDeclaration || | ||
node.kind === SyntaxKind.InterfaceDeclaration) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turn this into a big switch over There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
const decl = <SignatureDeclaration | ClassLikeDeclaration | InterfaceDeclaration>node; | ||
if (decl.typeParameters && decl.typeParameters.length && forEach(decl.typeParameters, p => mapper.mapsType(getSymbolOfNode(p)))) { | ||
return true; | ||
} | ||
if ((node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.InterfaceDeclaration || node.kind === SyntaxKind.ClassExpression) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
mapper.mapsType(getSymbolOfNode(decl))) { | ||
// mapper maps the this class type of a class | ||
return true; | ||
} | ||
} | ||
node = node.parent; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function instantiateIndexInfo(info: IndexInfo, mapper: TypeMapper): IndexInfo { | ||
return info && createIndexInfo(instantiateType(info.type, mapper), info.isReadonly, info.declaration); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this like some sort of optimization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the information that mappers previously did not expose -- which types they map. This is important because
hasTypeParametersInScope
will now tell its caller whether a type needs to be instantiated by a particular mapper.This is only partly an optimization, because some of the types for which we now skip instantiation should never be instantiated, like the anonymous
typeof ListWrapper
types in the generic static functions in the test.