Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions tsc/internal/fourslash/tests/renamePlaceholder_test.go
Original file line number Diff line number Diff line change
@@ -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,
})
})
}
}
13 changes: 12 additions & 1 deletion tsc/internal/ls/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down