Skip to content

Commit 790f65d

Browse files
author
Andy
authored
Simplify isJumpStatementTarget and isLabelOfLabeledStatement users using type predicates (#22100)
1 parent b90a56d commit 790f65d

File tree

4 files changed

+39
-48
lines changed

4 files changed

+39
-48
lines changed

src/services/findAllReferences.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -330,17 +330,15 @@ namespace ts.FindAllReferences.Core {
330330
}
331331

332332
// Labels
333-
if (isLabelName(node)) {
334-
if (isJumpStatementTarget(node)) {
335-
const labelDefinition = getTargetLabel((<BreakOrContinueStatement>node.parent), (<Identifier>node).text);
336-
// if we have a label definition, look within its statement for references, if not, then
337-
// the label is undefined and we have no results..
338-
return labelDefinition && getLabelReferencesInNode(labelDefinition.parent, labelDefinition);
339-
}
340-
else {
341-
// it is a label definition and not a target, search within the parent labeledStatement
342-
return getLabelReferencesInNode(node.parent, <Identifier>node);
343-
}
333+
if (isJumpStatementTarget(node)) {
334+
const labelDefinition = getTargetLabel(node.parent, node.text);
335+
// if we have a label definition, look within its statement for references, if not, then
336+
// the label is undefined and we have no results..
337+
return labelDefinition && getLabelReferencesInNode(labelDefinition.parent, labelDefinition);
338+
}
339+
else if (isLabelOfLabeledStatement(node)) {
340+
// it is a label definition and not a target, search within the parent labeledStatement
341+
return getLabelReferencesInNode(node.parent, node);
344342
}
345343

346344
if (isThis(node)) {

src/services/goToDefinition.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ namespace ts.GoToDefinition {
1313

1414
// Labels
1515
if (isJumpStatementTarget(node)) {
16-
const labelName = (<Identifier>node).text;
17-
const label = getTargetLabel((<BreakOrContinueStatement>node.parent), labelName);
18-
return label ? [createDefinitionInfoFromName(label, ScriptElementKind.label, labelName, /*containerName*/ undefined)] : undefined;
16+
const label = getTargetLabel(node.parent, node.text);
17+
return label ? [createDefinitionInfoFromName(label, ScriptElementKind.label, node.text, /*containerName*/ undefined)] : undefined;
1918
}
2019

2120
const typeChecker = program.getTypeChecker();

src/services/services.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,10 +1467,7 @@ namespace ts {
14671467
const sourceFile = getValidSourceFile(fileName);
14681468
const node = getTouchingPropertyName(sourceFile, position, /*includeJsDocComment*/ true);
14691469
if (node === sourceFile) {
1470-
return undefined;
1471-
}
1472-
1473-
if (isLabelName(node)) {
1470+
// Avoid giving quickInfo for the sourceFile as a whole.
14741471
return undefined;
14751472
}
14761473

@@ -1481,36 +1478,39 @@ namespace ts {
14811478
// Try getting just type at this position and show
14821479
switch (node.kind) {
14831480
case SyntaxKind.Identifier:
1481+
if (isLabelName(node)) {
1482+
// Type here will be 'any', avoid displaying this.
1483+
return undefined;
1484+
}
1485+
// falls through
14841486
case SyntaxKind.PropertyAccessExpression:
14851487
case SyntaxKind.QualifiedName:
14861488
case SyntaxKind.ThisKeyword:
14871489
case SyntaxKind.ThisType:
14881490
case SyntaxKind.SuperKeyword:
14891491
// For the identifiers/this/super etc get the type at position
14901492
const type = typeChecker.getTypeAtLocation(node);
1491-
if (type) {
1492-
return {
1493-
kind: ScriptElementKind.unknown,
1494-
kindModifiers: ScriptElementKindModifier.none,
1495-
textSpan: createTextSpan(node.getStart(), node.getWidth()),
1496-
displayParts: typeToDisplayParts(typeChecker, type, getContainerNode(node)),
1497-
documentation: type.symbol ? type.symbol.getDocumentationComment(typeChecker) : undefined,
1498-
tags: type.symbol ? type.symbol.getJsDocTags() : undefined
1499-
};
1500-
}
1493+
return type && {
1494+
kind: ScriptElementKind.unknown,
1495+
kindModifiers: ScriptElementKindModifier.none,
1496+
textSpan: createTextSpanFromNode(node, sourceFile),
1497+
displayParts: typeToDisplayParts(typeChecker, type, getContainerNode(node)),
1498+
documentation: type.symbol ? type.symbol.getDocumentationComment(typeChecker) : undefined,
1499+
tags: type.symbol ? type.symbol.getJsDocTags() : undefined
1500+
};
15011501
}
15021502

15031503
return undefined;
15041504
}
15051505

1506-
const displayPartsDocumentationsAndKind = SymbolDisplay.getSymbolDisplayPartsDocumentationAndSymbolKind(typeChecker, symbol, sourceFile, getContainerNode(node), node);
1506+
const { symbolKind, displayParts, documentation, tags } = SymbolDisplay.getSymbolDisplayPartsDocumentationAndSymbolKind(typeChecker, symbol, sourceFile, getContainerNode(node), node);
15071507
return {
1508-
kind: displayPartsDocumentationsAndKind.symbolKind,
1508+
kind: symbolKind,
15091509
kindModifiers: SymbolDisplay.getSymbolModifiers(symbol),
1510-
textSpan: createTextSpan(node.getStart(), node.getWidth()),
1511-
displayParts: displayPartsDocumentationsAndKind.displayParts,
1512-
documentation: displayPartsDocumentationsAndKind.documentation,
1513-
tags: displayPartsDocumentationsAndKind.tags
1510+
textSpan: createTextSpanFromNode(node, sourceFile),
1511+
displayParts,
1512+
documentation,
1513+
tags,
15141514
};
15151515
}
15161516

@@ -1519,11 +1519,9 @@ namespace ts {
15191519
&& isPropertyAssignment(node.parent)
15201520
&& node.parent.name === node) {
15211521
const type = checker.getContextualType(node.parent.parent);
1522-
if (type) {
1523-
const property = checker.getPropertyOfType(type, getTextOfIdentifierOrLiteral(node));
1524-
if (property) {
1525-
return property;
1526-
}
1522+
const property = type && checker.getPropertyOfType(type, getTextOfIdentifierOrLiteral(node));
1523+
if (property) {
1524+
return property;
15271525
}
15281526
}
15291527
return checker.getSymbolAtLocation(node);

src/services/utilities.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,12 @@ namespace ts {
214214
return undefined;
215215
}
216216

217-
export function isJumpStatementTarget(node: Node): boolean {
218-
return node.kind === SyntaxKind.Identifier &&
219-
(node.parent.kind === SyntaxKind.BreakStatement || node.parent.kind === SyntaxKind.ContinueStatement) &&
220-
(<BreakOrContinueStatement>node.parent).label === node;
221-
}
217+
export function isJumpStatementTarget(node: Node): node is Identifier & { parent: BreakOrContinueStatement } {
218+
return node.kind === SyntaxKind.Identifier && isBreakOrContinueStatement(node.parent) && node.parent.label === node;
219+
}
222220

223-
function isLabelOfLabeledStatement(node: Node): boolean {
224-
return node.kind === SyntaxKind.Identifier &&
225-
node.parent.kind === SyntaxKind.LabeledStatement &&
226-
(<LabeledStatement>node.parent).label === node;
221+
export function isLabelOfLabeledStatement(node: Node): node is Identifier {
222+
return node.kind === SyntaxKind.Identifier && isLabeledStatement(node.parent) && node.parent.label === node;
227223
}
228224

229225
export function isLabelName(node: Node): boolean {

0 commit comments

Comments
 (0)