Simplify uses of getPossibleSymbolReferencePositions (#22099)

This commit is contained in:
Andy 2018-02-22 13:22:52 -08:00 committed by GitHub
parent 790f65d15b
commit bb2c58b977
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -743,18 +743,13 @@ namespace ts.FindAllReferences.Core {
}
function getLabelReferencesInNode(container: Node, targetLabel: Identifier): SymbolAndEntries[] {
const references: Entry[] = [];
const sourceFile = container.getSourceFile();
const labelName = targetLabel.text;
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, labelName, container);
for (const position of possiblePositions) {
const references = mapDefined(getPossibleSymbolReferencePositions(sourceFile, labelName, container), position => {
const node = getTouchingWord(sourceFile, position, /*includeJsDocComment*/ false);
// Only pick labels that are either the target label, or have a target that is the target label
if (node && (node === targetLabel || (isJumpStatementTarget(node) && getTargetLabel(node, labelName) === targetLabel))) {
references.push(nodeEntry(node));
}
}
return node && (node === targetLabel || (isJumpStatementTarget(node) && getTargetLabel(node, labelName) === targetLabel)) ? nodeEntry(node) : undefined;
});
return [{ definition: { type: "label", node: targetLabel }, references }];
}
@ -780,24 +775,16 @@ namespace ts.FindAllReferences.Core {
}
function getAllReferencesForKeyword(sourceFiles: ReadonlyArray<SourceFile>, keywordKind: ts.SyntaxKind, cancellationToken: CancellationToken): SymbolAndEntries[] {
const references: NodeEntry[] = [];
for (const sourceFile of sourceFiles) {
const references = flatMap(sourceFiles, sourceFile => {
cancellationToken.throwIfCancellationRequested();
addReferencesForKeywordInFile(sourceFile, keywordKind, tokenToString(keywordKind), references);
}
return mapDefined(getPossibleSymbolReferencePositions(sourceFile, tokenToString(keywordKind), sourceFile), position => {
const referenceLocation = getTouchingPropertyName(sourceFile, position, /*includeJsDocComment*/ true);
return referenceLocation.kind === keywordKind ? nodeEntry(referenceLocation) : undefined;
});
});
return references.length ? [{ definition: { type: "keyword", node: references[0].node }, references }] : undefined;
}
function addReferencesForKeywordInFile(sourceFile: SourceFile, kind: SyntaxKind, searchText: string, references: Push<NodeEntry>): void {
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, searchText, sourceFile);
for (const position of possiblePositions) {
const referenceLocation = getTouchingPropertyName(sourceFile, position, /*includeJsDocComment*/ true);
if (referenceLocation.kind === kind) {
references.push(nodeEntry(referenceLocation));
}
}
}
function getReferencesInSourceFile(sourceFile: ts.SourceFile, search: Search, state: State): void {
state.cancellationToken.throwIfCancellationRequested();
return getReferencesInContainer(sourceFile, sourceFile, search, state);
@ -1288,15 +1275,11 @@ namespace ts.FindAllReferences.Core {
return undefined;
}
const references: Entry[] = [];
const sourceFile = searchSpaceNode.getSourceFile();
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "super", searchSpaceNode);
for (const position of possiblePositions) {
const references = mapDefined(getPossibleSymbolReferencePositions(sourceFile, "super", searchSpaceNode), position => {
const node = getTouchingWord(sourceFile, position, /*includeJsDocComment*/ false);
if (!node || node.kind !== SyntaxKind.SuperKeyword) {
continue;
return;
}
const container = getSuperContainer(node, /*stopOnFunctions*/ false);
@ -1304,10 +1287,8 @@ namespace ts.FindAllReferences.Core {
// If we have a 'super' container, we must have an enclosing class.
// Now make sure the owning class is the same as the search-space
// and has the same static qualifier as the original 'super's owner.
if (container && (ModifierFlags.Static & getModifierFlags(container)) === staticFlag && container.parent.symbol === searchSpaceNode.symbol) {
references.push(nodeEntry(node));
}
}
return container && (ModifierFlags.Static & getModifierFlags(container)) === staticFlag && container.parent.symbol === searchSpaceNode.symbol ? nodeEntry(node) : undefined;
});
return [{ definition: { type: "symbol", symbol: searchSpaceNode.symbol, node: superKeyword }, references }];
}
@ -1348,19 +1329,10 @@ namespace ts.FindAllReferences.Core {
}
const references: Entry[] = [];
let possiblePositions: ReadonlyArray<number>;
if (searchSpaceNode.kind === SyntaxKind.SourceFile) {
forEach(sourceFiles, sourceFile => {
cancellationToken.throwIfCancellationRequested();
possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this");
getThisReferencesInFile(sourceFile, sourceFile, possiblePositions, staticFlag, references);
});
}
else {
const sourceFile = searchSpaceNode.getSourceFile();
possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this", searchSpaceNode);
getThisReferencesInFile(sourceFile, searchSpaceNode, possiblePositions, staticFlag, references);
for (const sourceFile of searchSpaceNode.kind === SyntaxKind.SourceFile ? sourceFiles : [searchSpaceNode.getSourceFile()]) {
cancellationToken.throwIfCancellationRequested();
const positions = getPossibleSymbolReferencePositions(sourceFile, "this", isSourceFile(searchSpaceNode) ? sourceFile : searchSpaceNode);
getThisReferencesInFile(sourceFile, searchSpaceNode.kind === SyntaxKind.SourceFile ? sourceFile : searchSpaceNode, positions, staticFlag, references);
}
return [{
@ -1409,27 +1381,18 @@ namespace ts.FindAllReferences.Core {
}
function getReferencesForStringLiteral(node: StringLiteral, sourceFiles: ReadonlyArray<SourceFile>, cancellationToken: CancellationToken): SymbolAndEntries[] {
const references: NodeEntry[] = [];
for (const sourceFile of sourceFiles) {
const references = flatMap(sourceFiles, sourceFile => {
cancellationToken.throwIfCancellationRequested();
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, node.text);
getReferencesForStringLiteralInFile(sourceFile, node.text, possiblePositions, references);
}
return mapDefined(getPossibleSymbolReferencePositions(sourceFile, node.text), position => {
const ref = tryCast(getTouchingWord(sourceFile, position, /*includeJsDocComment*/ false), isStringLiteral);
return ref && ref.text === node.text ? nodeEntry(ref, /*isInString*/ true) : undefined;
});
});
return [{
definition: { type: "string", node },
references
}];
function getReferencesForStringLiteralInFile(sourceFile: SourceFile, searchText: string, possiblePositions: ReadonlyArray<number>, references: Push<NodeEntry>): void {
for (const position of possiblePositions) {
const node = getTouchingWord(sourceFile, position, /*includeJsDocComment*/ false);
if (node && node.kind === SyntaxKind.StringLiteral && (node as StringLiteral).text === searchText) {
references.push(nodeEntry(node, /*isInString*/ true));
}
}
}
}
// For certain symbol kinds, we need to include other symbols in the search set.