Skip to content

Commit 56b618b

Browse files
committed
Use indexOf and substr to exclude node_modules and bowerComponents instead of using loop
1 parent 67bb67e commit 56b618b

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

src/server/typingsInstaller/typingsInstaller.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ namespace ts.server.typingsInstaller {
6969
return base === "package.json" || base === "bower.json";
7070
}
7171

72-
function isInNodeModulesOrBowerComponents(f: string) {
73-
return stringContains(f, "/node_modules/") || stringContains(f, "/bower_components/");
72+
function getDirectoryExcludingNodeModulesOrBowerComponents(f: string) {
73+
const indexOfNodeModules = f.indexOf("/node_modules/");
74+
const indexOfBowerComponents = f.indexOf("/bower_components/");
75+
const subStrLength = indexOfNodeModules === -1 || indexOfBowerComponents === -1 ?
76+
Math.max(indexOfNodeModules, indexOfBowerComponents) :
77+
Math.min(indexOfNodeModules, indexOfBowerComponents);
78+
return subStrLength === -1 ? f : f.substr(0, subStrLength);
7479
}
7580

7681
type ProjectWatchers = Map<FileWatcher> & { isInvoked?: boolean; };
@@ -478,12 +483,7 @@ namespace ts.server.typingsInstaller {
478483
}
479484

480485
// Get path without node_modules and bower_components
481-
let pathToWatch = getDirectoryPath(filePath);
482-
while (isInNodeModulesOrBowerComponents(pathToWatch)) {
483-
pathToWatch = getDirectoryPath(pathToWatch);
484-
}
485-
486-
createProjectWatcher(pathToWatch, createProjectDirectoryWatcher);
486+
createProjectWatcher(getDirectoryExcludingNodeModulesOrBowerComponents(getDirectoryPath(filePath)), createProjectDirectoryWatcher);
487487
}
488488

489489
// Remove unused watches

0 commit comments

Comments
 (0)