From fea16670026815916653cbbcb17663dd1b74ca9b Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 15 Aug 2018 11:47:16 -0700 Subject: [PATCH] Use string contains and nodeModulesPathPart const for "/node_modules/" --- src/compiler/moduleNameResolver.ts | 5 +++-- src/compiler/moduleSpecifiers.ts | 4 ++-- src/compiler/resolutionCache.ts | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index f6024ed258d..7fb308912c3 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -769,7 +769,7 @@ namespace ts { const loader: ResolutionKindSpecificLoader = (extensions, candidate, failedLookupLocations, onlyRecordFailures, state) => nodeLoadModuleByRelativeName(extensions, candidate, failedLookupLocations, onlyRecordFailures, state, /*considerPackageJson*/ true); const resolved = tryLoadModuleUsingOptionalResolutionSettings(extensions, moduleName, containingDirectory, loader, failedLookupLocations, state); if (resolved) { - return toSearchResult({ resolved, isExternalLibraryImport: resolved.path.indexOf("/node_modules/") !== -1 }); + return toSearchResult({ resolved, isExternalLibraryImport: stringContains(resolved.path, nodeModulesPathPart) }); } if (!isExternalModuleNameRelative(moduleName)) { @@ -843,7 +843,8 @@ namespace ts { return loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocations, onlyRecordFailures, state, considerPackageJson); } - const nodeModulesPathPart = "/node_modules/"; + /*@internal*/ + export const nodeModulesPathPart = "/node_modules/"; /** * This will be called on the successfully resolved path from `loadModuleFromFile`. diff --git a/src/compiler/moduleSpecifiers.ts b/src/compiler/moduleSpecifiers.ts index e56951cb507..8c695e10be2 100644 --- a/src/compiler/moduleSpecifiers.ts +++ b/src/compiler/moduleSpecifiers.ts @@ -401,7 +401,7 @@ namespace ts.moduleSpecifiers { partEnd = fullPath.indexOf("/", partStart + 1); switch (state) { case States.BeforeNodeModules: - if (fullPath.indexOf("/node_modules/", partStart) === partStart) { + if (fullPath.indexOf(nodeModulesPathPart, partStart) === partStart) { topLevelNodeModulesIndex = partStart; topLevelPackageNameIndex = partEnd; state = States.NodeModules; @@ -418,7 +418,7 @@ namespace ts.moduleSpecifiers { } break; case States.PackageContent: - if (fullPath.indexOf("/node_modules/", partStart) === partStart) { + if (fullPath.indexOf(nodeModulesPathPart, partStart) === partStart) { state = States.NodeModules; } else { diff --git a/src/compiler/resolutionCache.ts b/src/compiler/resolutionCache.ts index ae451ae302d..2d7bcb34c2d 100644 --- a/src/compiler/resolutionCache.ts +++ b/src/compiler/resolutionCache.ts @@ -419,7 +419,7 @@ namespace ts { function getDirectoryToWatchFromFailedLookupLocationDirectory(dir: string, dirPath: Path) { // If directory path contains node module, get the most parent node_modules directory for watching - while (stringContains(dirPath, "/node_modules/")) { + while (stringContains(dirPath, nodeModulesPathPart)) { dir = getDirectoryPath(dir); dirPath = getDirectoryPath(dirPath); }