Skip to content

Commit eb09c11

Browse files
author
Andy Hanson
committed
Call the cancellationToken once per SourceFile, instead of calling it at every possible position.
1 parent 28a3604 commit eb09c11

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

src/services/findAllReferences.ts

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,11 @@ namespace ts.FindAllReferences.Core {
305305
const labelDefinition = getTargetLabel((<BreakOrContinueStatement>node.parent), (<Identifier>node).text);
306306
// if we have a label definition, look within its statement for references, if not, then
307307
// the label is undefined and we have no results..
308-
return labelDefinition && getLabelReferencesInNode(labelDefinition.parent, labelDefinition, cancellationToken);
308+
return labelDefinition && getLabelReferencesInNode(labelDefinition.parent, labelDefinition);
309309
}
310310
else {
311311
// it is a label definition and not a target, search within the parent labeledStatement
312-
return getLabelReferencesInNode(node.parent, <Identifier>node, cancellationToken);
312+
return getLabelReferencesInNode(node.parent, <Identifier>node);
313313
}
314314
}
315315

@@ -318,7 +318,7 @@ namespace ts.FindAllReferences.Core {
318318
}
319319

320320
if (node.kind === SyntaxKind.SuperKeyword) {
321-
return getReferencesForSuperKeyword(node, cancellationToken);
321+
return getReferencesForSuperKeyword(node);
322322
}
323323

324324
return undefined;
@@ -460,7 +460,7 @@ namespace ts.FindAllReferences.Core {
460460
};
461461

462462
function getImportSearches(exportSymbol: Symbol, exportInfo: ExportInfo): ImportsResult {
463-
if (!importTracker) importTracker = createImportTracker(sourceFiles, checker);
463+
if (!importTracker) importTracker = createImportTracker(sourceFiles, checker, cancellationToken);
464464
return importTracker(exportSymbol, exportInfo, options.isForRename);
465465
}
466466

