From 56b618b9fcb92a1363fcf1cced19062bfd0c3129 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 18 Apr 2018 11:44:28 -0700 Subject: [PATCH] Use indexOf and substr to exclude node_modules and bowerComponents instead of using loop --- src/server/typingsInstaller/typingsInstaller.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/server/typingsInstaller/typingsInstaller.ts b/src/server/typingsInstaller/typingsInstaller.ts index 463851e96f4..7d8cf02fab0 100644 --- a/src/server/typingsInstaller/typingsInstaller.ts +++ b/src/server/typingsInstaller/typingsInstaller.ts @@ -69,8 +69,13 @@ namespace ts.server.typingsInstaller { return base === "package.json" || base === "bower.json"; } - function isInNodeModulesOrBowerComponents(f: string) { - return stringContains(f, "/node_modules/") || stringContains(f, "/bower_components/"); + function getDirectoryExcludingNodeModulesOrBowerComponents(f: string) { + const indexOfNodeModules = f.indexOf("/node_modules/"); + const indexOfBowerComponents = f.indexOf("/bower_components/"); + const subStrLength = indexOfNodeModules === -1 || indexOfBowerComponents === -1 ? + Math.max(indexOfNodeModules, indexOfBowerComponents) : + Math.min(indexOfNodeModules, indexOfBowerComponents); + return subStrLength === -1 ? f : f.substr(0, subStrLength); } type ProjectWatchers = Map & { isInvoked?: boolean; }; @@ -478,12 +483,7 @@ namespace ts.server.typingsInstaller { } // Get path without node_modules and bower_components - let pathToWatch = getDirectoryPath(filePath); - while (isInNodeModulesOrBowerComponents(pathToWatch)) { - pathToWatch = getDirectoryPath(pathToWatch); - } - - createProjectWatcher(pathToWatch, createProjectDirectoryWatcher); + createProjectWatcher(getDirectoryExcludingNodeModulesOrBowerComponents(getDirectoryPath(filePath)), createProjectDirectoryWatcher); } // Remove unused watches