Skip to content

Commit 88fbdf2

Browse files
committed
There are a couple of issues with using the current endsWith() function to determine if we should allow a rename for default lib files:
1. XXXX-lib.d.ts would not allow renames even though it should as the preceding characters are not being verified for directory separators 2. There is the potential for false matches as there is currently no check to verify indexOf was successful (index >= 0)
1 parent a710902 commit 88fbdf2

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/services/services.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5520,7 +5520,7 @@ module ts {
55205520
var defaultLibFile = getDefaultLibFileName(host.getCompilationSettings());
55215521
for (var i = 0; i < declarations.length; i++) {
55225522
var sourceFile = declarations[i].getSourceFile();
5523-
if (sourceFile && endsWith(sourceFile.fileName, defaultLibFile)) {
5523+
if (sourceFile && isDefaultLibFile(sourceFile.fileName, defaultLibFile)) {
55245524
return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library.key));
55255525
}
55265526
}
@@ -5543,8 +5543,14 @@ module ts {
55435543

55445544
return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_this_element.key));
55455545

5546-
function endsWith(string: string, value: string): boolean {
5547-
return string.lastIndexOf(value) + value.length === string.length;
5546+
function isDefaultLibFile(fileName: string, defaultLibFile: string): boolean {
5547+
var hasValidPrefix = true;
5548+
var index = fileName.lastIndexOf(defaultLibFile);
5549+
if (index - 1 >= 0) {
5550+
var prefix = fileName[index - 1];
5551+
hasValidPrefix = (prefix === "\\" || prefix === "/");
5552+
}
5553+
return index >= 0 && hasValidPrefix && (index + defaultLibFile.length === fileName.length);
55485554
}
55495555

55505556
function getRenameInfoError(localizedErrorMessage: string): RenameInfo {

0 commit comments

Comments
 (0)