@@ -515,7 +515,6 @@ namespace ts.FindAllReferences.Core {
515515

516516
// For each import, find all references to that import in its source file.
517517
for (const [importLocation, importSymbol] of importSearches) {
518-
state.cancellationToken.throwIfCancellationRequested();
519518
getReferencesInSourceFile(importLocation.getSourceFile(), state.createSearch(importLocation, importSymbol, ImportExport.Export), state);
520519
}
521520

@@ -534,7 +533,6 @@ namespace ts.FindAllReferences.Core {
534533
}
535534
if (indirectSearch) {
536535
for (const indirectUser of indirectUsers) {
537-
state.cancellationToken.throwIfCancellationRequested();
538536
searchForName(indirectUser, indirectSearch, state);
539537
}
540538
}
@@ -648,7 +646,7 @@ namespace ts.FindAllReferences.Core {
648646
return parent ? scope.getSourceFile() : scope;
649647
}
650648

651-
function getPossibleSymbolReferencePositions(sourceFile: SourceFile, symbolName: string, start: number, end: number, cancellationToken: CancellationToken): number[] {
649+
function getPossibleSymbolReferencePositions(sourceFile: SourceFile, symbolName: string, start: number, end: number): number[] {
652650
const positions: number[] = [];
653651

654652
/// TODO: Cache symbol existence for files to save text search
@@ -665,8 +663,6 @@ namespace ts.FindAllReferences.Core {
665663

666664
let position = text.indexOf(symbolName, start);
667665
while (position >= 0) {
668-
cancellationToken.throwIfCancellationRequested();
669-
670666
// If we are past the end, stop looking
671667
if (position > end) break;
672668

@@ -685,14 +681,12 @@ namespace ts.FindAllReferences.Core {
685681
return positions;
686682
}
687683

688-
function getLabelReferencesInNode(container: Node, targetLabel: Identifier, cancellationToken: CancellationToken): SymbolAndEntries[] {
684+
function getLabelReferencesInNode(container: Node, targetLabel: Identifier): SymbolAndEntries[] {
689685
const references: Entry[] = [];
690686
const sourceFile = container.getSourceFile();
691687
const labelName = targetLabel.text;
692-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, labelName, container.getStart(), container.getEnd(), cancellationToken);
688+
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, labelName, container.getStart(), container.getEnd());
693689
for (const position of possiblePositions) {
694-
cancellationToken.throwIfCancellationRequested();
695-
696690
const node = getTouchingWord(sourceFile, position);
697691
if (!node || node.getWidth() !== labelName.length) {
698692
continue;
@@ -731,15 +725,14 @@ namespace ts.FindAllReferences.Core {
731725
const references: NodeEntry[] = [];
732726
for (const sourceFile of sourceFiles) {
733727
cancellationToken.throwIfCancellationRequested();
734-
addReferencesForKeywordInFile(sourceFile, keywordKind, tokenToString(keywordKind), cancellationToken, references);
728+
addReferencesForKeywordInFile(sourceFile, keywordKind, tokenToString(keywordKind), references);
735729
}
736730
return references.length ? [{ definition: { type: "keyword", node: references[0].node }, references }] : undefined;
737731
}
738732

739-
function addReferencesForKeywordInFile(sourceFile: SourceFile, kind: SyntaxKind, searchText: string, cancellationToken: CancellationToken, references: Push<NodeEntry>): void {
740-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, searchText, sourceFile.getStart(), sourceFile.getEnd(), cancellationToken);
733+
function addReferencesForKeywordInFile(sourceFile: SourceFile, kind: SyntaxKind, searchText: string, references: Push<NodeEntry>): void {
734+
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, searchText, sourceFile.getStart(), sourceFile.getEnd());
741735
for (const position of possiblePositions) {
742-
cancellationToken.throwIfCancellationRequested();
743736
const referenceLocation = getTouchingPropertyName(sourceFile, position);
744737
if (referenceLocation.kind === kind) {
745738
references.push(nodeEntry(referenceLocation));
@@ -748,6 +741,7 @@ namespace ts.FindAllReferences.Core {
748741
}
749742

750743
function getReferencesInSourceFile(sourceFile: ts.SourceFile, search: Search, state: State): void {
744+
state.cancellationToken.throwIfCancellationRequested();
751745
return getReferencesInContainer(sourceFile, sourceFile, search, state);
752746
}
753747

@@ -762,9 +756,8 @@ namespace ts.FindAllReferences.Core {
762756
}
763757

764758
const start = state.findInComments ? container.getFullStart() : container.getStart();
765-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, search.text, start, container.getEnd(), state.cancellationToken);
759+
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, search.text, start, container.getEnd());
766760
for (const position of possiblePositions) {
767-
state.cancellationToken.throwIfCancellationRequested();
768761
getReferencesAtLocation(sourceFile, position, search, state);
769762
}
770763
}
@@ -1182,7 +1175,7 @@ namespace ts.FindAllReferences.Core {
11821175
}
11831176
}
11841177

1185-
function getReferencesForSuperKeyword(superKeyword: Node, cancellationToken: CancellationToken): SymbolAndEntries[] {
1178+
function getReferencesForSuperKeyword(superKeyword: Node): SymbolAndEntries[] {
11861179
let searchSpaceNode = getSuperContainer(superKeyword, /*stopOnFunctions*/ false);
11871180
if (!searchSpaceNode) {
11881181
return undefined;
@@ -1208,10 +1201,8 @@ namespace ts.FindAllReferences.Core {
12081201
const references: Entry[] = [];
12091202

12101203
const sourceFile = searchSpaceNode.getSourceFile();
1211-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "super", searchSpaceNode.getStart(), searchSpaceNode.getEnd(), cancellationToken);
1204+
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "super", searchSpaceNode.getStart(), searchSpaceNode.getEnd());
12121205
for (const position of possiblePositions) {
1213-
cancellationToken.throwIfCancellationRequested();
1214-
12151206
const node = getTouchingWord(sourceFile, position);
12161207

12171208
if (!node || node.kind !== SyntaxKind.SuperKeyword) {
@@ -1271,13 +1262,14 @@ namespace ts.FindAllReferences.Core {
12711262
let possiblePositions: number[];
12721263
if (searchSpaceNode.kind === SyntaxKind.SourceFile) {
12731264
forEach(sourceFiles, sourceFile => {
1274-
possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this", sourceFile.getStart(), sourceFile.getEnd(), cancellationToken);
1265+
cancellationToken.throwIfCancellationRequested();
1266+
possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this", sourceFile.getStart(), sourceFile.getEnd());
12751267
getThisReferencesInFile(sourceFile, sourceFile, possiblePositions, references);
12761268
});
12771269
}
12781270
else {
12791271
const sourceFile = searchSpaceNode.getSourceFile();
1280-
possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this", searchSpaceNode.getStart(), searchSpaceNode.getEnd(), cancellationToken);
1272+
possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this", searchSpaceNode.getStart(), searchSpaceNode.getEnd());
12811273
getThisReferencesInFile(sourceFile, searchSpaceNode, possiblePositions, references);
12821274
}
12831275

@@ -1288,8 +1280,6 @@ namespace ts.FindAllReferences.Core {
12881280

12891281
function getThisReferencesInFile(sourceFile: SourceFile, searchSpaceNode: Node, possiblePositions: number[], result: Entry[]): void {
12901282
forEach(possiblePositions, position => {
1291-
cancellationToken.throwIfCancellationRequested();
1292-
12931283
const node = getTouchingWord(sourceFile, position);
12941284
if (!node || !isThis(node)) {
12951285
return;
@@ -1333,7 +1323,7 @@ namespace ts.FindAllReferences.Core {
13331323

13341324
for (const sourceFile of sourceFiles) {
13351325
cancellationToken.throwIfCancellationRequested();
1336-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, node.text, sourceFile.getStart(), sourceFile.getEnd(), cancellationToken);
1326+
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, node.text, sourceFile.getStart(), sourceFile.getEnd());
13371327
getReferencesForStringLiteralInFile(sourceFile, node.text, possiblePositions, references);
13381328
}
13391329

src/services/importTracker.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ namespace ts.FindAllReferences {
1212
export type ImportTracker = (exportSymbol: Symbol, exportInfo: ExportInfo, isForRename: boolean) => ImportsResult;
1313

1414
/** Creates the imports map and returns an ImportTracker that uses it. Call this lazily to avoid calling `getDirectImportsMap` unnecessarily. */
15-
export function createImportTracker(sourceFiles: SourceFile[], checker: TypeChecker): ImportTracker {
16-
const allDirectImports = getDirectImportsMap(sourceFiles, checker);
15+
export function createImportTracker(sourceFiles: SourceFile[], checker: TypeChecker, cancellationToken: CancellationToken): ImportTracker {
16+
const allDirectImports = getDirectImportsMap(sourceFiles, checker, cancellationToken);
1717
return (exportSymbol, exportInfo, isForRename) => {
18-
const { directImports, indirectUsers } = getImportersForExport(sourceFiles, allDirectImports, exportInfo, checker);
18+
const { directImports, indirectUsers } = getImportersForExport(sourceFiles, allDirectImports, exportInfo, checker, cancellationToken);
1919
return { indirectUsers, ...getSearchesFromDirectImports(directImports, exportSymbol, exportInfo.exportKind, checker, isForRename) };
2020
};
2121
}
@@ -37,7 +37,13 @@ namespace ts.FindAllReferences {
3737
type ImporterOrCallExpression = Importer | CallExpression;
3838

3939
/** Returns import statements that directly reference the exporting module, and a list of files that may access the module through a namespace. */
40-
function getImportersForExport(sourceFiles: SourceFile[], allDirectImports: Map<ImporterOrCallExpression[]>, { exportingModuleSymbol, exportKind }: ExportInfo, checker: TypeChecker): { directImports: Importer[], indirectUsers: SourceFile[] } {
40+
function getImportersForExport(
41+
sourceFiles: SourceFile[],
42+
allDirectImports: Map<ImporterOrCallExpression[]>,
43+
{ exportingModuleSymbol, exportKind }: ExportInfo,
44+
checker: TypeChecker,
45+
cancellationToken: CancellationToken
46+
): { directImports: Importer[], indirectUsers: SourceFile[] } {
4147
const markSeenDirectImport = nodeSeenTracker<ImporterOrCallExpression>();
4248
const markSeenIndirectUser = nodeSeenTracker<SourceFileLike>();
4349
const directImports: Importer[] = [];
@@ -72,6 +78,8 @@ namespace ts.FindAllReferences {
7278
continue;
7379
}
7480

81+
cancellationToken.throwIfCancellationRequested();
82+
7583
switch (direct.kind) {
7684
case SyntaxKind.CallExpression:
7785
if (!isAvailableThroughGlobal) {
@@ -289,10 +297,11 @@ namespace ts.FindAllReferences {
289297
}
290298

291299
/** Returns a map from a module symbol Id to all import statements that directly reference the module. */
292-
function getDirectImportsMap(sourceFiles: SourceFile[], checker: TypeChecker): Map<ImporterOrCallExpression[]> {
300+
function getDirectImportsMap(sourceFiles: SourceFile[], checker: TypeChecker, cancellationToken: CancellationToken): Map<ImporterOrCallExpression[]> {
293301
const map = createMap<ImporterOrCallExpression[]>();
294302

295303
for (const sourceFile of sourceFiles) {
304+
cancellationToken.throwIfCancellationRequested();
296305
forEachImport(sourceFile, (importDecl, moduleSpecifier) => {
297306
const moduleSymbol = checker.getSymbolAtLocation(moduleSpecifier);
298307
if (moduleSymbol) {

0 commit comments

Comments
 (0)