Skip to content

Commit f6803c6

Browse files
committed
Finish updating services layer
1 parent 6c892fe commit f6803c6

8 files changed

+63
-31
lines changed

src/compiler/utilities.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,38 @@ namespace ts {
15221522
return false;
15231523
}
15241524

1525+
export function hasType(node: VariableLikeDeclaration) {
1526+
if (node) {
1527+
switch (node.kind) {
1528+
case SyntaxKind.VariableDeclaration:
1529+
case SyntaxKind.PropertyDeclaration:
1530+
case SyntaxKind.PropertySignature:
1531+
case SyntaxKind.Parameter:
1532+
return node.type !== undefined;
1533+
}
1534+
}
1535+
1536+
return false;
1537+
}
1538+
1539+
export function canHaveType(node: Declaration): node is
1540+
| VariableDeclaration
1541+
| PropertyDeclaration
1542+
| PropertySignature
1543+
| ParameterDeclaration {
1544+
if (node) {
1545+
switch (node.kind) {
1546+
case SyntaxKind.VariableDeclaration:
1547+
case SyntaxKind.PropertyDeclaration:
1548+
case SyntaxKind.PropertySignature:
1549+
case SyntaxKind.Parameter:
1550+
return true;
1551+
}
1552+
}
1553+
1554+
return false;
1555+
}
1556+
15251557
export function canHaveInitializer(node: Declaration): node is
15261558
| VariableDeclaration
15271559
| PropertyAssignment
@@ -2083,7 +2115,7 @@ namespace ts {
20832115
return isPropertyAccessExpression(node) && isESSymbolIdentifier(node.expression);
20842116
}
20852117

