Skip to content

Commit bb2c58b

Browse files
author
Andy
authored
Simplify uses of getPossibleSymbolReferencePositions (#22099)
1 parent 790f65d commit bb2c58b

File tree

1 file changed

+23
-60
lines changed

1 file changed

+23
-60
lines changed

src/services/findAllReferences.ts

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -743,18 +743,13 @@ namespace ts.FindAllReferences.Core {
743743
}
744744

745745
function getLabelReferencesInNode(container: Node, targetLabel: Identifier): SymbolAndEntries[] {
746-
const references: Entry[] = [];
747746
const sourceFile = container.getSourceFile();
748747
const labelName = targetLabel.text;
749-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, labelName, container);
750-
for (const position of possiblePositions) {
748+
const references = mapDefined(getPossibleSymbolReferencePositions(sourceFile, labelName, container), position => {
751749
const node = getTouchingWord(sourceFile, position, /*includeJsDocComment*/ false);
752750
// Only pick labels that are either the target label, or have a target that is the target label
753-
if (node && (node === targetLabel || (isJumpStatementTarget(node) && getTargetLabel(node, labelName) === targetLabel))) {
754-
references.push(nodeEntry(node));
755-
}
756-
}
757-
751+
return node && (node === targetLabel || (isJumpStatementTarget(node) && getTargetLabel(node, labelName) === targetLabel)) ? nodeEntry(node) : undefined;
752+
});
758753
return [{ definition: { type: "label", node: targetLabel }, references }];
759754
}
760755

@@ -780,24 +775,16 @@ namespace ts.FindAllReferences.Core {
780775
}
781776

782777
function getAllReferencesForKeyword(sourceFiles: ReadonlyArray<SourceFile>, keywordKind: ts.SyntaxKind, cancellationToken: CancellationToken): SymbolAndEntries[] {
783-
const references: NodeEntry[] = [];
784-
for (const sourceFile of sourceFiles) {
778+
const references = flatMap(sourceFiles, sourceFile => {
785779
cancellationToken.throwIfCancellationRequested();
786-
addReferencesForKeywordInFile(sourceFile, keywordKind, tokenToString(keywordKind), references);
787-
}
780+
return mapDefined(getPossibleSymbolReferencePositions(sourceFile, tokenToString(keywordKind), sourceFile), position => {
781+
const referenceLocation = getTouchingPropertyName(sourceFile, position, /*includeJsDocComment*/ true);
782+
return referenceLocation.kind === keywordKind ? nodeEntry(referenceLocation) : undefined;
783+
});
784+
});
788785
return references.length ? [{ definition: { type: "keyword", node: references[0].node }, references }] : undefined;
789786
}
790787

791-
function addReferencesForKeywordInFile(sourceFile: SourceFile, kind: SyntaxKind, searchText: string, references: Push<NodeEntry>): void {
792-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, searchText, sourceFile);
793-
for (const position of possiblePositions) {
794-
const referenceLocation = getTouchingPropertyName(sourceFile, position, /*includeJsDocComment*/ true);
795-
if (referenceLocation.kind === kind) {
796-
references.push(nodeEntry(referenceLocation));
797-
}
798-
}
799-
}
800-
801788
function getReferencesInSourceFile(sourceFile: ts.SourceFile, search: Search, state: State): void {
802789
state.cancellationToken.throwIfCancellationRequested();
803790
return getReferencesInContainer(sourceFile, sourceFile, search, state);
@@ -1288,26 +1275,20 @@ namespace ts.FindAllReferences.Core {
12881275
return undefined;
12891276
}
12901277

1291-
const references: Entry[] = [];
1292-
12931278
const sourceFile = searchSpaceNode.getSourceFile();
1294-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "super", searchSpaceNode);
1295-
for (const position of possiblePositions) {
1279+
const references = mapDefined(getPossibleSymbolReferencePositions(sourceFile, "super", searchSpaceNode), position => {
12961280
const node = getTouchingWord(sourceFile, position, /*includeJsDocComment*/ false);
1297-
12981281
if (!node || node.kind !== SyntaxKind.SuperKeyword) {
1299-
continue;
1282+
return;
13001283
}
13011284

13021285
const container = getSuperContainer(node, /*stopOnFunctions*/ false);
13031286

13041287
// If we have a 'super' container, we must have an enclosing class.
13051288
// Now make sure the owning class is the same as the search-space
13061289
// and has the same static qualifier as the original 'super's owner.
1307-
if (container && (ModifierFlags.Static & getModifierFlags(container)) === staticFlag && container.parent.symbol === searchSpaceNode.symbol) {
1308-
references.push(nodeEntry(node));
1309-
}
1310-
}
1290+
return container && (ModifierFlags.Static & getModifierFlags(container)) === staticFlag && container.parent.symbol === searchSpaceNode.symbol ? nodeEntry(node) : undefined;
1291+
});
13111292

