Skip to content

Commit 7a64de3

Browse files
author
Andy
authored
Merge pull request #15772 from Microsoft/getWidth
findAllReferences: Clean up uses of `getWidth` and `getStart`
2 parents 83beae7 + 6d6cdac commit 7a64de3

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

src/compiler/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,11 @@ namespace ts {
571571

572572
export interface Identifier extends PrimaryExpression {
573573
kind: SyntaxKind.Identifier;
574-
text: string; // Text of identifier (with escapes converted to characters)
574+
/**
575+
* Text of identifier (with escapes converted to characters).
576+
* If the identifier begins with two underscores, this will begin with three.
577+
*/
578+
text: string;
575579
originalKeywordKind?: SyntaxKind; // Original syntaxKind which get set so that we can report an error later
576580
/*@internal*/ autoGenerateKind?: GeneratedIdentifierKind; // Specifies whether to auto-generate the text for an identifier.
577581
/*@internal*/ autoGenerateId?: number; // Ensures unique generated identifiers get unique names, but clones get the same name.

src/services/findAllReferences.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,9 @@ namespace ts.FindAllReferences.Core {
637637
return parent ? scope.getSourceFile() : scope;
638638
}
639639

640-
function getPossibleSymbolReferencePositions(sourceFile: SourceFile, symbolName: string, start: number, end: number): number[] {
640+
function getPossibleSymbolReferencePositions(sourceFile: SourceFile, symbolName: string, container: Node = sourceFile, fullStart = false): number[] {
641+
const start = fullStart ? container.getFullStart() : container.getStart(sourceFile);
642+
const end = container.getEnd();
641643
const positions: number[] = [];
642644

643645
/// TODO: Cache symbol existence for files to save text search
@@ -676,16 +678,11 @@ namespace ts.FindAllReferences.Core {
676678
const references: Entry[] = [];
677679
const sourceFile = container.getSourceFile();
678680
const labelName = targetLabel.text;
679-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, labelName, container.getStart(), container.getEnd());
681+
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, labelName, container);
680682
for (const position of possiblePositions) {
681683
const node = getTouchingWord(sourceFile, position);
682-
if (!node || node.getWidth() !== labelName.length) {
683-
continue;
684-
}
685-
686684
// Only pick labels that are either the target label, or have a target that is the target label
687-
if (node === targetLabel ||
688-
(isJumpStatementTarget(node) && getTargetLabel(node, labelName) === targetLabel)) {
685+
if (node && (node === targetLabel || (isJumpStatementTarget(node) && getTargetLabel(node, labelName) === targetLabel))) {
689686
references.push(nodeEntry(node));
690687
}
691688
}
@@ -697,15 +694,14 @@ namespace ts.FindAllReferences.Core {
697694
// Compare the length so we filter out strict superstrings of the symbol we are looking for
698695
switch (node && node.kind) {
699696
case SyntaxKind.Identifier:
700-
return node.getWidth() === searchSymbolName.length;
697+
return unescapeIdentifier((node as Identifier).text).length === searchSymbolName.length;
701698

702699
case SyntaxKind.StringLiteral:
703700
return (isLiteralNameOfPropertyDeclarationOrIndexAccess(node) || isNameOfExternalModuleImportOrDeclaration(node)) &&
704-
// For string literals we have two additional chars for the quotes
705-
node.getWidth() === searchSymbolName.length + 2;
701+
(node as StringLiteral).text.length === searchSymbolName.length;
706702

707703
case SyntaxKind.NumericLiteral:
708-
return isLiteralNameOfPropertyDeclarationOrIndexAccess(node) && node.getWidth() === searchSymbolName.length;
704+
return isLiteralNameOfPropertyDeclarationOrIndexAccess(node) && (node as NumericLiteral).text.length === searchSymbolName.length;
709705

710706
default:
711707
return false;
@@ -722,7 +718,7 @@ namespace ts.FindAllReferences.Core {
722718
}
723719

724720
function addReferencesForKeywordInFile(sourceFile: SourceFile, kind: SyntaxKind, searchText: string, references: Push<NodeEntry>): void {
725-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, searchText, sourceFile.getStart(), sourceFile.getEnd());
721+
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, searchText);
726722
for (const position of possiblePositions) {
727723
const referenceLocation = getTouchingPropertyName(sourceFile, position);
728724
if (referenceLocation.kind === kind) {
@@ -746,9 +742,7 @@ namespace ts.FindAllReferences.Core {
746742
return;
747743
}
748744

749-
const start = state.findInComments ? container.getFullStart() : container.getStart();
750-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, search.text, start, container.getEnd());
751-
for (const position of possiblePositions) {
745+
for (const position of getPossibleSymbolReferencePositions(sourceFile, search.text, container, /*fullStart*/ state.findInComments)) {
752746
getReferencesAtLocation(sourceFile, position, search, state);
753747
}
754748
}
@@ -1192,7 +1186,7 @@ namespace ts.FindAllReferences.Core {
11921186
const references: Entry[] = [];
11931187

11941188
const sourceFile = searchSpaceNode.getSourceFile();
1195-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "super", searchSpaceNode.getStart(), searchSpaceNode.getEnd());
1189+
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "super", searchSpaceNode);
11961190
for (const position of possiblePositions) {
11971191
const node = getTouchingWord(sourceFile, position);
11981192

@@ -1254,13 +1248,13 @@ namespace ts.FindAllReferences.Core {
12541248
if (searchSpaceNode.kind === SyntaxKind.SourceFile) {
12551249
forEach(sourceFiles, sourceFile => {
12561250
cancellationToken.throwIfCancellationRequested();
1257-
possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this", sourceFile.getStart(), sourceFile.getEnd());
1251+
possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this");
12581252
getThisReferencesInFile(sourceFile, sourceFile, possiblePositions, references);
12591253
});
12601254
}
12611255
else {
12621256
const sourceFile = searchSpaceNode.getSourceFile();
1263-
possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this", searchSpaceNode.getStart(), searchSpaceNode.getEnd());
1257+
possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this", searchSpaceNode);
12641258
getThisReferencesInFile(sourceFile, searchSpaceNode, possiblePositions, references);
12651259
}
12661260

@@ -1314,7 +1308,7 @@ namespace ts.FindAllReferences.Core {
13141308

13151309
for (const sourceFile of sourceFiles) {
13161310
cancellationToken.throwIfCancellationRequested();
1317-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, node.text, sourceFile.getStart(), sourceFile.getEnd());
1311+
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, node.text);
13181312
getReferencesForStringLiteralInFile(sourceFile, node.text, possiblePositions, references);
13191313
}
13201314

0 commit comments

Comments
 (0)