Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
e836fe1
Initial implementation of Union Types
ahejlsberg Oct 4, 2014
d70494f
Narrowing of variable types using typeof/instanceof type guards
ahejlsberg Oct 7, 2014
b8923b3
Support symbol kind for union properties
mhegazy Oct 8, 2014
5669f63
add test for quick info
mhegazy Oct 8, 2014
c439ae4
Add support for union properties in goto def
mhegazy Oct 8, 2014
95584e9
Addressing CR feedback
ahejlsberg Oct 8, 2014
fd5b808
Accepting new baselines
ahejlsberg Oct 8, 2014
3a17b02
Improved type argument inference with union types
ahejlsberg Oct 8, 2014
ea4cbbe
Merge branch 'master' into unionTypes
ahejlsberg Oct 8, 2014
5c661ba
Accepting new baselines after merge with master
ahejlsberg Oct 8, 2014
779db6e
Support find all refs on union properties
mhegazy Oct 9, 2014
2eb51ab
Use getRootSymbols for all union property needs
mhegazy Oct 9, 2014
dc43e83
Merge branch 'unionTypes' into unionTypesLS
mhegazy Oct 9, 2014
927f04f
Fix contextually typed object literal proeprties that are not propert…
mhegazy Oct 9, 2014
9f43ac0
respond to code review remarks
mhegazy Oct 10, 2014
bacb9d0
Test updates from union changes
danquirk Oct 10, 2014
8ce1760
Fixing merge conflicts
danquirk Oct 10, 2014
f5a9fee
ensure unionProperty symbols have declarations set at creation time
mhegazy Oct 10, 2014
483afea
Less aggressive subtype reduction in union types
ahejlsberg Oct 10, 2014
4e02b9f
Merge branch 'unionTypes' of https://github.com/Microsoft/TypeScript …
ahejlsberg Oct 10, 2014
c9a42c1
Accepting new baselines
ahejlsberg Oct 11, 2014
2ce627c
Handle union properties completions on apparant types
mhegazy Oct 11, 2014
4442b45
Add a temporary fix to quick info
mhegazy Oct 11, 2014
04e5309
Merge branch 'unionTypes' into unionTypesLS
mhegazy Oct 11, 2014
eee1602
Merge pull request #861 from Microsoft/unionTypesLS
mhegazy Oct 11, 2014
83d9aed
Correct contextual typing with union types
ahejlsberg Oct 13, 2014
a76a418
Accepting new baselines
ahejlsberg Oct 13, 2014
869ee41
Addressing CR feedback
ahejlsberg Oct 13, 2014
fc842b1
Merge branch 'master' into unionTypes
ahejlsberg Oct 13, 2014
4f4f59a
Merge changes from master in services.ts
mhegazy Oct 13, 2014
f5cd414
Merge branch 'master' into unionTypes
mhegazy Oct 13, 2014
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
Prev Previous commit
Next Next commit
Merge branch 'master' into unionTypes
Conflicts:
	src/compiler/checker.ts
	src/compiler/types.ts
	src/services/services.ts
	tests/baselines/reference/assignmentCompatBetweenTupleAndArray.errors.txt
	tests/baselines/reference/bestCommonTypeOfTuple.types
	tests/baselines/reference/bestCommonTypeOfTuple2.types
	tests/baselines/reference/castingTuple.errors.txt
	tests/baselines/reference/contextualTypeWithTuple.errors.txt
	tests/baselines/reference/genericCallWithTupleType.errors.txt
	tests/baselines/reference/indexerWithTuple.types
	tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt
  • Loading branch information
ahejlsberg committed Oct 13, 2014
commit fc842b177ef0e0e292a0bf302618f9d53e4f6535
9 changes: 7 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ module ts {
symbolToString: symbolToString,
writeSymbol: writeSymbol,
getAugmentedPropertiesOfApparentType: getAugmentedPropertiesOfApparentType,
getRootSymbol: getRootSymbol,
getRootSymbols: getRootSymbols,
getContextualType: getContextualType,
getFullyQualifiedName: getFullyQualifiedName,
Expand Down Expand Up @@ -1115,7 +1116,7 @@ module ts {
}
// Don't output function type literals in unions because '() => string | () => number' would be parsed
// as a function type that returns a union type. Instead output '{ (): string; } | { (): number; }'.
writeType(types[i], /*allowFunctionOrConstructorTypeLiteral*/ !union);
writeType(types[i], union ? flags & ~TypeFormatFlags.WriteArrowStyleSignature : flags | TypeFormatFlags.WriteArrowStyleSignature);
}
}

Expand Down Expand Up @@ -1145,7 +1146,7 @@ module ts {
writeTypeList(type.types, /*union*/ true);
}