13121293
return [{ definition: { type: "symbol", symbol: searchSpaceNode.symbol, node: superKeyword }, references }];
13131294
}
@@ -1348,19 +1329,10 @@ namespace ts.FindAllReferences.Core {
13481329
}
13491330

13501331
const references: Entry[] = [];
1351-
1352-
let possiblePositions: ReadonlyArray<number>;
1353-
if (searchSpaceNode.kind === SyntaxKind.SourceFile) {
1354-
forEach(sourceFiles, sourceFile => {
1355-
cancellationToken.throwIfCancellationRequested();
1356-
possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this");
1357-
getThisReferencesInFile(sourceFile, sourceFile, possiblePositions, staticFlag, references);
1358-
});
1359-
}
1360-
else {
1361-
const sourceFile = searchSpaceNode.getSourceFile();
1362-
possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this", searchSpaceNode);
1363-
getThisReferencesInFile(sourceFile, searchSpaceNode, possiblePositions, staticFlag, references);
1332+
for (const sourceFile of searchSpaceNode.kind === SyntaxKind.SourceFile ? sourceFiles : [searchSpaceNode.getSourceFile()]) {
1333+
cancellationToken.throwIfCancellationRequested();
1334+
const positions = getPossibleSymbolReferencePositions(sourceFile, "this", isSourceFile(searchSpaceNode) ? sourceFile : searchSpaceNode);
1335+
getThisReferencesInFile(sourceFile, searchSpaceNode.kind === SyntaxKind.SourceFile ? sourceFile : searchSpaceNode, positions, staticFlag, references);
13641336
}
13651337

13661338
return [{
@@ -1409,27 +1381,18 @@ namespace ts.FindAllReferences.Core {
14091381
}
14101382

14111383
function getReferencesForStringLiteral(node: StringLiteral, sourceFiles: ReadonlyArray<SourceFile>, cancellationToken: CancellationToken): SymbolAndEntries[] {
1412-
const references: NodeEntry[] = [];
1413-
1414-
for (const sourceFile of sourceFiles) {
1384+
const references = flatMap(sourceFiles, sourceFile => {
14151385
cancellationToken.throwIfCancellationRequested();
1416-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, node.text);
1417-
getReferencesForStringLiteralInFile(sourceFile, node.text, possiblePositions, references);
1418-
}
1386+
return mapDefined(getPossibleSymbolReferencePositions(sourceFile, node.text), position => {
1387+
const ref = tryCast(getTouchingWord(sourceFile, position, /*includeJsDocComment*/ false), isStringLiteral);
1388+
return ref && ref.text === node.text ? nodeEntry(ref, /*isInString*/ true) : undefined;
1389+
});
1390+
});
14191391

14201392
return [{
14211393
definition: { type: "string", node },
14221394
references
14231395
}];
1424-
1425-
function getReferencesForStringLiteralInFile(sourceFile: SourceFile, searchText: string, possiblePositions: ReadonlyArray<number>, references: Push<NodeEntry>): void {
1426-
for (const position of possiblePositions) {
1427-
const node = getTouchingWord(sourceFile, position, /*includeJsDocComment*/ false);
1428-
if (node && node.kind === SyntaxKind.StringLiteral && (node as StringLiteral).text === searchText) {
1429-
references.push(nodeEntry(node, /*isInString*/ true));
1430-
}
1431-
}
1432-
}
14331396
}
14341397

14351398
// For certain symbol kinds, we need to include other symbols in the search set.

0 commit comments

Comments
 (0)