mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-10 21:07:52 -05:00
findAllReferences: Consistently use 'this' parameter as definition site (#26950)
This commit is contained in:
@@ -5,12 +5,13 @@ namespace ts.FindAllReferences {
|
||||
references: Entry[];
|
||||
}
|
||||
|
||||
export const enum DefinitionKind { Symbol, Label, Keyword, This, String }
|
||||
export type Definition =
|
||||
| { type: "symbol"; symbol: Symbol }
|
||||
| { type: "label"; node: Identifier }
|
||||
| { type: "keyword"; node: Node }
|
||||
| { type: "this"; node: Node }
|
||||
| { type: "string"; node: StringLiteral };
|
||||
| { readonly type: DefinitionKind.Symbol; readonly symbol: Symbol }
|
||||
| { readonly type: DefinitionKind.Label; readonly node: Identifier }
|
||||
| { readonly type: DefinitionKind.Keyword; readonly node: Node }
|
||||
| { readonly type: DefinitionKind.This; readonly node: Node }
|
||||
| { readonly type: DefinitionKind.String; readonly node: StringLiteral };
|
||||
|
||||
export type Entry = NodeEntry | SpanEntry;
|
||||
export interface NodeEntry {
|
||||
@@ -98,29 +99,29 @@ namespace ts.FindAllReferences {
|
||||
function definitionToReferencedSymbolDefinitionInfo(def: Definition, checker: TypeChecker, originalNode: Node): ReferencedSymbolDefinitionInfo {
|
||||
const info = (() => {
|
||||
switch (def.type) {
|
||||
case "symbol": {
|
||||
case DefinitionKind.Symbol: {
|
||||
const { symbol } = def;
|
||||
const { displayParts, kind } = getDefinitionKindAndDisplayParts(symbol, checker, originalNode);
|
||||
const name = displayParts.map(p => p.text).join("");
|
||||
return { node: symbol.declarations ? getNameOfDeclaration(first(symbol.declarations)) || first(symbol.declarations) : originalNode, name, kind, displayParts };
|
||||
}
|
||||
case "label": {
|
||||
case DefinitionKind.Label: {
|
||||
const { node } = def;
|
||||
return { node, name: node.text, kind: ScriptElementKind.label, displayParts: [displayPart(node.text, SymbolDisplayPartKind.text)] };
|
||||
}
|
||||
case "keyword": {
|
||||
case DefinitionKind.Keyword: {
|
||||
const { node } = def;
|
||||
const name = tokenToString(node.kind)!;
|
||||
return { node, name, kind: ScriptElementKind.keyword, displayParts: [{ text: name, kind: ScriptElementKind.keyword }] };
|
||||
}
|
||||
case "this": {
|
||||
case DefinitionKind.This: {
|
||||
const { node } = def;
|
||||
const symbol = checker.getSymbolAtLocation(node);
|
||||
const displayParts = symbol && SymbolDisplay.getSymbolDisplayPartsDocumentationAndSymbolKind(
|
||||
checker, symbol, node.getSourceFile(), getContainerNode(node), node).displayParts || [textPart("this")];
|
||||
return { node, name: "this", kind: ScriptElementKind.variableElement, displayParts };
|
||||
}
|
||||
case "string": {
|
||||
case DefinitionKind.String: {
|
||||
const { node } = def;
|
||||
return { node, name: node.text, kind: ScriptElementKind.variableElement, displayParts: [displayPart(getTextOfNode(node), SymbolDisplayPartKind.stringLiteral)] };
|
||||
}
|
||||
@@ -374,7 +375,7 @@ namespace ts.FindAllReferences.Core {
|
||||
}
|
||||
}
|
||||
|
||||
return references.length ? [{ definition: { type: "symbol", symbol }, references }] : emptyArray;
|
||||
return references.length ? [{ definition: { type: DefinitionKind.Symbol, symbol }, references }] : emptyArray;
|
||||
}
|
||||
|
||||
/** getReferencedSymbols for special node kinds. */
|
||||
@@ -585,7 +586,7 @@ namespace ts.FindAllReferences.Core {
|
||||
let references = this.symbolIdToReferences[symbolId];
|
||||
if (!references) {
|
||||
references = this.symbolIdToReferences[symbolId] = [];
|
||||
this.result.push({ definition: { type: "symbol", symbol: searchSymbol }, references });
|
||||
this.result.push({ definition: { type: DefinitionKind.Symbol, symbol: searchSymbol }, references });
|
||||
}
|
||||
return node => references.push(nodeEntry(node));
|
||||
}
|
||||
@@ -879,7 +880,7 @@ namespace ts.FindAllReferences.Core {
|
||||
const references = mapDefined(getPossibleSymbolReferenceNodes(sourceFile, labelName, container), node =>
|
||||
// Only pick labels that are either the target label, or have a target that is the target label
|
||||
node === targetLabel || (isJumpStatementTarget(node) && getTargetLabel(node, labelName) === targetLabel) ? nodeEntry(node) : undefined);
|
||||
return [{ definition: { type: "label", node: targetLabel }, references }];
|
||||
return [{ definition: { type: DefinitionKind.Label, node: targetLabel }, references }];
|
||||
}
|
||||
|
||||
function isValidReferencePosition(node: Node, searchSymbolName: string): boolean {
|
||||
@@ -911,7 +912,7 @@ namespace ts.FindAllReferences.Core {
|
||||
return mapDefined(getPossibleSymbolReferenceNodes(sourceFile, tokenToString(keywordKind)!, sourceFile), referenceLocation =>
|
||||
referenceLocation.kind === keywordKind ? nodeEntry(referenceLocation) : undefined);
|
||||
});
|
||||
return references.length ? [{ definition: { type: "keyword", node: references[0].node }, references }] : undefined;
|
||||
return references.length ? [{ definition: { type: DefinitionKind.Keyword, node: references[0].node }, references }] : undefined;
|
||||
}
|
||||
|
||||
function getReferencesInSourceFile(sourceFile: SourceFile, search: Search, state: State, addReferencesHere = true): void {
|
||||
@@ -1353,7 +1354,7 @@ namespace ts.FindAllReferences.Core {
|
||||
return container && (ModifierFlags.Static & getModifierFlags(container)) === staticFlag && container.parent.symbol === searchSpaceNode.symbol ? nodeEntry(node) : undefined;
|
||||
});
|
||||
|
||||
return [{ definition: { type: "symbol", symbol: searchSpaceNode.symbol }, references }];
|
||||
return [{ definition: { type: DefinitionKind.Symbol, symbol: searchSpaceNode.symbol }, references }];
|
||||
}
|
||||
|
||||
function getReferencesForThisKeyword(thisOrSuperKeyword: Node, sourceFiles: ReadonlyArray<SourceFile>, cancellationToken: CancellationToken): SymbolAndEntries[] | undefined {
|
||||
@@ -1416,8 +1417,9 @@ namespace ts.FindAllReferences.Core {
|
||||
});
|
||||
}).map(n => nodeEntry(n));
|
||||
|
||||
const thisParameter = firstDefined(references, r => isParameter(r.node.parent) ? r.node : undefined);
|
||||
return [{
|
||||
definition: { type: "this", node: thisOrSuperKeyword },
|
||||
definition: { type: DefinitionKind.This, node: thisParameter || thisOrSuperKeyword },
|
||||
references
|
||||
}];
|
||||
}
|
||||
@@ -1430,7 +1432,7 @@ namespace ts.FindAllReferences.Core {
|
||||
});
|
||||
|
||||
return [{
|
||||
definition: { type: "string", node },
|
||||
definition: { type: DefinitionKind.String, node },
|
||||
references
|
||||
}];
|
||||
}
|
||||
|
||||
@@ -26,10 +26,8 @@
|
||||
|
||||
const [global, f0, f1, g0, g1, x, y, constructor, method, propDef, propUse] = test.ranges();
|
||||
verify.singleReferenceGroup("this", [global]);
|
||||
verify.referenceGroups(f0, [{ definition: "(parameter) this: any", ranges: [f0, f1] }]);
|
||||
verify.referenceGroups(f1, [{ definition: "this: any", ranges: [f0, f1] }]);
|
||||
verify.referenceGroups(g0, [{ definition: "(parameter) this: any", ranges: [g0, g1] }]);
|
||||
verify.referenceGroups(g1, [{ definition: "this: any", ranges: [g0, g1] }]);
|
||||
verify.singleReferenceGroup("(parameter) this: any", [f0, f1]);
|
||||
verify.singleReferenceGroup("(parameter) this: any", [g0, g1]);
|
||||
verify.singleReferenceGroup("this: typeof C", [x, y]);
|
||||
verify.singleReferenceGroup("this: this", [constructor, method]);
|
||||
verify.singleReferenceGroup("(property) this: number", [propDef, propUse]);
|
||||
|
||||
Reference in New Issue
Block a user