-
Notifications
You must be signed in to change notification settings - Fork 12.8k
feat(45549): track string literal references within call expressions #45791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1480,6 +1480,25 @@ namespace ts.FindAllReferences { | |||||
state.addStringOrCommentReference(sourceFile.fileName, createTextSpan(position, search.text.length)); | ||||||
} | ||||||
|
||||||
const callExpr = findAncestor(referenceLocation, isCallExpression); | ||||||
if (callExpr && isStringLiteralLike(referenceLocation)) { | ||||||
// In the case where the reference node is a string literal within a | ||||||
// call expression, the current symbol we're searching for might be | ||||||
// a reference to a property name for one of the parameters of the | ||||||
// call's resolved signature. | ||||||
const sig = state.checker.getResolvedSignature(callExpr); | ||||||
if (sig) { | ||||||
const params = sig.getParameters(); | ||||||
for (let i = 0; i < params.length; i++) { | ||||||
const paramType = state.checker.getParameterType(sig, i); | ||||||
const prop = paramType.getProperty(referenceLocation.text); | ||||||
if (prop) { | ||||||
const relatedSymbol = getRelatedSymbol(search, prop, referenceLocation, state); | ||||||
if (relatedSymbol) addReference(referenceLocation, relatedSymbol, state); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
return; | ||||||
} | ||||||
|
||||||
|
@@ -2004,20 +2023,51 @@ namespace ts.FindAllReferences { | |||||
|
||||||
function getReferencesForStringLiteral(node: StringLiteralLike, sourceFiles: readonly SourceFile[], checker: TypeChecker, cancellationToken: CancellationToken): SymbolAndEntries[] { | ||||||
const type = getContextualTypeFromParentOrAncestorTypeNode(node, checker); | ||||||
const callExpr = findAncestor(node, isCallExpression); | ||||||
const references = flatMap(sourceFiles, sourceFile => { | ||||||
cancellationToken.throwIfCancellationRequested(); | ||||||
return mapDefined(getPossibleSymbolReferenceNodes(sourceFile, node.text), ref => { | ||||||
if (isStringLiteralLike(ref) && ref.text === node.text) { | ||||||
if (type) { | ||||||
const refType = getContextualTypeFromParentOrAncestorTypeNode(ref, checker); | ||||||
if (type !== checker.getStringType() && type === refType) { | ||||||
return nodeEntry(ref, EntryKind.StringLiteral); | ||||||
} | ||||||
} | ||||||
else { | ||||||
// Special case: Every string literal refers at least to itself. | ||||||
if (node === ref) return nodeEntry(ref, EntryKind.StringLiteral); | ||||||
|
||||||
// When evaluating references for a string literal, guarantee that the string literal | ||||||
// node is either global or that any reference shares the same scope. Global literals | ||||||
// might all refer to each other, but if the string literal being considered is in a | ||||||
// call expression, any reference should also be from the same call expression. | ||||||
const refHasSameScope = !callExpr || callExpr === findAncestor(ref, isCallExpression); | ||||||
const refHasSameText = isStringLiteralLike(ref) || isPropertyNameLiteral(ref) && getTextOfIdentifierOrLiteral(ref) === node.text; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently all string literal
Suggested change
|
||||||
|
||||||
if (type && isStringLiteralLike(ref) && refHasSameText) { | ||||||
const refType = getContextualTypeFromParentOrAncestorTypeNode(ref, checker); | ||||||
if ((refHasSameScope || !type.isStringLiteral() || type.isUnionOrIntersection()) && type === refType) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
// Reference globally or contextually matches the given string literal by referencing | ||||||
// the same union or intersection type or sharing a similar scope. | ||||||
return nodeEntry(ref, EntryKind.StringLiteral); | ||||||
} | ||||||
} | ||||||
if (callExpr) { | ||||||
if (refHasSameScope && isPropertyNameLiteral(ref) && refHasSameText) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A nested eg f(a => { a: a }, "a") |
||||||
// Reference is the property name of an object literal argument. | ||||||
return nodeEntry(ref, EntryKind.SearchedLocalFoundProperty); | ||||||
} | ||||||
|
||||||
const sig = checker.getResolvedSignature(callExpr); | ||||||
if (sig) { | ||||||
const params = sig.getParameters(); | ||||||
for (let i = 0; i < params.length; i++) { | ||||||
const paramType = checker.getParameterType(sig, i); | ||||||
if (isLiteralTypeNode(ref.parent) && paramType.isStringLiteral() && refHasSameText) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is it |
||||||
// Reference is the definition of a string literal parameter. | ||||||
return nodeEntry(ref, EntryKind.StringLiteral); | ||||||
} | ||||||
const prop = paramType.getProperty(node.text); | ||||||
if (prop && prop === checker.getSymbolAtLocation(ref)) { | ||||||
// Reference is a property name in one of the parameter types. | ||||||
return nodeEntry(ref, EntryKind.SearchedLocalFoundProperty); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
}); | ||||||
}); | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////interface A { | ||
//// [|[|{| "contextRangeIndex": 0 |}prop|]: string;|] | ||
////} | ||
//// | ||
////declare function f(a: A, k: keyof A): void; | ||
////declare let a: A; | ||
////f(a, "[|prop|]"); | ||
//// | ||
////declare const f2: <T>(a: T, b: keyof T) => void; | ||
////f2({ | ||
//// [|[|{| "contextRangeIndex": 3 |}prop|]: () => {}|] | ||
////}, "[|prop|]"); | ||
//// | ||
////declare const f3: <K extends string>(a: K, b: { [_ in K]: unknown }) => void; | ||
////f3("[|prop|]", { | ||
//// [|[|{| "contextRangeIndex": 7 |}prop|]: () => {}|] | ||
////}); | ||
|
||
const [r0Def, r0, r1, r2Def, r2, r3, r4, r5Def, r5] = test.ranges(); | ||
verify.renameLocations([r0, r1], [r0, r1]); | ||
verify.renameLocations([r2, r3], [r2, r3]); | ||
verify.renameLocations([r4, r5], [r4, r5]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this happen? I'd expect
getPossibleSymbolReferenceNodes
should filternode
out of the refs list.It's fine if it doesn't, but maybe we should consider making it do that.