Skip to content

fix(57026): Renaming on declaration whose name is a String Literal #61353

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
6 changes: 2 additions & 4 deletions src/services/findAllReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,9 +1013,8 @@ export namespace Core {
const checker = program.getTypeChecker();
// constructors should use the class symbol, detected by name, if present
const symbol = checker.getSymbolAtLocation(isConstructorDeclaration(node) && node.parent.name || node);

// Could not find a symbol e.g. unknown identifier
if (!symbol) {
if (!symbol || (isStringLiteralLike(node) && isStringLiteralPropertyReference(node, checker))) {
// String literal might be a property (and thus have a symbol), so do this here rather than in getReferencedSymbolsSpecial.
if (!options.implementations && isStringLiteralLike(node)) {
if (isModuleSpecifierLike(node)) {
Expand Down Expand Up @@ -1389,7 +1388,6 @@ export namespace Core {
/** Only set if `options.implementations` is true. These are the symbols checked to get the implementations of a property access. */
readonly parents: readonly Symbol[] | undefined;
readonly allSearchSymbols: readonly Symbol[];

/**
* Whether a symbol is in the search set.
* Do not compare directly to `symbol` because there may be related symbols to search for. See `populateSearchSymbolSet`.
Expand Down Expand Up @@ -2480,7 +2478,7 @@ export namespace Core {
cancellationToken.throwIfCancellationRequested();
return mapDefined(getPossibleSymbolReferenceNodes(sourceFile, node.text), ref => {
if (isStringLiteralLike(ref) && ref.text === node.text) {
if (type) {
if (type && !isStringLiteralPropertyReference(node, checker)) {
const refType = getContextualTypeFromParentOrAncestorTypeNode(ref, checker);
if (type !== checker.getStringType() && (type === refType || isStringLiteralPropertyReference(ref, checker))) {
return nodeEntry(ref, EntryKind.StringLiteral);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// === findRenameLocations ===
// === /tests/cases/fourslash/renameStringLiteralTypes6.ts ===
// interface Case_1 {
// <|"[|Att 2RENAME|]"/*RENAME*/: string|>
// }
//
// declare const fnc_1: <K extends keyof Case_1>(p: K) => void
//
// fnc_1("[|Att 2RENAME|]")
11 changes: 11 additions & 0 deletions tests/baselines/reference/renameStringLiteralTypes7.baseline.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// === findRenameLocations ===
// === /tests/cases/fourslash/renameStringLiteralTypes7.ts ===
// declare const Att_1: "[|Att 2RENAME|]"/*RENAME*/
//
// interface Case_1 {
// [Att_1]: string
// }
//
// declare const fnc_1: <K extends keyof Case_1>(p: K) => void
//
// fnc_1("[|Att 2RENAME|]")
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// === findRenameLocations ===
// === /tests/cases/fourslash/renameStringLiteralTypes8.ts ===
// type T = {
// <|"[|Prop 1RENAME|]"/*RENAME*/: string;|>
// }
//
// declare const fn: <K extends keyof T>(p: K) => void
//
// fn("[|Prop 1RENAME|]")
4 changes: 1 addition & 3 deletions tests/cases/fourslash/renameStringLiteralTypes5.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/// <reference path="fourslash.ts" />

////type T = {
//// "Prop 1": string;
////}
////
////declare const fn: <K extends keyof T>(p: K) => void
////
////fn("Prop 1"/**/)

verify.baselineRename("", {});
verify.baselineRename("", {});
12 changes: 12 additions & 0 deletions tests/cases/fourslash/renameStringLiteralTypes6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path="fourslash.ts" />


////interface Case_1 {
//// "Att 2"/**/: string
////}
////
////declare const fnc_1: <K extends keyof Case_1>(p: K) => void
////
////fnc_1("Att 2")

verify.baselineRename("", {});
13 changes: 13 additions & 0 deletions tests/cases/fourslash/renameStringLiteralTypes7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

////declare const Att_1: "Att 2"/**/
////
////interface Case_1 {
//// [Att_1]: string
////}
////
////declare const fnc_1: <K extends keyof Case_1>(p: K) => void
////
////fnc_1("Att 2")

verify.baselineRename("", {});
9 changes: 9 additions & 0 deletions tests/cases/fourslash/renameStringLiteralTypes8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path="fourslash.ts" />
////type T = {
//// "Prop 1"/**/: string;
////}
////
////declare const fn: <K extends keyof T>(p: K) => void
////
////fn("Prop 1")
verify.baselineRename("", {});