MCPcopy Index your code
hub / github.com/nodejs/node / tryResolve

Function tryResolve

test/fixtures/snapshot/typescript.js:43374–43411  ·  view source on GitHub ↗
(extensions)

Source from the content-addressed store, hash-verified

43372 var result = ts.forEach(extensions, function (ext) { return tryResolve(ext); });
43373 return createResolvedModuleWithFailedLookupLocations((_a = result === null || result === void 0 ? void 0 : result.value) === null || _a === void 0 ? void 0 : _a.resolved, (_b = result === null || result === void 0 ? void 0 : result.value) === null || _b === void 0 ? void 0 : _b.isExternalLibraryImport, failedLookupLocations, diagnostics, state.resultFromCache);
43374 function tryResolve(extensions) {
43375 var loader = function (extensions, candidate, onlyRecordFailures, state) { return nodeLoadModuleByRelativeName(extensions, candidate, onlyRecordFailures, state, /*considerPackageJson*/ true); };
43376 var resolved = tryLoadModuleUsingOptionalResolutionSettings(extensions, moduleName, containingDirectory, loader, state);
43377 if (resolved) {
43378 return toSearchResult({ resolved: resolved, isExternalLibraryImport: pathContainsNodeModules(resolved.path) });
43379 }
43380 if (!ts.isExternalModuleNameRelative(moduleName)) {
43381 var resolved_1;
43382 if (features & NodeResolutionFeatures.Imports && ts.startsWith(moduleName, "#")) {
43383 resolved_1 = loadModuleFromImports(extensions, moduleName, containingDirectory, state, cache, redirectedReference);
43384 }
43385 if (!resolved_1 && features & NodeResolutionFeatures.SelfName) {
43386 resolved_1 = loadModuleFromSelfNameReference(extensions, moduleName, containingDirectory, state, cache, redirectedReference);
43387 }
43388 if (!resolved_1) {
43389 if (traceEnabled) {
43390 trace(host, ts.Diagnostics.Loading_module_0_from_node_modules_folder_target_file_type_1, moduleName, Extensions[extensions]);
43391 }
43392 resolved_1 = loadModuleFromNearestNodeModulesDirectory(extensions, moduleName, containingDirectory, state, cache, redirectedReference);
43393 }
43394 if (!resolved_1)
43395 return undefined;
43396 var resolvedValue = resolved_1.value;
43397 if (!compilerOptions.preserveSymlinks && resolvedValue && !resolvedValue.originalPath) {
43398 var path = realPath(resolvedValue.path, host, traceEnabled);
43399 var originalPath = arePathsEqual(path, resolvedValue.path, host) ? undefined : resolvedValue.path;
43400 resolvedValue = __assign(__assign({}, resolvedValue), { path: path, originalPath: originalPath });
43401 }
43402 // For node_modules lookups, get the real path so that multiple accesses to an `npm link`-ed module do not create duplicate files.
43403 return { value: resolvedValue && { resolved: resolvedValue, isExternalLibraryImport: true } };
43404 }
43405 else {
43406 var _a = normalizePathForCJSResolution(containingDirectory, moduleName), candidate = _a.path, parts = _a.parts;
43407 var resolved_2 = nodeLoadModuleByRelativeName(extensions, candidate, /*onlyRecordFailures*/ false, state, /*considerPackageJson*/ true);
43408 // Treat explicit "node_modules" import as an external library import.
43409 return resolved_2 && toSearchResult({ resolved: resolved_2, isExternalLibraryImport: ts.contains(parts, "node_modules") });
43410 }
43411 }
43412 }
43413 // If you import from "." inside a containing directory "/foo", the result of `normalizePath`
43414 // would be "/foo", but this loses the information that `foo` is a directory and we intended

Callers 2

classicNameResolverFunction · 0.85

Calls 15

toSearchResultFunction · 0.85
pathContainsNodeModulesFunction · 0.85
loadModuleFromImportsFunction · 0.85
traceFunction · 0.85
realPathFunction · 0.85
arePathsEqualFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…