Skip to content

Commit da86b27

Browse files
committed
getEditsForFileRename: fix updateTsconfigFiles w/ empty include
Avoid the assumption that there are always include patterns: when there are none (and therefore the renamed file didn't match anyway), just skip the test for added include. Also change the code to use `return` to make it flatter. (Also get rid of a redundant type.) Fixes microsoft#40386.
1 parent faefc72 commit da86b27

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/services/getEditsForFileRename.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ namespace ts {
5959
case "include":
6060
case "exclude": {
6161
const foundExactMatch = updatePaths(property);
62-
if (!foundExactMatch && propertyName === "include" && isArrayLiteralExpression(property.initializer)) {
63-
const includes = mapDefined(property.initializer.elements, e => isStringLiteral(e) ? e.text : undefined);
64-
const matchers = getFileMatcherPatterns(configDir, /*excludes*/ [], includes, useCaseSensitiveFileNames, currentDirectory);
65-
// If there isn't some include for this, add a new one.
66-
if (getRegexFromPattern(Debug.checkDefined(matchers.includeFilePattern), useCaseSensitiveFileNames).test(oldFileOrDirPath) &&
67-
!getRegexFromPattern(Debug.checkDefined(matchers.includeFilePattern), useCaseSensitiveFileNames).test(newFileOrDirPath)) {
68-
changeTracker.insertNodeAfter(configFile, last(property.initializer.elements), factory.createStringLiteral(relativePath(newFileOrDirPath)));
69-
}
62+
if (foundExactMatch || propertyName !== "include" || !isArrayLiteralExpression(property.initializer)) return;
63+
const includes = mapDefined(property.initializer.elements, e => isStringLiteral(e) ? e.text : undefined);
64+
if (includes.length === 0) return;
65+
const matchers: FileMatcherPatterns = getFileMatcherPatterns(configDir, /*excludes*/ [], includes, useCaseSensitiveFileNames, currentDirectory);
66+
// If there isn't some include for this, add a new one.
67+
if (getRegexFromPattern(Debug.checkDefined(matchers.includeFilePattern), useCaseSensitiveFileNames).test(oldFileOrDirPath) &&
68+
!getRegexFromPattern(Debug.checkDefined(matchers.includeFilePattern), useCaseSensitiveFileNames).test(newFileOrDirPath)) {
69+
changeTracker.insertNodeAfter(configFile, last(property.initializer.elements), factory.createStringLiteral(relativePath(newFileOrDirPath)));
7070
}
71-
break;
71+
return;
7272
}
7373
case "compilerOptions":
7474
forEachProperty(property.initializer, (property, propertyName) => {
@@ -85,13 +85,12 @@ namespace ts {
8585
});
8686
}
8787
});
88-
break;
88+
return;
8989
}
9090
});
9191

9292
function updatePaths(property: PropertyAssignment): boolean {
93-
// Type annotation needed due to #7294
94-
const elements: readonly Expression[] = isArrayLiteralExpression(property.initializer) ? property.initializer.elements : [property.initializer];
93+
const elements = isArrayLiteralExpression(property.initializer) ? property.initializer.elements : [property.initializer];
9594
let foundExactMatch = false;
9695
for (const element of elements) {
9796
foundExactMatch = tryUpdateString(element) || foundExactMatch;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /a/foo.ts
4+
////const x = 1
5+
6+
// @Filename: /a/tsconfig.json
7+
////{ "include": [] }
8+
9+
verify.getEditsForFileRename({
10+
oldPath: "/a/foo.ts",
11+
newPath: "/a/bar.ts",
12+
newFileContents: {
13+
}
14+
});

0 commit comments

Comments
 (0)