From a47712c68da5f4e4da4f909ed0d723d7390ae01f Mon Sep 17 00:00:00 2001 From: zhengdaodeguL <1159957339@qq.com> Date: Sun, 15 Nov 2026 21:47:49 +0800 Subject: [PATCH] fix: deduplicate rename edits for dynamic imports with .ts specifiers (#65505) --- ...ileRename_dynamicImportTsExtension_test.go | 54 +++++++++++++++++++ tsc/internal/ls/file_rename.go | 8 ++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tsc/internal/fourslash/tests/getEditsForFileRename_dynamicImportTsExtension_test.go diff --git a/tsc/internal/fourslash/tests/getEditsForFileRename_dynamicImportTsExtension_test.go b/tsc/internal/fourslash/tests/getEditsForFileRename_dynamicImportTsExtension_test.go new file mode 100644 index 0000000000000..a765936fde623 --- /dev/null +++ b/tsc/internal/fourslash/tests/getEditsForFileRename_dynamicImportTsExtension_test.go @@ -0,0 +1,54 @@ +package fourslash_test + +import ( + "strings" + "testing" + + "github.com/microsoft/TypeScript/tsc/internal/fourslash" + "github.com/microsoft/TypeScript/tsc/internal/ls/lsconv" + "github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto" + "github.com/microsoft/TypeScript/tsc/internal/testutil" +) + +func TestGetEditsForFileRename_dynamicImportTsExtension(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `// @Filename: /tsconfig.json +{ + "compilerOptions": { + "strict": true, + "module": "esnext", + "moduleResolution": "bundler", + "target": "es2022", + "noEmit": true, + "allowImportingTsExtensions": true + }, + "files": ["imported.ts", "loads.ts"] +} +// @Filename: /imported.ts +export const value = 1; +// @Filename: /loads.ts +export const load = () => import("./imported.ts");` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + result := f.WillRenameFiles(t, &lsproto.FileRename{ + OldUri: lsconv.FileNameToDocumentURI("/imported.ts"), + NewUri: lsconv.FileNameToDocumentURI("/sub/moved.ts"), + }) + if result.WorkspaceEdit == nil || result.WorkspaceEdit.DocumentChanges == nil { + t.Fatal("workspace/willRenameFiles returned no document changes") + } + + var loadsEdits []lsproto.TextEditOrAnnotatedTextEditOrSnippetTextEdit + for _, change := range *result.WorkspaceEdit.DocumentChanges { + if change.TextDocumentEdit != nil && strings.HasSuffix(string(change.TextDocumentEdit.TextDocument.Uri), "/loads.ts") { + loadsEdits = change.TextDocumentEdit.Edits + } + } + if len(loadsEdits) != 1 { + t.Fatalf("expected one edit for loads.ts, got %d", len(loadsEdits)) + } + if loadsEdits[0].TextEdit == nil || loadsEdits[0].TextEdit.NewText != "./sub/moved.ts" { + t.Fatalf("expected import to be updated to ./sub/moved.ts, got %#v", loadsEdits[0]) + } +} diff --git a/tsc/internal/ls/file_rename.go b/tsc/internal/ls/file_rename.go index b3c1ca5e6eeb4..b667722a05639 100644 --- a/tsc/internal/ls/file_rename.go +++ b/tsc/internal/ls/file_rename.go @@ -240,10 +240,16 @@ func (l *LanguageService) updateImportsForFileRename(program *compiler.Program, } } + seenImportEdits := make(map[core.TextRange]string) for _, importStringLiteral := range sourceFile.Imports() { updated := l.getUpdatedImportSpecifier(program, checker, sourceFile, importStringLiteral, oldToNew, movedFiles, newImportFromPath, fileMoved, moduleSpecifierPreferences) if updated != "" && updated != importStringLiteral.Text() { - changeTracker.ReplaceTextRangeWithText(sourceFile, createStringTextRange(sourceFile, importStringLiteral), updated) + textRange := createStringTextRange(sourceFile, importStringLiteral) + if previous, ok := seenImportEdits[textRange]; ok && previous == updated { + continue + } + seenImportEdits[textRange] = updated + changeTracker.ReplaceTextRangeWithText(sourceFile, textRange, updated) } } }