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

Function computeModuleSpecifiers

test/fixtures/snapshot/typescript.js:122062–122123  ·  view source on GitHub ↗
(modulePaths, compilerOptions, importingSourceFile, host, userPreferences, options)

Source from the content-addressed store, hash-verified

122060 }
122061 moduleSpecifiers_1.getModuleSpecifiersWithCacheInfo = getModuleSpecifiersWithCacheInfo;
122062 function computeModuleSpecifiers(modulePaths, compilerOptions, importingSourceFile, host, userPreferences, options) {
122063 if (options === void 0) { options = {}; }
122064 var info = getInfo(importingSourceFile.path, host);
122065 var preferences = getPreferences(host, userPreferences, compilerOptions, importingSourceFile);
122066 var existingSpecifier = ts.forEach(modulePaths, function (modulePath) { return ts.forEach(host.getFileIncludeReasons().get(ts.toPath(modulePath.path, host.getCurrentDirectory(), info.getCanonicalFileName)), function (reason) {
122067 if (reason.kind !== ts.FileIncludeKind.Import || reason.file !== importingSourceFile.path)
122068 return undefined;
122069 // If the candidate import mode doesn't match the mode we're generating for, don't consider it
122070 // TODO: maybe useful to keep around as an alternative option for certain contexts where the mode is overridable
122071 if (importingSourceFile.impliedNodeFormat && importingSourceFile.impliedNodeFormat !== ts.getModeForResolutionAtIndex(importingSourceFile, reason.index))
122072 return undefined;
122073 var specifier = ts.getModuleNameStringLiteralAt(importingSourceFile, reason.index).text;
122074 // If the preference is for non relative and the module specifier is relative, ignore it
122075 return preferences.relativePreference !== 1 /* RelativePreference.NonRelative */ || !ts.pathIsRelative(specifier) ?
122076 specifier :
122077 undefined;
122078 }); });
122079 if (existingSpecifier) {
122080 var moduleSpecifiers_2 = [existingSpecifier];
122081 return moduleSpecifiers_2;
122082 }
122083 var importedFileIsInNodeModules = ts.some(modulePaths, function (p) { return p.isInNodeModules; });
122084 // Module specifier priority:
122085 // 1. "Bare package specifiers" (e.g. "@foo/bar") resulting from a path through node_modules to a package.json's "types" entry
122086 // 2. Specifiers generated using "paths" from tsconfig
122087 // 3. Non-relative specfiers resulting from a path through node_modules (e.g. "@foo/bar/path/to/file")
122088 // 4. Relative paths
122089 var nodeModulesSpecifiers;
122090 var pathsSpecifiers;
122091 var relativeSpecifiers;
122092 for (var _i = 0, modulePaths_1 = modulePaths; _i < modulePaths_1.length; _i++) {
122093 var modulePath = modulePaths_1[_i];
122094 var specifier = tryGetModuleNameAsNodeModule(modulePath, info, importingSourceFile, host, compilerOptions, userPreferences, /*packageNameOnly*/ undefined, options.overrideImportMode);
122095 nodeModulesSpecifiers = ts.append(nodeModulesSpecifiers, specifier);
122096 if (specifier && modulePath.isRedirect) {
122097 // If we got a specifier for a redirect, it was a bare package specifier (e.g. "@foo/bar",
122098 // not "@foo/bar/path/to/file"). No other specifier will be this good, so stop looking.
122099 return nodeModulesSpecifiers;
122100 }
122101 if (!specifier && !modulePath.isRedirect) {
122102 var local = getLocalModuleSpecifier(modulePath.path, info, compilerOptions, host, preferences);
122103 if (ts.pathIsBareSpecifier(local)) {
122104 pathsSpecifiers = ts.append(pathsSpecifiers, local);
122105 }
122106 else if (!importedFileIsInNodeModules || modulePath.isInNodeModules) {
122107 // Why this extra conditional, not just an `else`? If some path to the file contained
122108 // 'node_modules', but we can't create a non-relative specifier (e.g. "@foo/bar/path/to/file"),
122109 // that means we had to go through a *sibling's* node_modules, not one we can access directly.
122110 // If some path to the file was in node_modules but another was not, this likely indicates that
122111 // we have a monorepo structure with symlinks. In this case, the non-node_modules path is
122112 // probably the realpath, e.g. "../bar/path/to/file", but a relative path to another package
122113 // in a monorepo is probably not portable. So, the module specifier we actually go with will be
122114 // the relative path through node_modules, so that the declaration emitter can produce a
122115 // portability error. (See declarationEmitReexportedSymlinkReference3)
122116 relativeSpecifiers = ts.append(relativeSpecifiers, local);
122117 }
122118 }
122119 }

Callers 1

Calls 8

getInfoFunction · 0.85
getPreferencesFunction · 0.85
getLocalModuleSpecifierFunction · 0.85
someMethod · 0.80
forEachMethod · 0.65
getMethod · 0.65
appendMethod · 0.45

Tested by

no test coverage detected