8000 Addressing CR feedback · johnangularjs/TypeScript@b82ae85 · GitHub
[go: up one dir, main page]

Skip to content

Commit b82ae85

Browse files
committed
Addressing CR feedback
1 parent bcdbc98 commit b82ae85

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

src/compiler/checker.ts

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,7 +1789,7 @@ module ts {
17891789
function buildTypeParameterDisplayFromSymbol(symbol: Symbol, writer: SymbolWriter, enclosingDeclaraiton?: Node, flags?: TypeFormatFlags) {
17901790
let targetSymbol = getTargetSymbol(symbol);
17911791
if (targetSymbol.flags & SymbolFlags.Class || targetSymbol.flags & SymbolFlags.Interface) {
1792-
buildDisplayForTypeParametersAndDelimiters(getLocalTypeParametersOfClassOrInterface(symbol), writer, enclosingDeclaraiton, flags);
1792+
buildDisplayForTypeParametersAndDelimiters(getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol), writer, enclosingDeclaraiton, flags);
17931793
}
17941794
}
17951795

@@ -2573,8 +2573,9 @@ module ts {
25732573
return appendOuterTypeParameters(undefined, getDeclarationOfKind(symbol, kind));
25742574
}
25752575

2576-
// The local type parameters are the combined set of type parameters from all declarations of the class or interface.
2577-
function getLocalTypeParametersOfClassOrInterface(symbol: Symbol): TypeParameter[] {
2576+
// The local type parameters are the combined set of typ 10000 e parameters from all declarations of the class,
2577+
// interface, or type alias.
2578+
function getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol: Symbol): TypeParameter[] {
25782579
let result: TypeParameter[];
25792580
for (let node of symbol.declarations) {
25802581
if (node.kind === SyntaxKind.InterfaceDeclaration || node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.TypeAliasDeclaration) {
@@ -2590,16 +2591,7 @@ module ts {
25902591
// The full set of type parameters for a generic class or interface type consists of its outer type parameters plus
25912592
// its locally declared type parameters.
25922593
function getTypeParametersOfClassOrInterface(symbol: Symbol): TypeParameter[] {
2593-
return concatenate(getOuterTypeParametersOfClassOrInterface(symbol), getLocalTypeParametersOfClassOrInterface(symbol));
2594-
}
2595-
2596-
function getTypeParametersOfTypeAlias(symbol: Symbol): TypeParameter[] {
2597-
let links = getSymbolLinks(symbol);
2598-
if (!links.typeParameters) {
2599-
let declaration = <TypeAliasDeclaration>getDeclarationOfKind(symbol, SyntaxKind.TypeAliasDeclaration);
2600-
links.typeParameters = declaration.typeParameters ? appendTypeParameters(undefined, declaration.typeParameters) : emptyArray;
2601-
}
2602-
return links.typeParameters;
2594+
return concatenate(getOuterTypeParametersOfClassOrInterface(symbol), getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol));
26032595
}
26042596

26052597
function getBaseTypes(type: InterfaceType): ObjectType[] {
@@ -2672,7 +2664,7 @@ module ts {
26722664
let kind = symbol.flags & SymbolFlags.Class ? TypeFlags.Class : TypeFlags.Interface;
26732665
let type = links.declaredType = <InterfaceType>createObjectType(kind, symbol);
26742666
let outerTypeParameters = getOuterTypeParametersOfClassOrInterface(symbol);
2675-
let localTypeParameters = getLocalTypeParametersOfClassOrInterface(symbol);
2667+
let localTypeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol);
26762668
if (outerTypeParameters || localTypeParameters) {
26772669
type.flags |= TypeFlags.Reference;
26782670
type.typeParameters = concatenate(outerTypeParameters, localTypeParameters);
@@ -2697,7 +2689,16 @@ module ts {
26972689
}
26982690
let declaration = <TypeAliasDeclaration>getDeclarationOfKind(symbol, SyntaxKind.TypeAliasDeclaration);
26992691
let type = getTypeFromTypeNode(declaration.type);
2700-
if (!popTypeResolution()) {
2692+
if (popTypeResolution()) {
2693+
links.typeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol);
2694+
if (links.typeParameters) {
2695+
// Initialize the instantiation cache for generic type aliases. The declared type corresponds to
2696+
// an instantiation of the type alias with the type parameters supplied as type arguments.
2697+
links.instantiations = {};
2698+
links.instantiations[getTypeListId(links.typeParameters)] = type;
2699+
}
2700+
}
2701+
else {
27012702
type = unknownType;
27022703
error(declaration.name, Diagnostics.Type_alias_0_circularly_references_itself, symbolToString(symbol));
27032704
}
@@ -3522,6 +3523,7 @@ module ts {
35223523
}
35233524
}
35243525

3526+
// Get type from reference to class or interface
35253527
function getTypeFromClassOrInterfaceReference(node: TypeReferenceNode | ExpressionWithTypeArguments, symbol: Symbol): Type {
35263528
let type = getDeclaredTypeOfSymbol(symbol);
35273529
let typeParameters = (<InterfaceType>type).localTypeParameters;
@@ -3543,22 +3545,20 @@ module ts {
35433545
return type;
35443546
}
35453547

3548+
// Get type from reference to type alias. When a type alias is generic, the declared type of the type alias may include
3549+
// references to the type parameters of the alias. We replace those with the actual type arguments by instantiating the
3550+
// declared type. Instantiations are cached using the type identities of the type arguments as the key.
35463551
function getTypeFromTypeAliasReference(node: TypeReferenceNode | ExpressionWithTypeArguments, symbol: Symbol): Type {
35473552
let type = getDeclaredTypeOfSymbol(symbol);
3548-
let typeParameters = getTypeParametersOfTypeAlias(symbol);
3549-
if (typeParameters.length) {
3553+
let links = getSymbolLinks(symbol);
3554+
let typeParameters = links.typeParameters;
3555+
if (typeParameters) {
35503556
if (!node.typeArguments || node.typeArguments.length !== typeParameters.length) {
35513557
error(node, Diagnostics.Generic_type_0_requires_1_type_argument_s, symbolToString(symbol), typeParameters.length);
35523558
return unknownType;
35533559
}
35543560
let typeArguments = map(node.typeArguments, getTypeFromTypeNode);
35553561
let id = getTypeListId(typeArguments);
3556-
let links = getSymbolLinks(symbol);
3557-
if (!links.instantiations) {
3558-
links.instantiations = {};
3559-
// The declared type corresponds to an instantiation with its own type parameters as type arguments
3560-
links.instantiations[getTypeListId(typeParameters)] = type;
3561-
}
35623562
return links.instantiations[id] || (links.instantiations[id] = instantiateType(type, createTypeMapper(typeParameters, typeArguments)));
35633563
}
35643564
if (node.typeArguments) {
@@ -3568,12 +3568,20 @@ module ts {
35683568
return type;
35693569
}
35703570

3571-
function getTypeFromTypeParameterReference(node: TypeReferenceNode | ExpressionWithTypeArguments, symbol: Symbol): Type {
3572-
// TypeScript 1.0 spec (April 2014): 3.4.1
3573-
// Type parameters declared in a particular type parameter list
3574-
// may not be referenced in constraints in that type parameter list
3575-
// Implementation: such type references are resolved to 'unknown' type that usually denotes error
3576-
return isTypeParameterReferenceIllegalInConstraint(node, symbol) ? unknownType : getDeclaredTypeOfSymbol(symbol);
3571+
// Get type from reference to named type that cannot be generic (enum or type parameter)
3572+
function getTypeFromNonGenericTypeReference(node: TypeReferenceNode | ExpressionWithTypeArguments, symbol: Symbol): Type {
3573+
if (symbol.flags & SymbolFlags.TypeParameter && isTypeParameterReferenceIllegalInConstraint(node, symbol)) {
3574+
// TypeScript 1.0 spec (April 2014): 3.4.1
3575+
// Type parameters declared in a particular type parameter list
3576+
// may not be referenced in constraints in that type parameter list
3577+
// Implementation: such type references are resolved to 'unknown' type that usually denotes error
3578+
return unknownType;
3579+
}
3580+
if (node.typeArguments) {
3581+
error(node, Diagnostics.Type_0_is_not_generic, symbolToString(symbol));
3582+
return unknownType;
3583+
}
3584+
return getDeclaredTypeOfSymbol(symbol);
35773585
}
35783586

35793587
function getTypeFromTypeReference(node: TypeReferenceNode | ExpressionWithTypeArguments): Type {
@@ -3583,10 +3591,10 @@ module ts {
35833591
isSupportedExpressionWithTypeArguments(<ExpressionWithTypeArguments>node) ? (<ExpressionWithTypeArguments>node).expression :
35843592
undefined;
35853593
let symbol = typeNameOrExpression && resolveEntityName(typeNameOrExpression, SymbolFlags.Type) || unknownSymbol;
3586-
let type = symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface) ? getTypeFromClassOrInterfaceReference(node, symbol) :
3587-
symbol.flags & SymbolFlags.TypeParameter ? getTypeFromTypeParameterReference(node, symbol) :
3594+
let type = symbol === unknownSymbol ? unknownType :
3595+
symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface) ? getTypeFromClassOrInterfaceReference(node, symbol) :
35883596
symbol.flags & SymbolFlags.TypeAlias ? getTypeFromTypeAliasReference(node, symbol) :
3589-
getDeclaredTypeOfSymbol(symbol);
3597+
getTypeFromNonGenericTypeReference(node, symbol);
35903598
// Cache both the resolved symbol and the resolved type. The resolved symbol is needed in when we check the
35913599
// type reference in checkTypeReferenceOrExpressionWithTypeArguments.
35923600
links.resolvedSymbol = symbol;
@@ -8786,7 +8794,7 @@ module ts {
87868794
if (type !== unknownType && node.typeArguments) {
87878795
// Do type argument local checks only if referenced type is successfully resolved
87888796
let symbol = getNodeLinks(node).resolvedSymbol;
8789-
let typeParameters = symbol.flags & SymbolFlags.TypeAlias ? getTypeParametersOfTypeAlias(symbol) : (<TypeReference>type).target.localTypeParameters;
8797+
let typeParameters = symbol.flags & SymbolFlags.TypeAlias ? getSymbolLinks(symbol).typeParameters : (<TypeReference>type).target.localTypeParameters;
87908798
let len = node.typeArguments.length;
87918799
for (let i = 0; i < len; i++) {
87928800
checkSourceElement(node.typeArguments[i]);

src/compiler/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,8 +1527,8 @@ module ts {
15271527
target?: Symbol; // Resolved (non-alias) target of an alias
15281528
type?: Type; // Type of value symbol
15291529
declaredType?: Type; // Type of class, interface, enum, type alias, or type parameter
1530-
typeParameters?: TypeParameter[]; // Type parameters of type alias (empty array if none)
1531-
instantiations?: Map<Type>; // Instantiations of generic type alias
1530+
typeParameters?: TypeParameter[]; // Type parameters of type alias (undefined if non-generic)
1531+
instantiations?: Map<Type>; // Instantiations of generic type alias (undefined if non-generic)
15321532
mapper?: TypeMapper; // Type mapper for instantiation alias
15331533
referenced?: boolean; // True if alias symbol has been referenced as a value
15341534
unionType?: UnionType; // Containing union type for union property

0 commit comments

Comments
 (0)
0