diff --git a/tsc/internal/fourslash/tests/renamePlaceholder_test.go b/tsc/internal/fourslash/tests/renamePlaceholder_test.go new file mode 100644 index 0000000000000..381db2d6736b4 --- /dev/null +++ b/tsc/internal/fourslash/tests/renamePlaceholder_test.go @@ -0,0 +1,100 @@ +package fourslash_test + +import ( + "testing" + + "github.com/microsoft/TypeScript/tsc/internal/fourslash" + "github.com/microsoft/TypeScript/tsc/internal/testutil" +) + +func TestRenamePlaceholder(t *testing.T) { + t.Parallel() + for _, test := range []struct { + name string + content string + placeholders []string + }{ + { + name: "quoted and computed properties", + content: `const obj = { + [|a|]: 1, + '[|b|]': 1, + ['[|c|]']: 1, +}; +obj.[|a|]; +obj['[|a|]']; +obj.[|b|]; +obj.[|c|];`, + placeholders: []string{"a", "b", "c", "a", "a", "b", "c"}, + }, + { + name: "double quotes and templates", + content: "const obj = { \"[|b|]\": 1, [`[|c|]`]: 1 };\n" + + "obj[\"[|b|]\"]; obj[`[|c|]`]; obj.[|b|]; obj.[|c|];", + placeholders: []string{"b", "c", "b", "c", "b", "c"}, + }, + { + name: "escaped and unicode properties", + content: `const obj = { '[|\u0062|]': 1, ['[|𐐀|]']: 2 }; +obj.[|b|]; obj['[|\u0062|]']; obj.[|𐐀|]; obj['[|𐐀|]'];`, + placeholders: []string{"b", "𐐀", "b", "b", "𐐀", "𐐀"}, + }, + { + name: "private identifiers", + content: `class C { [|#foo|] = 1; method() { return this.[|#foo|]; } }`, + placeholders: []string{"#foo", "#foo"}, + }, + { + name: "numeric properties", + content: `const obj = { [|0|]: 1, [[|1|]]: 2 }; obj[[|0|]]; obj[[|1|]];`, + placeholders: []string{"0", "1", "0", "1"}, + }, + { + name: "import aliases", + content: `// @Filename: /a.ts +export const original = 1; +// @Filename: /b.ts +import { original as [|local|] } from "./a"; +[|local|];`, + placeholders: []string{"local", "local"}, + }, + { + name: "named default export", + content: `export default class [|C|] {} const obj = new [|C|]();`, + placeholders: []string{"C", "C"}, + }, + } { + t.Run(test.name, func(t *testing.T) { + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, test.content) + defer done() + for i, r := range f.Ranges() { + f.GoToRangeStart(t, r) + f.VerifyRenameRange(t, r.LSRange, test.placeholders[i], nil /*preferences*/) + } + }) + } +} + +func TestRenamePlaceholderEdits(t *testing.T) { + t.Parallel() + for _, test := range []struct { + name string + content string + expected string + }{ + {"b", "const obj = { 'b': 1 };\nobj.[|/*rename*/b|];", "const obj = { 'b1': 1 };\nobj.b1;"}, + {"c", "const obj = { ['c']: 1 };\nobj.[|/*rename*/c|];", "const obj = { ['c1']: 1 };\nobj.c1;"}, + } { + t.Run(test.name, func(t *testing.T) { + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, "// @Filename: /a.ts\n"+test.content) + defer done() + f.GoToMarker(t, "rename") + f.VerifyRenameRange(t, f.Ranges()[0].LSRange, test.name, nil /*preferences*/) + f.VerifyRename(t, "rename", test.name+"1", map[string]string{ + "/a.ts": test.expected, + }) + }) + } +} diff --git a/tsc/internal/ls/rename.go b/tsc/internal/ls/rename.go index 3415142ae6545..b8c0642af8cd4 100644 --- a/tsc/internal/ls/rename.go +++ b/tsc/internal/ls/rename.go @@ -203,7 +203,18 @@ func (l *LanguageService) getRenameInfoForNode(ctx context.Context, newName stri return RenameInfo{}, false } - return getRenameInfoSuccess(node, sourceFile, ch.SymbolToString(symbol), l.converters), true + // Use the name at the rename location rather than the symbol's formatted + // declaration, which may include quotes or computed property brackets. + switch node.Kind { + case ast.KindIdentifier, + ast.KindPrivateIdentifier, + ast.KindStringLiteral, + ast.KindNumericLiteral, + ast.KindNoSubstitutionTemplateLiteral: + return getRenameInfoSuccess(node, sourceFile, node.Text(), l.converters), true + default: + return getRenameInfoSuccess(node, sourceFile, ch.SymbolToString(symbol), l.converters), true + } } func nodeIsEligibleForRename(node *ast.Node) bool {