1namespace ts.projectSystem { 2 describe("unittests:: tsserver:: getEditsForFileRename", () => { 3 it("works for host implementing 'resolveModuleNames' and 'getResolvedModuleWithFailedLookupLocationsFromCache'", () => { 4 const userTs: File = { 5 path: "/user.ts", 6 content: 'import { x } from "./old";', 7 }; 8 const newTs: File = { 9 path: "/new.ts", 10 content: "export const x = 0;", 11 }; 12 const tsconfig: File = { 13 path: "/tsconfig.json", 14 content: "{}", 15 }; 16 17 const host = createServerHost([userTs, newTs, tsconfig]); 18 const projectService = createProjectService(host); 19 projectService.openClientFile(userTs.path); 20 const project = projectService.configuredProjects.get(tsconfig.path)!; 21 22 Debug.assert(!!project.resolveModuleNames); 23 24 const edits = project.getLanguageService().getEditsForFileRename("/old.ts", "/new.ts", testFormatSettings, emptyOptions); 25 assert.deepEqual<readonly FileTextChanges[]>(edits, [{ 26 fileName: "/user.ts", 27 textChanges: [{ 28 span: textSpanFromSubstring(userTs.content, "./old"), 29 newText: "./new", 30 }], 31 }]); 32 }); 33 34 it("works with multiple projects", () => { 35 const aUserTs: File = { 36 path: "/a/user.ts", 37 content: 'import { x } from "./old";', 38 }; 39 const aOldTs: File = { 40 path: "/a/old.ts", 41 content: "export const x = 0;", 42 }; 43 const aTsconfig: File = { 44 path: "/a/tsconfig.json", 45 content: JSON.stringify({ files: ["./old.ts", "./user.ts"] }), 46 }; 47 const bUserTs: File = { 48 path: "/b/user.ts", 49 content: 'import { x } from "../a/old";', 50 }; 51 const bTsconfig: File = { 52 path: "/b/tsconfig.json", 53 content: "{}", 54 }; 55 56 const host = createServerHost([aUserTs, aOldTs, aTsconfig, bUserTs, bTsconfig]); 57 const session = createSession(host); 58 openFilesForSession([aUserTs, bUserTs], session); 59 60 const response = executeSessionRequest<protocol.GetEditsForFileRenameRequest, protocol.GetEditsForFileRenameResponse>(session, CommandNames.GetEditsForFileRename, { 61 oldFilePath: aOldTs.path, 62 newFilePath: "/a/new.ts", 63 }); 64 assert.deepEqual<readonly protocol.FileCodeEdits[]>(response, [ 65 { 66 fileName: aTsconfig.path, 67 textChanges: [{ ...protocolTextSpanFromSubstring(aTsconfig.content, "./old.ts"), newText: "new.ts" }], 68 }, 69 { 70 fileName: aUserTs.path, 71 textChanges: [{ ...protocolTextSpanFromSubstring(aUserTs.content, "./old"), newText: "./new" }], 72 }, 73 { 74 fileName: bUserTs.path, 75 textChanges: [{ ...protocolTextSpanFromSubstring(bUserTs.content, "../a/old"), newText: "../a/new" }], 76 }, 77 ]); 78 }); 79 80 it("works with file moved to inferred project", () => { 81 const aTs: File = { path: "/a.ts", content: 'import {} from "./b";' }; 82 const cTs: File = { path: "/c.ts", content: "export {};" }; 83 const tsconfig: File = { path: "/tsconfig.json", content: JSON.stringify({ files: ["./a.ts", "./b.ts"] }) }; 84 85 const host = createServerHost([aTs, cTs, tsconfig]); 86 const session = createSession(host); 87 openFilesForSession([aTs, cTs], session); 88 89 const response = executeSessionRequest<protocol.GetEditsForFileRenameRequest, protocol.GetEditsForFileRenameResponse>(session, CommandNames.GetEditsForFileRename, { 90 oldFilePath: "/b.ts", 91 newFilePath: cTs.path, 92 }); 93 assert.deepEqual<readonly protocol.FileCodeEdits[]>(response, [ 94 { 95 fileName: "/tsconfig.json", 96 textChanges: [{ ...protocolTextSpanFromSubstring(tsconfig.content, "./b.ts"), newText: "c.ts" }], 97 }, 98 { 99 fileName: "/a.ts", 100 textChanges: [{ ...protocolTextSpanFromSubstring(aTs.content, "./b"), newText: "./c" }], 101 }, 102 ]); 103 }); 104 }); 105} 106