Replace endsWith() with isDefaultLib()

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)
This commit is contained in:
Jason Ramsay
2015-02-09 12:56:54 -08:00
parent a710902a5f
commit bf7bb517e0

View File

@@ -5520,7 +5520,7 @@ module ts {
var defaultLibFile = getDefaultLibFileName(host.getCompilationSettings());
for (var i = 0; i < declarations.length; i++) {
var sourceFile = declarations[i].getSourceFile();
if (sourceFile && endsWith(sourceFile.fileName, defaultLibFile)) {
if (sourceFile && isDefaultLibFile(sourceFile.fileName, defaultLibFile)) {
return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library.key));
}
}
@@ -5543,8 +5543,14 @@ module ts {
return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_this_element.key));
function endsWith(string: string, value: string): boolean {
return string.lastIndexOf(value) + value.length === string.length;
function isDefaultLibFile(fileName: string, defaultLibFile: string): boolean {
var hasValidPrefix = true;
var index = fileName.lastIndexOf(defaultLibFile);
if (index - 1 >= 0) {
var prefix = fileName[index - 1];
hasValidPrefix = (prefix === "\\" || prefix === "/");
}
return index >= 0 && hasValidPrefix && (index + defaultLibFile.length === fileName.length);
}
function getRenameInfoError(localizedErrorMessage: string): RenameInfo {