Skip to content

Commit 8110b8c

Browse files
committed
Union types for all the things
Migration WIP More betterness Unions, unions everywhere Full type action Finish updating services layer Just change the type
1 parent c1f2afd commit 8110b8c

21 files changed

+1168
-456
lines changed

src/compiler/binder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,7 +2138,7 @@ namespace ts {
21382138
return updateStrictModeStatementList((<Block | ModuleBlock>node).statements);
21392139

21402140
case SyntaxKind.JSDocParameterTag:
2141-
if (node.parent.kind !== SyntaxKind.JSDocTypeLiteral) {
2141+
if ((node as Node).parent.kind !== SyntaxKind.JSDocTypeLiteral) {
21422142
break;
21432143
}
21442144
// falls through
@@ -2553,11 +2553,11 @@ namespace ts {
25532553
// report error on all statements except empty ones
25542554
(isStatementButNotDeclaration(node) && node.kind !== SyntaxKind.EmptyStatement) ||
25552555
// report error on class declarations
2556-
node.kind === SyntaxKind.ClassDeclaration ||
2556+
(node as Declaration).kind === SyntaxKind.ClassDeclaration ||
25572557
// report error on instantiated modules or const-enums only modules if preserveConstEnums is set
2558-
(node.kind === SyntaxKind.ModuleDeclaration && shouldReportErrorOnModuleDeclaration(<ModuleDeclaration>node)) ||
2558+
((node as Declaration).kind === SyntaxKind.ModuleDeclaration && shouldReportErrorOnModuleDeclaration(<ModuleDeclaration>(node as Declaration))) ||
25592559
// report error on regular enums and const enums if preserveConstEnums is set
2560-
(node.kind === SyntaxKind.EnumDeclaration && (!isConstEnumDeclaration(node) || options.preserveConstEnums));
2560+
((node as Declaration).kind === SyntaxKind.EnumDeclaration && (!isConstEnumDeclaration(node) || options.preserveConstEnums));
25612561

25622562
if (reportError) {
25632563
currentFlow = reportedUnreachableFlow;

src/compiler/checker.ts

Lines changed: 94 additions & 83 deletions
Large diffs are not rendered by default.

src/compiler/core.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ namespace ts {
247247
}
248248

249249
/** Works like Array.prototype.find, returning `undefined` if no element satisfying the predicate is found. */
250+
export function find<T, U extends T>(array: ReadonlyArray<T>, predicate: (element: T, index: number) => element is U): U | undefined;
251+
export function find<T>(array: ReadonlyArray<T>, predicate: (element: T, index: number) => boolean): T | undefined;
250252
export function find<T>(array: ReadonlyArray<T>, predicate: (element: T, index: number) => boolean): T | undefined {
251253
for (let i = 0; i < array.length; i++) {
252254
const value = array[i];
@@ -2338,7 +2340,7 @@ namespace ts {
23382340

23392341
export interface ObjectAllocator {
23402342
getNodeConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => Node;
2341-
getTokenConstructor(): new <TKind extends SyntaxKind>(kind: TKind, pos?: number, end?: number) => Token<TKind>;
2343+
getTokenConstructor(): new <TKind extends SyntaxKind>(kind: TKind, pos?: number, end?: number) => Node; // Token<TKind> is incompatible with the union-based Node, as its union is not lifted out of the kind field
23422344
getIdentifierConstructor(): new (kind: SyntaxKind.Identifier, pos?: number, end?: number) => Identifier;
23432345
getSourceFileConstructor(): new (kind: SyntaxKind.SourceFile, pos?: number, end?: number) => SourceFile;
23442346
getSymbolConstructor(): new (flags: SymbolFlags, name: __String) => Symbol;

src/compiler/declarationEmitter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace ts {
2727
interface SymbolAccessibilityDiagnostic {
2828
errorNode: Node;
2929
diagnosticMessage: DiagnosticMessage;
30-
typeName?: DeclarationName;
30+
typeName?: DeclarationName | QualifiedName;
3131
}
3232

3333
export function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver, targetSourceFile: SourceFile): Diagnostic[] {
@@ -64,7 +64,7 @@ namespace ts {
6464
let currentIdentifiers: Map<string>;
6565
let isCurrentFileExternalModule: boolean;
6666
let reportedDeclarationError = false;
67-
let errorNameNode: DeclarationName;
67+
let errorNameNode: DeclarationName | QualifiedName;
6868
const emitJsDocComments = compilerOptions.removeComments ? noop : writeJsDocComments;
6969
const emit = compilerOptions.stripInternal ? stripInternal : emitNode;
7070
let needsDeclare = true;
@@ -1351,7 +1351,7 @@ namespace ts {
13511351
}
13521352
}
13531353

1354-
function emitTypeOfVariableDeclarationFromTypeLiteral(node: VariableLikeDeclaration) {
1354+
function emitTypeOfVariableDeclarationFromTypeLiteral(node: VariableLikeDeclarationBase) {
13551355
// if this is property of type literal,
13561356
// or is parameter of method/call/construct/index signature of type literal
13571357
// emit only if type is specified

src/compiler/factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ namespace ts {
11081108

11091109
export function createBinary(left: Expression, operator: BinaryOperator | BinaryOperatorToken, right: Expression) {
11101110
const node = <BinaryExpression>createSynthesizedNode(SyntaxKind.BinaryExpression);
1111-
const operatorToken = asToken(operator);
1111+
const operatorToken = asToken(operator) as BinaryOperatorToken;
11121112
const operatorKind = operatorToken.kind;
11131113
node.left = parenthesizeBinaryOperand(operatorKind, left, /*isLeftSideOfBinary*/ true, /*leftOperand*/ undefined);
11141114
node.operatorToken = operatorToken;

src/compiler/parser.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace ts {
2525
return new (IdentifierConstructor || (IdentifierConstructor = objectAllocator.getIdentifierConstructor()))(kind, pos, end);
2626
}
2727
else if (!isNodeKind(kind)) {
28-
return new (TokenConstructor || (TokenConstructor = objectAllocator.getTokenConstructor()))(kind, pos, end);
28+
return new (TokenConstructor || (TokenConstructor = objectAllocator.getTokenConstructor()))(kind as SyntaxKind, pos, end);
2929
}
3030
else {
3131
return new (NodeConstructor || (NodeConstructor = objectAllocator.getNodeConstructor()))(kind, pos, end);
@@ -93,12 +93,13 @@ namespace ts {
9393
case SyntaxKind.BindingElement:
9494
return visitNodes(cbNode, cbNodes, node.decorators) ||
9595
visitNodes(cbNode, cbNodes, node.modifiers) ||
96-
visitNode(cbNode, (<VariableLikeDeclaration>node).propertyName) ||
97-
visitNode(cbNode, (<VariableLikeDeclaration>node).dotDotDotToken) ||
98-
visitNode(cbNode, (<VariableLikeDeclaration>node).name) ||
99-
visitNode(cbNode, (<VariableLikeDeclaration>node).questionToken) ||
100-
visitNode(cbNode, (<VariableLikeDeclaration>node).type) ||
101-
visitNode(cbNode, (<VariableLikeDeclaration>node).initializer);
96+
// TODO: Specialize, remove casts to VariableLikeDeclarationBase
97+
visitNode(cbNode, (<VariableLikeDeclarationBase>node).propertyName) ||
98+
visitNode(cbNode, (<VariableLikeDeclarationBase>node).dotDotDotToken) ||
99+
visitNode(cbNode, (<VariableLikeDeclarationBase>node).name) ||
100+
visitNode(cbNode, (<VariableLikeDeclarationBase>node).questionToken) ||
101+
visitNode(cbNode, (<VariableLikeDeclarationBase>node).type) ||
102+
visitNode(cbNode, (<VariableLikeDeclarationBase>node).initializer);
102103
case SyntaxKind.FunctionType:
103104
case SyntaxKind.ConstructorType:
104105
case SyntaxKind.CallSignature:
@@ -298,9 +299,9 @@ namespace ts {
298299
case SyntaxKind.InterfaceDeclaration:
299300
return visitNodes(cbNode, cbNodes, node.decorators) ||
300301
visitNodes(cbNode, cbNodes, node.modifiers) ||
301-
visitNode(cbNode, (<InterfaceDeclaration>node).name) ||
302-
visitNodes(cbNode, cbNodes, (<InterfaceDeclaration>node).typeParameters) ||
303-
visitNodes(cbNode, cbNodes, (<ClassDeclaration>node).heritageClauses) ||
302+
visitNode(cbNode, node.name) ||
303+
visitNodes(cbNode, cbNodes, node.typeParameters) ||
304+
visitNodes(cbNode, cbNodes, node.heritageClauses) ||
304305
visitNodes(cbNode, cbNodes, (<InterfaceDeclaration>node).members);
305306
case SyntaxKind.TypeAliasDeclaration:
306307
return visitNodes(cbNode, cbNodes, node.decorators) ||
@@ -1089,7 +1090,7 @@ namespace ts {
10891090

10901091
function parseExpectedToken<TKind extends SyntaxKind>(t: TKind, reportAtCurrentPosition: boolean, diagnosticMessage: DiagnosticMessage, arg0?: any): Token<TKind>;
10911092
function parseExpectedToken(t: SyntaxKind, reportAtCurrentPosition: boolean, diagnosticMessage: DiagnosticMessage, arg0?: any): Node {
1092-
return parseOptionalToken(t) ||
1093+
return parseOptionalToken(t) as Node ||
10931094
createMissingNode(t, reportAtCurrentPosition, diagnosticMessage, arg0);
10941095
}
10951096

@@ -1180,7 +1181,7 @@ namespace ts {
11801181
(result as LiteralLikeNode).text = "";
11811182
}
11821183

1183-
return finishNode(result) as T;
1184+
return finishNode(result as Node) as T;
11841185
}
11851186

11861187
function internIdentifier(text: string): string {
@@ -1609,11 +1610,11 @@ namespace ts {
16091610

16101611
// Ok, we have a node that looks like it could be reused. Now verify that it is valid
16111612
// in the current list parsing context that we're currently at.
1612-
if (!canReuseNode(node, parsingContext)) {
1613+
if (!canReuseNode(node as Node, parsingContext)) {
16131614
return undefined;
16141615
}
16151616

1616-
return node;
1617+
return node as Node;
16171618
}
16181619

16191620
function consumeNode(node: Node) {
@@ -6970,23 +6971,23 @@ namespace ts {
69706971

69716972
function moveElementEntirelyPastChangeRange(element: IncrementalElement, isArray: boolean, delta: number, oldText: string, newText: string, aggressiveChecks: boolean) {
69726973
if (isArray) {
6973-
visitArray(<IncrementalNodeArray>element);
6974+
visitArray(<NodeArray<Node>><IncrementalNodeArray>element);
69746975
}
69756976
else {
6976-
visitNode(<IncrementalNode>element);
6977+
visitNode(<Node><IncrementalNode>element);
69776978
}
69786979
return;
69796980

6980-
function visitNode(node: IncrementalNode) {
6981+
function visitNode(node: Node) {
69816982
let text = "";
69826983
if (aggressiveChecks && shouldCheckNode(node)) {
69836984
text = oldText.substring(node.pos, node.end);
69846985
}
69856986

69866987
// Ditch any existing LS children we may have created. This way we can avoid
69876988
// moving them forward.
6988-
if (node._children) {
6989-
node._children = undefined;
6989+
if ((node as IncrementalNode)._children) {
6990+
(node as IncrementalNode)._children = undefined;
69906991
}
69916992

69926993
node.pos += delta;
@@ -6996,7 +6997,7 @@ namespace ts {
69966997
Debug.assert(text === newText.substring(node.pos, node.end));
69976998
}
69986999

6999-
forEachChild(node, visitNode, visitArray);
7000+
forEachChild(node as Node, visitNode, visitArray);
70007001
if (hasJSDocNodes(node)) {
70017002
for (const jsDocComment of node.jsDoc) {
70027003
forEachChild(jsDocComment, visitNode, visitArray);
@@ -7005,8 +7006,8 @@ namespace ts {
70057006
checkNodePositions(node, aggressiveChecks);
70067007
}
70077008

7008-
function visitArray(array: IncrementalNodeArray) {
7009-
array._children = undefined;
7009+
function visitArray(array: NodeArray<Node>) {
7010+
(array as IncrementalNodeArray)._children = undefined;
70107011
array.pos += delta;
70117012
array.end += delta;
70127013

@@ -7016,7 +7017,7 @@ namespace ts {
70167017
}
70177018
}
70187019

7019-
function shouldCheckNode(node: Node) {
7020+
function shouldCheckNode(node: BaseNode) {
70207021
switch (node.kind) {
70217022
case SyntaxKind.StringLiteral:
70227023
case SyntaxKind.NumericLiteral:
@@ -7145,9 +7146,9 @@ namespace ts {
71457146

71467147
// Adjust the pos or end (or both) of the intersecting element accordingly.
71477148
adjustIntersectingElement(child, changeStart, changeRangeOldEnd, changeRangeNewEnd, delta);
7148-
forEachChild(child, visitNode, visitArray);
7149+
forEachChild(child as Node, visitNode as (n: Node) => undefined, visitArray as (n: NodeArray<Node>) => undefined);
71497150

7150-
checkNodePositions(child, aggressiveChecks);
7151+
checkNodePositions(child as Node, aggressiveChecks);
71517152
return;
71527153
}
71537154

@@ -7336,7 +7337,7 @@ namespace ts {
73367337
_children: Node[];
73377338
}
73387339

7339-
export interface IncrementalNode extends Node, IncrementalElement {
7340+
export interface IncrementalNode extends BaseNode, IncrementalElement {
73407341
hasBeenIncrementallyParsed: boolean;
73417342
}
73427343

src/compiler/transformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ namespace ts {
9393
* @param allowDtsFiles A value indicating whether to allow the transformation of .d.ts files.
9494
*/
9595
export function transformNodes<T extends Node>(resolver: EmitResolver, host: EmitHost, options: CompilerOptions, nodes: ReadonlyArray<T>, transformers: ReadonlyArray<TransformerFactory<T>>, allowDtsFiles: boolean): TransformationResult<T> {
96-
const enabledSyntaxKindFeatures = new Array<SyntaxKindFeatureFlags>(SyntaxKind.Count);
96+
const enabledSyntaxKindFeatures = new Array<SyntaxKindFeatureFlags>(SyntaxKind.Count + 1);
9797
let lexicalEnvironmentVariableDeclarations: VariableDeclaration[];
9898
let lexicalEnvironmentFunctionDeclarations: FunctionDeclaration[];
9999
let lexicalEnvironmentVariableDeclarationsStack: VariableDeclaration[][] = [];

src/compiler/transformers/es2015.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ namespace ts {
342342
function shouldVisitNode(node: Node): boolean {
343343
return (node.transformFlags & TransformFlags.ContainsES2015) !== 0
344344
|| convertedLoopState !== undefined
345-
|| (hierarchyFacts & HierarchyFacts.ConstructorWithCapturedSuper && (isStatement(node) || (node.kind === SyntaxKind.Block)))
345+
|| (hierarchyFacts & HierarchyFacts.ConstructorWithCapturedSuper && (isStatement(node) || ((node as Node).kind === SyntaxKind.Block)))
346346
|| (isIterationStatement(node, /*lookInLabeledStatements*/ false) && shouldConvertIterationStatementBody(node))
347347
|| (getEmitFlags(node) & EmitFlags.TypeScriptClassWrapper) !== 0;
348348
}

src/compiler/transformers/module/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ namespace ts {
15511551
? setTextRange(
15521552
createBinary(
15531553
node.operand,
1554-
createToken(node.operator === SyntaxKind.PlusPlusToken ? SyntaxKind.PlusEqualsToken : SyntaxKind.MinusEqualsToken),
1554+
node.operator === SyntaxKind.PlusPlusToken ? createToken(SyntaxKind.PlusEqualsToken) : createToken(SyntaxKind.MinusEqualsToken),
15551555
createLiteral(1)
15561556
),
15571557
/*location*/ node)

0 commit comments

Comments
 (0)