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

Function tryGetModuleNameAsNodeModule

test/fixtures/snapshot/typescript.js:122487–122600  ·  view source on GitHub ↗
(_a, _b, importingSourceFile, host, options, userPreferences, packageNameOnly, overrideMode)

Source from the content-addressed store, hash-verified

122485 : ts.removeFileExtension(relativePath);
122486 }
122487 function tryGetModuleNameAsNodeModule(_a, _b, importingSourceFile, host, options, userPreferences, packageNameOnly, overrideMode) {
122488 var path = _a.path, isRedirect = _a.isRedirect;
122489 var getCanonicalFileName = _b.getCanonicalFileName, sourceDirectory = _b.sourceDirectory;
122490 if (!host.fileExists || !host.readFile) {
122491 return undefined;
122492 }
122493 var parts = ts.getNodeModulePathParts(path);
122494 if (!parts) {
122495 return undefined;
122496 }
122497 // Simplify the full file path to something that can be resolved by Node.
122498 var moduleSpecifier = path;
122499 var isPackageRootPath = false;
122500 if (!packageNameOnly) {
122501 var preferences = getPreferences(host, userPreferences, options, importingSourceFile);
122502 var packageRootIndex = parts.packageRootIndex;
122503 var moduleFileName = void 0;
122504 while (true) {
122505 // If the module could be imported by a directory name, use that directory's name
122506 var _c = tryDirectoryWithPackageJson(packageRootIndex), moduleFileToTry = _c.moduleFileToTry, packageRootPath = _c.packageRootPath, blockedByExports = _c.blockedByExports, verbatimFromExports = _c.verbatimFromExports;
122507 if (ts.getEmitModuleResolutionKind(options) !== ts.ModuleResolutionKind.Classic) {
122508 if (blockedByExports) {
122509 return undefined; // File is under this package.json, but is not publicly exported - there's no way to name it via `node_modules` resolution
122510 }
122511 if (verbatimFromExports) {
122512 return moduleFileToTry;
122513 }
122514 }
122515 if (packageRootPath) {
122516 moduleSpecifier = packageRootPath;
122517 isPackageRootPath = true;
122518 break;
122519 }
122520 if (!moduleFileName)
122521 moduleFileName = moduleFileToTry;
122522 // try with next level of directory
122523 packageRootIndex = path.indexOf(ts.directorySeparator, packageRootIndex + 1);
122524 if (packageRootIndex === -1) {
122525 moduleSpecifier = removeExtensionAndIndexPostFix(moduleFileName, preferences.ending, options, host);
122526 break;
122527 }
122528 }
122529 }
122530 if (isRedirect && !isPackageRootPath) {
122531 return undefined;
122532 }
122533 var globalTypingsCacheLocation = host.getGlobalTypingsCacheLocation && host.getGlobalTypingsCacheLocation();
122534 // Get a path that's relative to node_modules or the importing file's path
122535 // if node_modules folder is in this folder or any of its parent folders, no need to keep it.
122536 var pathToTopLevelNodeModules = getCanonicalFileName(moduleSpecifier.substring(0, parts.topLevelNodeModulesIndex));
122537 if (!(ts.startsWith(sourceDirectory, pathToTopLevelNodeModules) || globalTypingsCacheLocation && ts.startsWith(getCanonicalFileName(globalTypingsCacheLocation), pathToTopLevelNodeModules))) {
122538 return undefined;
122539 }
122540 // If the module was found in @types, get the actual Node package name
122541 var nodeModulesDirectoryName = moduleSpecifier.substring(parts.topLevelPackageNameIndex + 1);
122542 var packageName = ts.getPackageNameFromTypesPackageName(nodeModulesDirectoryName);
122543 // For classic resolution, only allow importing from node_modules/@types, not other node_modules
122544 return ts.getEmitModuleResolutionKind(options) === ts.ModuleResolutionKind.Classic && packageName === nodeModulesDirectoryName ? undefined : packageName;

Callers 3

getModuleSpecifierWorkerFunction · 0.85
computeModuleSpecifiersFunction · 0.85

Calls 5

getPreferencesFunction · 0.85
getCanonicalFileNameFunction · 0.85
indexOfMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…