Skip to content

Commit

Permalink
Support symbol kind for union properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mhegazy committed Oct 8, 2014
1 parent d70494f commit b8923b3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ module ts {
getContextualType: getContextualType,
getFullyQualifiedName: getFullyQualifiedName,
getResolvedSignature: getResolvedSignature,
getEnumMemberValue: getEnumMemberValue
getEnumMemberValue: getEnumMemberValue,
getUnionTypesOfUnionProperty: getUnionTypesOfUnionProperty
};

var undefinedSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "undefined");
Expand Down Expand Up @@ -1750,6 +1751,10 @@ module ts {
return links.type;
}

function getUnionTypesOfUnionProperty(symbol: Symbol): Type[] {
return (symbol.flags & SymbolFlags.UnionProperty) ? getSymbolLinks(symbol).unionType.types : undefined;
}

function getTypeOfSymbol(symbol: Symbol): Type {
if (symbol.flags & (SymbolFlags.Variable | SymbolFlags.Property)) {
return getTypeOfVariableOrParameterOrProperty(symbol);
Expand Down Expand Up @@ -3583,7 +3588,8 @@ module ts {
}

function getBestCommonType(types: Type[], contextualType?: Type): Type {
return contextualType && isSupertypeOfEach(contextualType, types) ? contextualType : getUnionType(types); }
return contextualType && isSupertypeOfEach(contextualType, types) ? contextualType : getUnionType(types);
}

function isTypeOfObjectLiteral(type: Type): boolean {
return (type.flags & TypeFlags.Anonymous) && type.symbol && (type.symbol.flags & SymbolFlags.ObjectLiteral) ? true : false;
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,8 @@ module ts {
getContextualType(node: Node): Type;
getResolvedSignature(node: CallExpression, candidatesOutArray?: Signature[]): Signature;

getUnionTypesOfUnionProperty(symbol: Symbol): Type[];

// Returns the constant value of this enum member, or 'undefined' if the enum member has a
// computed value.
getEnumMemberValue(node: EnumMember): number;
Expand Down
11 changes: 10 additions & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2284,8 +2284,16 @@ module ts {
}
}

function getConcreteSymbol(symbol: Symbol): Symbol {

This comment has been minimized.

Copy link
@DanielRosenwasser

DanielRosenwasser Oct 10, 2014

Member

Please document what this does. This is a vague name when offhandedly reading it.

if (symbol.flags & SymbolFlags.UnionProperty) {
var types = typeInfoResolver.getUnionTypesOfUnionProperty(symbol);
symbol = typeInfoResolver.getPropertyOfType(types[0], symbol.name);

This comment has been minimized.

Copy link
@DanielRosenwasser

DanielRosenwasser Oct 10, 2014

Member

Is the idea just to take the first type in the list? Is this temporary?

}
return typeInfoResolver.getRootSymbol(symbol);
}

function getSymbolKind(symbol: Symbol): string {
var flags = typeInfoResolver.getRootSymbol(symbol).getFlags();
var flags = getConcreteSymbol(symbol).getFlags();

if (flags & SymbolFlags.Module) return ScriptElementKind.moduleElement;
if (flags & SymbolFlags.Class) return ScriptElementKind.classElement;
Expand Down Expand Up @@ -2344,6 +2352,7 @@ module ts {
}

function getSymbolModifiers(symbol: Symbol): string {
symbol = getConcreteSymbol(symbol);
return symbol && symbol.declarations && symbol.declarations.length > 0
? getNodeModifiers(symbol.declarations[0])
: ScriptElementKindModifier.none;
Expand Down
20 changes: 20 additions & 0 deletions tests/cases/fourslash/completionEntryForUnionProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
///<reference path="fourslash.ts" />

////interface One {
//// commonProperty: number;
//// commonFunction(): number;
////}
////
////interface Two {
//// commonProperty: string
//// commonFunction(): number;
////}
////
////var x : One | Two;
////
////x./**/

goTo.marker();
verify.memberListContains("commonProperty", "string | number", undefined, undefined, "property");
verify.memberListContains("commonFunction", "() => number", undefined, undefined, "method");
verify.memberListCount(2);

0 comments on commit b8923b3

Please sign in to comment.