From 321e70785ffb9aa7a71ce24410984beb39aad88b Mon Sep 17 00:00:00 2001 From: Josue Barros Date: Mon, 16 Nov 2026 15:59:58 -0300 Subject: [PATCH] Visit each dynamic import once when the specifier contains import ForEachDynamicImportOrRequireCall scans the source text for the keywords and resolves each hit to a node. A specifier such as "./imported.ts" contains the text "import", so the same import call was visited twice. collectExternalModuleReferences appended the specifier twice, and willRenameFiles then produced two overlapping text edits for one range and panicked with 'changes overlap'. Track visited nodes and invoke the callback once per node. Adds a fourslash willRenameFiles test for a .ts dynamic import specifier. --- tsc/internal/ast/utilities.go | 28 ++++++++++------ ...ileRename_dynamicImportTsSpecifier_test.go | 33 +++++++++++++++++++ 2 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 tsc/internal/fourslash/tests/getEditsForFileRename_dynamicImportTsSpecifier_test.go diff --git a/tsc/internal/ast/utilities.go b/tsc/internal/ast/utilities.go index f178903372dc2..30a0ed6969ae3 100644 --- a/tsc/internal/ast/utilities.go +++ b/tsc/internal/ast/utilities.go @@ -2848,20 +2848,28 @@ func ForEachDynamicImportOrRequireCall( cb func(node *Node, argument *Expression) bool, ) bool { isJavaScriptFile := IsInJSFile(file.AsNode()) + // The text scan can match the keyword inside a string literal, which resolves + // back to the same import call. Visit each node once. + seenNodes := make(map[*Node]bool) lastIndex, size := findImportOrRequire(file.Text(), 0) for lastIndex >= 0 { node := GetNodeAtPosition(file, lastIndex, isJavaScriptFile && includeTypeSpaceImports) - if isJavaScriptFile && IsRequireCall(node, requireStringLiteralLikeArgument) { - if cb(node, node.Arguments()[0]) { - return true - } - } else if IsImportCall(node) && len(node.Arguments()) > 0 && (!requireStringLiteralLikeArgument || IsStringLiteralLike(node.Arguments()[0])) { - if cb(node, node.Arguments()[0]) { - return true + if node == nil || !seenNodes[node] { + if node != nil { + seenNodes[node] = true } - } else if includeTypeSpaceImports && IsLiteralImportTypeNode(node) { - if cb(node, node.AsImportTypeNode().Argument.AsLiteralTypeNode().Literal) { - return true + if isJavaScriptFile && IsRequireCall(node, requireStringLiteralLikeArgument) { + if cb(node, node.Arguments()[0]) { + return true + } + } else if IsImportCall(node) && len(node.Arguments()) > 0 && (!requireStringLiteralLikeArgument || IsStringLiteralLike(node.Arguments()[0])) { + if cb(node, node.Arguments()[0]) { + return true + } + } else if includeTypeSpaceImports && IsLiteralImportTypeNode(node) { + if cb(node, node.AsImportTypeNode().Argument.AsLiteralTypeNode().Literal) { + return true + } } } // skip past import/require diff --git a/tsc/internal/fourslash/tests/getEditsForFileRename_dynamicImportTsSpecifier_test.go b/tsc/internal/fourslash/tests/getEditsForFileRename_dynamicImportTsSpecifier_test.go new file mode 100644 index 0000000000000..24cf57c000aac --- /dev/null +++ b/tsc/internal/fourslash/tests/getEditsForFileRename_dynamicImportTsSpecifier_test.go @@ -0,0 +1,33 @@ +package fourslash_test + +import ( + "testing" + + "github.com/microsoft/TypeScript/tsc/internal/fourslash" + "github.com/microsoft/TypeScript/tsc/internal/testutil" +) + +func TestGetEditsForFileRename_dynamicImportTsSpecifier(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 + } +} +// @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() + f.VerifyWillRenameFilesEdits(t, "/imported.ts", "/sub/moved.ts", map[string]string{ + "/loads.ts": `export const load = () => import("./sub/moved.ts");`, + }, nil /*preferences*/) +}