From bf7bb517e08b9e10003905c6cb8f2c6d39d087bb Mon Sep 17 00:00:00 2001 From: Jason Ramsay Date: Mon, 9 Feb 2015 12:56:54 -0800 Subject: [PATCH 1/3] 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 { From a51ce92500acc2949f1ea9e69e79965a7f209612 Mon Sep 17 00:00:00 2001 From: Jason Ramsay Date: Fri, 13 Feb 2015 11:56:39 -0800 Subject: [PATCH 2/3] switch to using host.getDefaultLibFileName(options) --- src/services/services.ts | 22 +++++++--------------- src/services/shims.ts | 5 +---- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index ad6643faf3c..6f6d4b869a1 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -5517,11 +5517,13 @@ module ts { var declarations = symbol.getDeclarations(); if (declarations && declarations.length > 0) { // Disallow rename for elements that are defined in the standard TypeScript library. - var defaultLibFile = getDefaultLibFileName(host.getCompilationSettings()); - for (var i = 0; i < declarations.length; i++) { - var sourceFile = declarations[i].getSourceFile(); - if (sourceFile && isDefaultLibFile(sourceFile.fileName, defaultLibFile)) { - return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library.key)); + var defaultLibFileName = host.getDefaultLibFileName(host.getCompilationSettings()); + if (defaultLibFileName) { + for (var i = 0; i < declarations.length; i++) { + var sourceFile = declarations[i].getSourceFile(); + if (sourceFile && ts.normalizePath(sourceFile.fileName) === ts.normalizePath(defaultLibFileName)) { + return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library.key)); + } } } @@ -5543,16 +5545,6 @@ module ts { return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_this_element.key)); - 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 { return { canRename: false, diff --git a/src/services/shims.ts b/src/services/shims.ts index 0f9f1a12cd1..776fecf374e 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -274,10 +274,7 @@ module ts { } public getDefaultLibFileName(options: CompilerOptions): string { - // Shim the API changes for 1.5 release. This should be removed once - // TypeScript 1.5 has shipped. - return ""; - //return this.shimHost.getDefaultLibFileName(JSON.stringify(options)); + return this.shimHost.getDefaultLibFileName(JSON.stringify(options)); } } From 975f10c6a1bff1c1b71e8cac52ea81b4814615c0 Mon Sep 17 00:00:00 2001 From: Jason Ramsay Date: Tue, 17 Feb 2015 14:44:45 -0800 Subject: [PATCH 3/3] Adding getCanonicalFileName to ensure case-sensitive systems do not have issues --- src/services/services.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/services.ts b/src/services/services.ts index 94ad9d60b6c..a69a3b4154c 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -5522,7 +5522,7 @@ module ts { if (defaultLibFileName) { for (var i = 0; i < declarations.length; i++) { var sourceFile = declarations[i].getSourceFile(); - if (sourceFile && ts.normalizePath(sourceFile.fileName) === ts.normalizePath(defaultLibFileName)) { + if (sourceFile && getCanonicalFileName(ts.normalizePath(sourceFile.fileName)) === getCanonicalFileName(ts.normalizePath(defaultLibFileName))) { return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library.key)); } }