From efde942d4cab898e18977dae2107f42e43f47bc3 Mon Sep 17 00:00:00 2001 From: Josue Barros Date: Mon, 16 Nov 2026 15:53:35 -0300 Subject: [PATCH 1/2] Use the symbol name for the rename placeholder getRenameInfoForNode passed SymbolToString to the rename placeholder. For a property declared with a quoted or computed name, SymbolToString prints the declaration form, so prepareRename prefilled 'b' or ['c'] instead of b or c. The classic implementation uses the symbol name, which is already unquoted. The trigger range was already correct and is unchanged. Adds an LSP test for obj.a, obj['a'], obj.b and obj.c. --- tsc/internal/ls/rename.go | 2 +- tsc/internal/lsp/server_rename_test.go | 76 ++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 tsc/internal/lsp/server_rename_test.go diff --git a/tsc/internal/ls/rename.go b/tsc/internal/ls/rename.go index 3415142ae6545..d4b9d2f3cee29 100644 --- a/tsc/internal/ls/rename.go +++ b/tsc/internal/ls/rename.go @@ -203,7 +203,7 @@ func (l *LanguageService) getRenameInfoForNode(ctx context.Context, newName stri return RenameInfo{}, false } - return getRenameInfoSuccess(node, sourceFile, ch.SymbolToString(symbol), l.converters), true + return getRenameInfoSuccess(node, sourceFile, symbol.Name(), l.converters), true } func nodeIsEligibleForRename(node *ast.Node) bool { diff --git a/tsc/internal/lsp/server_rename_test.go b/tsc/internal/lsp/server_rename_test.go new file mode 100644 index 0000000000000..ef404233c767b --- /dev/null +++ b/tsc/internal/lsp/server_rename_test.go @@ -0,0 +1,76 @@ +package lsp_test + +import ( + "context" + "io" + "testing" + + "github.com/microsoft/TypeScript/tsc/internal/bundled" + "github.com/microsoft/TypeScript/tsc/internal/lsp" + "github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto" + "github.com/microsoft/TypeScript/tsc/internal/testutil/lsptestutil" + "github.com/microsoft/TypeScript/tsc/internal/tspath" + "github.com/microsoft/TypeScript/tsc/internal/vfs/vfstest" + "gotest.tools/v3/assert" +) + +// TestPrepareRenamePlaceholderForQuotedProperties reproduces #65444: the +// placeholder used the printed declaration name, including quotes and brackets. +func TestPrepareRenamePlaceholderForQuotedProperties(t *testing.T) { + t.Parallel() + if !bundled.Embedded { + t.Skip("bundled files are not embedded") + } + + const text = "const obj = {\n a: 1,\n 'b': 1,\n ['c']: 1,\n};\n\nobj.a;\nobj['a'];\nobj.b;\nobj.c;\n" + files := map[string]string{ + "/home/projects/tsconfig.json": `{}`, + "/home/projects/repro.ts": text, + } + fs := bundled.WrapFS(vfstest.FromMap(files, tspath.CaseInsensitive)) + + onServerRequest := func(_ context.Context, req *lsproto.RequestMessage) *lsproto.ResponseMessage { + if req.Method == lsproto.MethodClientRegisterCapability || req.Method == lsproto.MethodClientUnregisterCapability { + return &lsproto.ResponseMessage{ID: req.ID, JSONRPC: req.JSONRPC, Result: lsproto.Null{}} + } + return nil + } + + client, closeClient := lsptestutil.NewLSPClient(t, lsp.ServerOptions{ + Err: io.Discard, + Cwd: "/home/projects", + FS: fs, + DefaultLibraryPath: bundled.LibPath(), + }, onServerRequest) + t.Cleanup(func() { _ = closeClient() }) + + initMsg, _, ok := client.SendRequest(t, lsproto.InitializeInfo, &lsproto.InitializeParams{ + Capabilities: &lsproto.ClientCapabilities{}, + }) + assert.Assert(t, ok && initMsg.AsResponse().Error == nil, "Initialize failed") + client.SendNotification(t, lsproto.InitializedInfo, &lsproto.InitializedParams{}) + <-client.Server.InitComplete() + + uri := lsproto.DocumentUri("file:///home/projects/repro.ts") + client.SendNotification(t, lsproto.TextDocumentDidOpenInfo, &lsproto.DidOpenTextDocumentParams{ + TextDocument: &lsproto.TextDocumentItem{Uri: uri, LanguageId: "typescript", Text: text}, + }) + + for _, tc := range []struct { + position lsproto.Position + placeholder string + }{ + {lsproto.Position{Line: 6, Character: 4}, "a"}, + {lsproto.Position{Line: 7, Character: 5}, "a"}, + {lsproto.Position{Line: 8, Character: 4}, "b"}, + {lsproto.Position{Line: 9, Character: 4}, "c"}, + } { + msg, res, ok := client.SendRequest(t, lsproto.TextDocumentPrepareRenameInfo, &lsproto.PrepareRenameParams{ + TextDocument: lsproto.TextDocumentIdentifier{Uri: uri}, + Position: tc.position, + }) + assert.Assert(t, ok && msg.AsResponse().Error == nil) + assert.Assert(t, res.PrepareRenamePlaceholder != nil) + assert.Equal(t, res.PrepareRenamePlaceholder.Placeholder, tc.placeholder) + } +} From 711ced4f9cd0445ec7784adc10ba7bc089781b84 Mon Sep 17 00:00:00 2001 From: Josue Barros Date: Mon, 16 Nov 2026 17:20:18 -0300 Subject: [PATCH 2/2] Keep the private identifier in the rename placeholder symbol.Name() returns the class-specific internal key for a private identifier, so preparing a rename on #x returned that key instead of #x. Use SymbolToString for private identifiers and keep the symbol name for the quoted and computed property fix. --- tsc/internal/ls/rename.go | 7 +++- tsc/internal/lsp/server_rename_test.go | 49 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/tsc/internal/ls/rename.go b/tsc/internal/ls/rename.go index d4b9d2f3cee29..25fd2c5c6601e 100644 --- a/tsc/internal/ls/rename.go +++ b/tsc/internal/ls/rename.go @@ -203,7 +203,12 @@ func (l *LanguageService) getRenameInfoForNode(ctx context.Context, newName stri return RenameInfo{}, false } - return getRenameInfoSuccess(node, sourceFile, symbol.Name(), l.converters), true + displayName := symbol.Name() + if ast.IsPrivateIdentifier(node) { + // symbol.Name() is the internal key for private identifiers. + displayName = ch.SymbolToString(symbol) + } + return getRenameInfoSuccess(node, sourceFile, displayName, l.converters), true } func nodeIsEligibleForRename(node *ast.Node) bool { diff --git a/tsc/internal/lsp/server_rename_test.go b/tsc/internal/lsp/server_rename_test.go index ef404233c767b..94e36e15d5280 100644 --- a/tsc/internal/lsp/server_rename_test.go +++ b/tsc/internal/lsp/server_rename_test.go @@ -74,3 +74,52 @@ func TestPrepareRenamePlaceholderForQuotedProperties(t *testing.T) { assert.Equal(t, res.PrepareRenamePlaceholder.Placeholder, tc.placeholder) } } + +func TestPrepareRenamePlaceholderForPrivateIdentifier(t *testing.T) { + t.Parallel() + if !bundled.Embedded { + t.Skip("bundled files are not embedded") + } + + const text = "class C {\n #x = 1;\n m() { return this.#x; }\n}\n" + files := map[string]string{ + "/home/projects/tsconfig.json": `{}`, + "/home/projects/repro.ts": text, + } + fs := bundled.WrapFS(vfstest.FromMap(files, tspath.CaseInsensitive)) + + onServerRequest := func(_ context.Context, req *lsproto.RequestMessage) *lsproto.ResponseMessage { + if req.Method == lsproto.MethodClientRegisterCapability || req.Method == lsproto.MethodClientUnregisterCapability { + return &lsproto.ResponseMessage{ID: req.ID, JSONRPC: req.JSONRPC, Result: lsproto.Null{}} + } + return nil + } + + client, closeClient := lsptestutil.NewLSPClient(t, lsp.ServerOptions{ + Err: io.Discard, + Cwd: "/home/projects", + FS: fs, + DefaultLibraryPath: bundled.LibPath(), + }, onServerRequest) + t.Cleanup(func() { _ = closeClient() }) + + initMsg, _, ok := client.SendRequest(t, lsproto.InitializeInfo, &lsproto.InitializeParams{ + Capabilities: &lsproto.ClientCapabilities{}, + }) + assert.Assert(t, ok && initMsg.AsResponse().Error == nil, "Initialize failed") + client.SendNotification(t, lsproto.InitializedInfo, &lsproto.InitializedParams{}) + <-client.Server.InitComplete() + + uri := lsproto.DocumentUri("file:///home/projects/repro.ts") + client.SendNotification(t, lsproto.TextDocumentDidOpenInfo, &lsproto.DidOpenTextDocumentParams{ + TextDocument: &lsproto.TextDocumentItem{Uri: uri, LanguageId: "typescript", Text: text}, + }) + + msg, res, ok := client.SendRequest(t, lsproto.TextDocumentPrepareRenameInfo, &lsproto.PrepareRenameParams{ + TextDocument: lsproto.TextDocumentIdentifier{Uri: uri}, + Position: lsproto.Position{Line: 1, Character: 3}, + }) + assert.Assert(t, ok && msg.AsResponse().Error == nil) + assert.Assert(t, res.PrepareRenamePlaceholder != nil) + assert.Equal(t, res.PrepareRenamePlaceholder.Placeholder, "#x") +}