mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-10 18:04:18 -05:00
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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user