Skip to content

Commit 2e78d1e

Browse files
author
Andy
authored
getEditsForFileRename: Update tsconfig "files" (microsoft#23625)
1 parent eb112ab commit 2e78d1e

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/harness/fourslash.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,10 @@ namespace FourSlash {
276276
if (configFileName) {
277277
const baseDir = ts.normalizePath(ts.getDirectoryPath(configFileName));
278278
const host = new Utils.MockParseConfigHost(baseDir, /*ignoreCase*/ false, this.inputFiles);
279-
280-
const configJsonObj = ts.parseConfigFileTextToJson(configFileName, this.inputFiles.get(configFileName));
281-
assert.isTrue(configJsonObj.config !== undefined);
282-
283-
compilationOptions = ts.parseJsonConfigFileContent(configJsonObj.config, host, baseDir, compilationOptions, configFileName).options;
279+
const jsonSourceFile = ts.parseJsonText(configFileName, this.inputFiles.get(configFileName));
280+
compilationOptions = ts.parseJsonSourceFileConfigFileContent(jsonSourceFile, host, baseDir, compilationOptions, configFileName).options;
284281
}
285282

286-
287283
if (compilationOptions.typeRoots) {
288284
compilationOptions.typeRoots = compilationOptions.typeRoots.map(p => ts.getNormalizedAbsolutePath(p, this.basePath));
289285
}

src/services/getEditsForFileRename.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,33 @@ namespace ts {
33
export function getEditsForFileRename(program: Program, oldFilePath: string, newFilePath: string, host: LanguageServiceHost, formatContext: formatting.FormatContext): ReadonlyArray<FileTextChanges> {
44
const pathUpdater = getPathUpdater(oldFilePath, newFilePath, host);
55
return textChanges.ChangeTracker.with({ host, formatContext }, changeTracker => {
6+
updateTsconfigFiles(program, changeTracker, oldFilePath, newFilePath);
67
for (const { sourceFile, toUpdate } of getImportsToUpdate(program, oldFilePath)) {
78
const newPath = pathUpdater(isRef(toUpdate) ? toUpdate.fileName : toUpdate.text);
89
if (newPath !== undefined) {
9-
const range = isRef(toUpdate) ? toUpdate : createTextRange(toUpdate.getStart(sourceFile) + 1, toUpdate.end - 1);
10+
const range = isRef(toUpdate) ? toUpdate : createStringRange(toUpdate, sourceFile);
1011
changeTracker.replaceRangeWithText(sourceFile, range, isRef(toUpdate) ? newPath : removeFileExtension(newPath));
1112
}
1213
}
1314
});
1415
}
1516

17+
function updateTsconfigFiles(program: Program, changeTracker: textChanges.ChangeTracker, oldFilePath: string, newFilePath: string): void {
18+
const cfg = program.getCompilerOptions().configFile;
19+
if (!cfg) return;
20+
const oldFile = cfg.jsonObject && getFilesEntry(cfg.jsonObject, oldFilePath);
21+
if (oldFile) {
22+
changeTracker.replaceRangeWithText(cfg, createStringRange(oldFile, cfg), newFilePath);
23+
}
24+
}
25+
26+
function getFilesEntry(cfg: ObjectLiteralExpression, fileName: string): StringLiteral | undefined {
27+
const filesProp = find(cfg.properties, (prop): prop is PropertyAssignment =>
28+
isPropertyAssignment(prop) && isStringLiteral(prop.name) && prop.name.text === "files");
29+
const files = filesProp && filesProp.initializer;
30+
return files && isArrayLiteralExpression(files) ? find(files.elements, (e): e is StringLiteral => isStringLiteral(e) && e.text === fileName) : undefined;
31+
}
32+
1633
interface ToUpdate {
1734
readonly sourceFile: SourceFile;
1835
readonly toUpdate: StringLiteralLike | FileReference;
@@ -52,4 +69,8 @@ namespace ts {
5269
return ensurePathIsRelative(normalizePath(combinePaths(getDirectoryPath(oldPath), rel)));
5370
};
5471
}
72+
73+
function createStringRange(node: StringLiteralLike, sourceFile: SourceFileLike): TextRange {
74+
return createTextRange(node.getStart(sourceFile) + 1, node.end - 1);
75+
}
5576
}

tests/cases/fourslash/getEditsForFileRename.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
/////// <reference path="../old.ts" />
1313
////import old from "../old";
1414

15+
// @Filename: /tsconfig.json
16+
////{ "files": ["/a.ts", "/src/a.ts", "/src/foo/a.ts", "/src/old.ts"] }
17+
1518
verify.getEditsForFileRename({
1619
oldPath: "/src/old.ts",
1720
newPath: "/src/new.ts",
1821
newFileContents: {
1922
"/a.ts": '/// <reference path="./src/new.ts" />\nimport old from "./src/new";',
2023
"/src/a.ts": '/// <reference path="./new.ts" />\nimport old from "./new";',
2124
"/src/foo/a.ts": '/// <reference path="../new.ts" />\nimport old from "../new";',
25+
"/tsconfig.json": '{ "files": ["/a.ts", "/src/a.ts", "/src/foo/a.ts", "/src/new.ts"] }',
2226
},
2327
});

0 commit comments

Comments
 (0)