function writeAnonymousType(type: ObjectType, allowFunctionOrConstructorTypeLiteral: boolean) {
function writeAnonymousType(type: ObjectType, flags: TypeFormatFlags) {
// Always use 'typeof T' for type of class, enum, and module objects
if (type.symbol && type.symbol.flags & (SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule)) {
writeTypeofSymbol(type);
Expand Down Expand Up @@ -8091,6 +8092,10 @@ module ts {
}
}

function getRootSymbol(symbol: Symbol): Symbol {
return symbol.flags & SymbolFlags.Transient && getSymbolLinks(symbol).target || symbol;
}

function getRootSymbols(symbol: Symbol): Symbol[] {
if (symbol.flags & SymbolFlags.UnionProperty) {
var symbols: Symbol[] = [];
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ module ts {
writeSymbol(symbol: Symbol, writer: SymbolWriter, enclosingDeclaration?: Node, meaning?: SymbolFlags, flags?: SymbolFormatFlags): void;
getFullyQualifiedName(symbol: Symbol): string;
getAugmentedPropertiesOfApparentType(type: Type): Symbol[];
getRootSymbol(symbol: Symbol): Symbol;
getRootSymbols(symbol: Symbol): Symbol[];
getContextualType(node: Node): Type;
getResolvedSignature(node: CallExpression, candidatesOutArray?: Signature[]): Signature;
Expand Down Expand Up @@ -769,6 +770,7 @@ module ts {
Merged = 0x02000000, // Merged symbol (created during program binding)
Transient = 0x04000000, // Transient symbol (created during type check)
Prototype = 0x08000000, // Prototype property (no source representation)
Undefined = 0x10000000, // Symbol for the undefined

Value = Variable | Property | EnumMember | Function | Class | Enum | ValueModule | Method | GetAccessor | SetAccessor | UnionProperty,
Type = Class | Interface | Enum | TypeLiteral | ObjectLiteral | TypeParameter,
Expand Down
102 changes: 14 additions & 88 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3039,93 +3039,7 @@ module ts {
return undefined;
}

var documentationParts = getSymbolDocumentationDisplayParts(symbol);

// TODO: handle union properties appropriately when merging with master
var symbolFlags = typeInfoResolver.getRootSymbols(symbol)[0].flags;

// Having all this logic here is pretty unclean. Consider moving to the roslyn model
// where all symbol display logic is encapsulated into visitors and options.
var totalParts: SymbolDisplayPart[] = [];

if (symbolFlags & SymbolFlags.Class) {
totalParts.push(keywordPart(SyntaxKind.ClassKeyword));
totalParts.push(spacePart());
totalParts.push.apply(totalParts, typeInfoResolver.symbolToDisplayParts(symbol, sourceFile));
}
else if (symbolFlags & SymbolFlags.Interface) {
totalParts.push(keywordPart(SyntaxKind.InterfaceKeyword));
totalParts.push(spacePart());
totalParts.push.apply(totalParts, typeInfoResolver.symbolToDisplayParts(symbol, sourceFile));
}
else if (symbolFlags & SymbolFlags.Enum) {
totalParts.push(keywordPart(SyntaxKind.EnumKeyword));
totalParts.push(spacePart());
totalParts.push.apply(totalParts, typeInfoResolver.symbolToDisplayParts(symbol, sourceFile));
}
else if (symbolFlags & SymbolFlags.Module) {
totalParts.push(keywordPart(SyntaxKind.ModuleKeyword));
totalParts.push(spacePart());
totalParts.push.apply(totalParts, typeInfoResolver.symbolToDisplayParts(symbol, sourceFile));
}
else if (symbolFlags & SymbolFlags.TypeParameter) {
totalParts.push(punctuationPart(SyntaxKind.OpenParenToken));
totalParts.push(new SymbolDisplayPart("type parameter", SymbolDisplayPartKind.text, undefined));
totalParts.push(punctuationPart(SyntaxKind.CloseParenToken));
totalParts.push(spacePart());
totalParts.push.apply(totalParts, typeInfoResolver.symbolToDisplayParts(symbol));
}
else {
totalParts.push(punctuationPart(SyntaxKind.OpenParenToken));
var text: string;

if (symbolFlags & SymbolFlags.Property) { text = "property" }
else if (symbolFlags & SymbolFlags.EnumMember) { text = "enum member" }
else if (symbolFlags & SymbolFlags.Function) { text = "function" }
else if (symbolFlags & SymbolFlags.Variable) { text = "variable" }
else if (symbolFlags & SymbolFlags.Method) { text = "method" }

if (!text) {
return undefined;
}

totalParts.push(new SymbolDisplayPart(text, SymbolDisplayPartKind.text, undefined));
totalParts.push(punctuationPart(SyntaxKind.CloseParenToken));
totalParts.push(spacePart());

totalParts.push.apply(totalParts, typeInfoResolver.symbolToDisplayParts(symbol, getContainerNode(node)));

var type = typeInfoResolver.getTypeOfSymbol(symbol);

if (symbolFlags & SymbolFlags.Property ||
symbolFlags & SymbolFlags.Variable) {

if (type) {
totalParts.push(punctuationPart(SyntaxKind.ColonToken));
totalParts.push(spacePart());
totalParts.push.apply(totalParts, typeInfoResolver.typeToDisplayParts(type, getContainerNode(node)));
}
}
else if (symbolFlags & SymbolFlags.Function ||
symbolFlags & SymbolFlags.Method) {
if (type) {
totalParts.push.apply(totalParts, typeInfoResolver.typeToDisplayParts(type, getContainerNode(node)));
}
}
else if (symbolFlags & SymbolFlags.EnumMember) {
var declaration = symbol.declarations[0];
if (declaration.kind === SyntaxKind.EnumMember) {
var constantValue = typeInfoResolver.getEnumMemberValue(<EnumMember>declaration);
if (constantValue !== undefined) {
totalParts.push(spacePart());
totalParts.push(operatorPart(SyntaxKind.EqualsToken));
totalParts.push(spacePart());
totalParts.push(new SymbolDisplayPart(constantValue.toString(), SymbolDisplayPartKind.numericLiteral, undefined));
}
}
}
}

var displayPartsDocumentationsAndKind = getSymbolDisplayPartsDocumentationAndSymbolKind(symbol, sourceFile, getContainerNode(node), typeInfoResolver, node);
return {
kind: displayPartsDocumentationsAndKind.symbolKind,
kindModifiers: getSymbolModifiers(symbol),
Expand Down Expand Up @@ -3257,7 +3171,19 @@ module ts {

var result: DefinitionInfo[] = [];

getDefinitionFromSymbol(symbol, node, result);
var declarations = symbol.getDeclarations();
var symbolName = typeInfoResolver.symbolToString(symbol); // Do not get scoped name, just the name of the symbol
var symbolKind = getSymbolKind(symbol);
var containerSymbol = symbol.parent;
var containerName = containerSymbol ? typeInfoResolver.symbolToString(containerSymbol, node) : "";

if (!tryAddConstructSignature(symbol, node, symbolKind, symbolName, containerName, result) &&
!tryAddCallSignature(symbol, node, symbolKind, symbolName, containerName, result)) {
// Just add all the declarations.
forEach(declarations, declaration => {
result.push(getDefinitionInfo(declaration, symbolKind, symbolName, containerName));
});
}

return result;
}
Expand Down
46 changes: 23 additions & 23 deletions tests/baselines/reference/decrementOperatorWithEnumType.errors.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumType.ts(7,23): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer.
tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumType.ts(12,1): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type.
tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumType.ts(12,7): error TS2304: Cannot find name 'A'.


==== tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumType.ts (3 errors) ====
// -- operator on enum type

enum ENUM1 { A, B, "" };

// expression
var ResultIsNumber1 = --ENUM1["A"];
var ResultIsNumber2 = ENUM1.A--;
~~~~~~~
!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer.

// miss assignment operator
--ENUM1["A"];

ENUM1[A]--;
~~~~~~~~
!!! error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type.
~
tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumType.ts(7,23): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer.
tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumType.ts(12,1): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type.
tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumType.ts(12,7): error TS2304: Cannot find name 'A'.
==== tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumType.ts (3 errors) ====
// -- operator on enum type
enum ENUM1 { A, B, "" };
// expression
var ResultIsNumber1 = --ENUM1["A"];
var ResultIsNumber2 = ENUM1.A--;
~~~~~~~
!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer.
// miss assignment operator
--ENUM1["A"];
ENUM1[A]--;
~~~~~~~~
!!! error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type.
~
!!! error TS2304: Cannot find name 'A'.
48 changes: 24 additions & 24 deletions tests/baselines/reference/enumIdentifierLiterals.errors.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
tests/cases/compiler/enumIdentifierLiterals.ts(2,5): error TS1151: An enum member cannot have a numeric name.
tests/cases/compiler/enumIdentifierLiterals.ts(3,5): error TS1151: An enum member cannot have a numeric name.
tests/cases/compiler/enumIdentifierLiterals.ts(4,5): error TS1151: An enum member cannot have a numeric name.
tests/cases/compiler/enumIdentifierLiterals.ts(5,5): error TS1151: An enum member cannot have a numeric name.
tests/cases/compiler/enumIdentifierLiterals.ts(6,5): error TS1151: An enum member cannot have a numeric name.


==== tests/cases/compiler/enumIdentifierLiterals.ts (5 errors) ====
enum Nums {
1.0,
~~~
!!! error TS1151: An enum member cannot have a numeric name.
11e-1,
~~~~~
!!! error TS1151: An enum member cannot have a numeric name.
0.12e1,
~~~~~~
!!! error TS1151: An enum member cannot have a numeric name.
"13e-1",
~~~~~~~
!!! error TS1151: An enum member cannot have a numeric name.
0xF00D
~~~~~~
!!! error TS1151: An enum member cannot have a numeric name.
tests/cases/compiler/enumIdentifierLiterals.ts(2,5): error TS1151: An enum member cannot have a numeric name.
tests/cases/compiler/enumIdentifierLiterals.ts(3,5): error TS1151: An enum member cannot have a numeric name.
tests/cases/compiler/enumIdentifierLiterals.ts(4,5): error TS1151: An enum member cannot have a numeric name.
tests/cases/compiler/enumIdentifierLiterals.ts(5,5): error TS1151: An enum member cannot have a numeric name.
tests/cases/compiler/enumIdentifierLiterals.ts(6,5): error TS1151: An enum member cannot have a numeric name.
==== tests/cases/compiler/enumIdentifierLiterals.ts (5 errors) ====
enum Nums {
1.0,
~~~
!!! error TS1151: An enum member cannot have a numeric name.
11e-1,
~~~~~
!!! error TS1151: An enum member cannot have a numeric name.
0.12e1,
~~~~~~
!!! error TS1151: An enum member cannot have a numeric name.
"13e-1",
~~~~~~~
!!! error TS1151: An enum member cannot have a numeric name.
0xF00D
~~~~~~
!!! error TS1151: An enum member cannot have a numeric name.
}
24 changes: 12 additions & 12 deletions tests/baselines/reference/enumIdentifierLiterals.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//// [enumIdentifierLiterals.ts]
//// [enumIdentifierLiterals.ts]
enum Nums {
1.0,
11e-1,
0.12e1,
"13e-1",
0xF00D
}

//// [enumIdentifierLiterals.js]
var Nums;
(function (Nums) {
Nums[Nums["1"] = 0] = "1";
Nums[Nums["1.1"] = 1] = "1.1";
Nums[Nums["1.2"] = 2] = "1.2";
Nums[Nums["13e-1"] = 3] = "13e-1";
Nums[Nums["61453"] = 4] = "61453";
})(Nums || (Nums = {}));
}
//// [enumIdentifierLiterals.js]
var Nums;
(function (Nums) {
Nums[Nums["1"] = 0] = "1";
Nums[Nums["1.1"] = 1] = "1.1";
Nums[Nums["1.2"] = 2] = "1.2";
Nums[Nums["13e-1"] = 3] = "13e-1";
Nums[Nums["61453"] = 4] = "61453";
})(Nums || (Nums = {}));
40 changes: 20 additions & 20 deletions tests/baselines/reference/incrementOperatorWithEnumType.errors.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithEnumType.ts(7,23): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer.
tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithEnumType.ts(12,1): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer.


==== tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithEnumType.ts (2 errors) ====
// ++ operator on enum type

enum ENUM1 { A, B, "" };

// expression
var ResultIsNumber1 = ++ENUM1["B"];
var ResultIsNumber2 = ENUM1.B++;
~~~~~~~
!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer.

// miss assignment operator
++ENUM1["B"];

ENUM1.B++;
~~~~~~~
tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithEnumType.ts(7,23): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer.
tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithEnumType.ts(12,1): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer.
==== tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithEnumType.ts (2 errors) ====
// ++ operator on enum type
enum ENUM1 { A, B, "" };
// expression
var ResultIsNumber1 = ++ENUM1["B"];
var ResultIsNumber2 = ENUM1.B++;
~~~~~~~
!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer.
// miss assignment operator
++ENUM1["B"];
ENUM1.B++;
~~~~~~~
!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(55,5): error TS2412: Property '"4.0"' of type 'number' is not assignable to numeric index type 'string'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(68,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(73,5): error TS2412: Property '"4.0"' of type 'number' is not assignable to numeric index type 'string'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(78,5): error TS2322: Type '{ [x: number]: string | number; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: unknown; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }':
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(78,5): error TS2322: Type '{ [x: number]: string | number; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: any; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }':
Index signatures are incompatible:
Type 'string | number' is not assignable to type 'string':
Type 'number' is not assignable to type 'string'.
Expand Down Expand Up @@ -117,7 +117,7 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo
// error
var b: { [x: number]: string; } = {
~
!!! error TS2322: Type '{ [x: number]: string | number; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: unknown; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }':
!!! error TS2322: Type '{ [x: number]: string | number; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: any; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }':
!!! error TS2322: Index signatures are incompatible:
!!! error TS2322: Type 'string | number' is not assignable to type 'string':
!!! error TS2322: Type 'number' is not assignable to type 'string'.
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.