2086-
export function getPropertyNameForPropertyNameNode(name: DeclarationName): __String {
2118+
export function getPropertyNameForPropertyNameNode(name: DeclarationName | QualifiedName): __String {
20872119
if (name.kind === SyntaxKind.Identifier) {
20882120
return name.escapedText;
20892121
}

src/services/breakpoints.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ namespace ts.BreakpointResolver {
329329

330330
// If this is name of property assignment, set breakpoint in the initializer
331331
if (node.parent.kind === SyntaxKind.PropertyAssignment &&
332-
(<PropertyDeclaration>node.parent).name === node &&
332+
node.parent.name === node &&
333333
!isArrayLiteralOrObjectLiteralDestructuringPattern(node.parent.parent)) {
334-
return spanInNode((<PropertyDeclaration>node.parent).initializer);
334+
return spanInNode(node.parent.initializer);
335335
}
336336

337337
// Breakpoint in type assertion goes to its operand

src/services/completions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ namespace ts.Completions {
987987
// through type declaration or inference.
988988
// Also proceed if rootDeclaration is a parameter and if its containing function expression/arrow function is contextually typed -
989989
// type of parameter will flow in from the contextual type of the function
990-
let canGetType = rootDeclaration.initializer || rootDeclaration.type || rootDeclaration.parent.parent.kind === SyntaxKind.ForOfStatement;
990+
let canGetType = hasInitializer(rootDeclaration) || hasType(rootDeclaration) || rootDeclaration.parent.parent.kind === SyntaxKind.ForOfStatement;
991991
if (!canGetType && rootDeclaration.kind === SyntaxKind.Parameter) {
992992
if (isExpression(rootDeclaration.parent)) {
993993
canGetType = !!typeChecker.getContextualType(<Expression>rootDeclaration.parent);

src/services/documentHighlights.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ namespace ts.DocumentHighlights {
191191
let child: Node = throwStatement;
192192

193193
while (child.parent) {
194-
const parent = child.parent;
194+
const parent: Node = child.parent;
195195

196196
if (isFunctionBlock(parent) || parent.kind === SyntaxKind.SourceFile) {
197197
return parent;
@@ -307,7 +307,7 @@ namespace ts.DocumentHighlights {
307307
nodes = [...(<ClassDeclaration>declaration).members, declaration];
308308
}
309309
else {
310-
nodes = (<Block>container).statements;
310+
nodes = container.statements;
311311
}
312312
break;
313313
case SyntaxKind.Constructor:
@@ -383,7 +383,7 @@ namespace ts.DocumentHighlights {
383383

384384
return keywords;
385385

386-
function tryPushAccessorKeyword(accessorSymbol: Symbol, accessorKind: SyntaxKind): void {
386+
function tryPushAccessorKeyword(accessorSymbol: Symbol, accessorKind: SyntaxKind.GetAccessor | SyntaxKind.SetAccessor): void {
387387
const accessor = getDeclarationOfKind(accessorSymbol, accessorKind);
388388

389389
if (accessor) {

src/services/findAllReferences.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ namespace ts.FindAllReferences.Core {
10581058
const containingTypeReference = getContainingTypeReference(refNode);
10591059
if (containingTypeReference && state.markSeenContainingTypeReference(containingTypeReference)) {
10601060
const parent = containingTypeReference.parent;
1061-
if (isVariableLike(parent) && parent.type === containingTypeReference && parent.initializer && isImplementationExpression(parent.initializer)) {
1061+
if (isVariableLike(parent) && canHaveType(parent) && parent.type === containingTypeReference && parent.initializer && isImplementationExpression(parent.initializer)) {
10621062
addReference(parent.initializer);
10631063
}
10641064
else if (isFunctionLike(parent) && parent.type === containingTypeReference && (parent as FunctionLikeDeclaration).body) {
@@ -1647,7 +1647,7 @@ namespace ts.FindAllReferences.Core {
16471647
return false;
16481648
}
16491649
else if (isVariableLike(node)) {
1650-
if (node.initializer) {
1650+
if (hasInitializer(node)) {
16511651
return true;
16521652
}
16531653
else if (node.kind === SyntaxKind.VariableDeclaration) {

src/services/services.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ namespace ts {
6565
}
6666

6767
public getSourceFile(): SourceFile {
68-
return getSourceFileOfNode(this);
68+
return getSourceFileOfNode(this as Node);
6969
}
7070

7171
public getStart(sourceFile?: SourceFileLike, includeJsDocComment?: boolean): number {
72-
return getTokenPosOfNode(this, sourceFile, includeJsDocComment);
72+
return getTokenPosOfNode(this as Node, sourceFile, includeJsDocComment);
7373
}
7474

7575
public getFullStart(): number {
@@ -109,7 +109,7 @@ namespace ts {
109109
const token = scanner.scan();
110110
const textPos = scanner.getTextPos();
111111
if (textPos <= end) {
112-
nodes.push(createNode(token, pos, textPos, this));
112+
nodes.push(createNode(token, pos, textPos, this as Node) as Node);
113113
}
114114
pos = textPos;
115115
if (token === SyntaxKind.EndOfFileToken) {
@@ -120,7 +120,7 @@ namespace ts {
120120
}
121121

122122
private createSyntaxList(nodes: NodeArray<Node>): Node {
123-
const list = <NodeObject>createNode(SyntaxKind.SyntaxList, nodes.pos, nodes.end, this);
123+
const list = <NodeObject>createNode(SyntaxKind.SyntaxList, nodes.pos, nodes.end, this as Node);
124124
list._children = [];
125125
let pos = nodes.pos;
126126

@@ -134,7 +134,7 @@ namespace ts {
134134
if (pos < nodes.end) {
135135
this.addSyntheticNodes(list._children, pos, nodes.end);
136136
}
137-
return list;
137+
return list as Node;
138138
}
139139

140140
private createChildren(sourceFile?: SourceFileLike) {
@@ -143,7 +143,7 @@ namespace ts {
143143
return;
144144
}
145145

146-
if (isJSDocCommentContainingNode(this)) {
146+
if (isJSDocCommentContainingNode(this as Node)) {
147147
/** Don't add trivia for "tokens" since this is in a comment. */
148148
const children: Node[] = [];
149149
this.forEachChild(child => { children.push(child); });
@@ -176,7 +176,7 @@ namespace ts {
176176
// For that to work, the jsdoc comments should still be the leading trivia of the first child.
177177
// Restoring the scanner position ensures that.
178178
pos = this.pos;
179-
forEachChild(this, processNode, processNodes);
179+
forEachChild(this as Node, processNode, processNodes);
180180
if (pos < this.end) {
181181
this.addSyntheticNodes(children, pos, this.end);
182182
}
@@ -223,11 +223,11 @@ namespace ts {
223223
}
224224

225225
public forEachChild<T>(cbNode: (node: Node) => T, cbNodeArray?: (nodes: NodeArray<Node>) => T): T {
226-
return forEachChild(this, cbNode, cbNodeArray);
226+
return forEachChild(this as Node, cbNode, cbNodeArray);
227227
}
228228
}
229229

230-
class TokenOrIdentifierObject implements Node {
230+
class TokenOrIdentifierObject implements BaseNode {
231231
public kind: SyntaxKind;
232232
public pos: number;
233233
public end: number;
@@ -244,11 +244,11 @@ namespace ts {
244244
}
245245

246246
public getSourceFile(): SourceFile {
247-
return getSourceFileOfNode(this);
247+
return getSourceFileOfNode(this as Node);
248248
}
249249

250250
public getStart(sourceFile?: SourceFileLike, includeJsDocComment?: boolean): number {
251-
return getTokenPosOfNode(this, sourceFile, includeJsDocComment);
251+
return getTokenPosOfNode(this as Node, sourceFile, includeJsDocComment);
252252
}
253253

254254
public getFullStart(): number {
@@ -740,8 +740,8 @@ namespace ts {
740740

741741
function getServicesObjectAllocator(): ObjectAllocator {
742742
return {
743-
getNodeConstructor: () => NodeObject,
744-
getTokenConstructor: () => TokenObject,
743+
getNodeConstructor: () => NodeObject as new (kind: SyntaxKind, pos?: number, end?: number) => Node,
744+
getTokenConstructor: () => TokenObject as new (kind: SyntaxKind, pos?: number, end?: number) => Node,
745745

746746
getIdentifierConstructor: () => IdentifierObject,
747747
getSourceFileConstructor: () => SourceFileObject,
@@ -1595,7 +1595,7 @@ namespace ts {
15951595
return;
15961596
}
15971597

1598-
let nodeForStartPos = node;
1598+
let nodeForStartPos: Node = node;
15991599
while (true) {
16001600
if (isRightSideOfPropertyAccess(nodeForStartPos) || isRightSideOfQualifiedName(nodeForStartPos)) {
16011601
// If on the span is in right side of the the property or qualified name, return the span from the qualified name pos to end of this node

src/services/signatureHelp.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ namespace ts.SignatureHelp {
154154
const tagExpression = <TaggedTemplateExpression>templateExpression.parent;
155155
Debug.assert(templateExpression.kind === SyntaxKind.TemplateExpression);
156156

157-
const argumentIndex = isInsideTemplateLiteral(<LiteralExpression>node, position) ? 0 : 1;
157+
const argumentIndex = 1;
158158

159159
return getArgumentListInfoForTemplate(tagExpression, argumentIndex, sourceFile);
160160
}
@@ -165,7 +165,7 @@ namespace ts.SignatureHelp {
165165
Debug.assert(templateExpression.kind === SyntaxKind.TemplateExpression);
166166

167167
// If we're just after a template tail, don't show signature help.
168-
if (node.kind === SyntaxKind.TemplateTail && !isInsideTemplateLiteral(<LiteralExpression>node, position)) {
168+
if (node.kind === SyntaxKind.TemplateTail) {
169169
return undefined;
170170
}
171171

src/services/textChanges.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ namespace ts.textChanges {
166166
/**
167167
* Checks if 'candidate' argument is a legal separator in the list that contains 'node' as an element
168168
*/
169-
function isSeparator(node: Node, candidate: Node): candidate is Token<SyntaxKind.CommaToken | SyntaxKind.SemicolonToken> {
169+
function isSeparator(node: Node, candidate: Node): candidate is Token<SyntaxKind.CommaToken> | Token<SyntaxKind.SemicolonToken> {
170170
return candidate && node.parent && (candidate.kind === SyntaxKind.CommaToken || (candidate.kind === SyntaxKind.SemicolonToken && node.parent.kind === SyntaxKind.ObjectLiteralExpression));
171171
}
172172

@@ -425,7 +425,7 @@ namespace ts.textChanges {
425425
const afterStart = after.getStart(sourceFile);
426426
const afterStartLinePosition = getLineStartPositionForPosition(afterStart, sourceFile);
427427

428-
let separator: SyntaxKind.CommaToken | SyntaxKind.SemicolonToken;
428+
let separator: Token<SyntaxKind.CommaToken> | Token<SyntaxKind.SemicolonToken>;
429429
let multilineList = false;
430430

431431
// insert element after the last element in the list that has more than one item
@@ -436,12 +436,12 @@ namespace ts.textChanges {
436436
// if list has only one element then we'll format is as multiline if node has comment in trailing trivia, or as singleline otherwise
437437
// i.e. var x = 1 // this is x
438438
// | new element will be inserted at this position
439-
separator = SyntaxKind.CommaToken;
439+
separator = createToken(SyntaxKind.CommaToken);
440440
}
441441
else {
442442
// element has more than one element, pick separator from the list
443443
const tokenBeforeInsertPosition = findPrecedingToken(after.pos, sourceFile);
444-
separator = isSeparator(after, tokenBeforeInsertPosition) ? tokenBeforeInsertPosition.kind : SyntaxKind.CommaToken;
444+
separator = isSeparator(after, tokenBeforeInsertPosition) ? createToken(tokenBeforeInsertPosition.kind) as typeof separator : createToken(SyntaxKind.CommaToken);
445445
// determine if list is multiline by checking lines of after element and element that precedes it.
446446
const afterMinusOneStartLinePosition = getLineStartPositionForPosition(containingList[index - 1].getStart(sourceFile), sourceFile);
447447
multilineList = afterMinusOneStartLinePosition !== afterStartLinePosition;
@@ -456,7 +456,7 @@ namespace ts.textChanges {
456456
kind: ChangeKind.ReplaceWithSingleNode,
457457
sourceFile,
458458
range: { pos: end, end },
459-
node: createToken(separator),
459+
node: separator,
460460
options: {}
461461
});
462462
// use the same indentation as 'after' item
@@ -480,7 +480,7 @@ namespace ts.textChanges {
480480
sourceFile,
481481
range: { pos: end, end },
482482
node: newNode,
483-
options: { prefix: `${tokenToString(separator)} ` }
483+
options: { prefix: `${tokenToString(separator.kind)} ` }
484484
});
485485
}
486486
}

0 commit comments

Comments
 (0)