From bf7bb517e08b9e10003905c6cb8f2c6d39d087bb Mon Sep 17 00:00:00 2001 From: Jason Ramsay Date: Mon, 9 Feb 2015 12:56:54 -0800 Subject: [PATCH] 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) --- src/services/services.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index 56e68b2f5a1..ad6643faf3c 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -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 {