From bb2c58b9775e250cb934894ee7d56af57feccdb7 Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 22 Feb 2018 13:22:52 -0800 Subject: [PATCH] Simplify uses of getPossibleSymbolReferencePositions (#22099) --- src/services/findAllReferences.ts | 83 +++++++++---------------------- 1 file changed, 23 insertions(+), 60 deletions(-) diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index 46a17707e79..41bc5685f80 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -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, 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): 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; - 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, 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, references: Push): 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.