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

Function getLocalModuleSpecifier

test/fixtures/snapshot/typescript.js:122130–122191  ·  view source on GitHub ↗
(moduleFileName, info, compilerOptions, host, _a)

Source from the content-addressed store, hash-verified

122128 return { getCanonicalFileName: getCanonicalFileName, importingSourceFileName: importingSourceFileName, sourceDirectory: sourceDirectory };
122129 }
122130 function getLocalModuleSpecifier(moduleFileName, info, compilerOptions, host, _a) {
122131 var ending = _a.ending, relativePreference = _a.relativePreference;
122132 var baseUrl = compilerOptions.baseUrl, paths = compilerOptions.paths, rootDirs = compilerOptions.rootDirs;
122133 var sourceDirectory = info.sourceDirectory, getCanonicalFileName = info.getCanonicalFileName;
122134 var relativePath = rootDirs && tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName, ending, compilerOptions) ||
122135 removeExtensionAndIndexPostFix(ts.ensurePathIsNonModuleName(ts.getRelativePathFromDirectory(sourceDirectory, moduleFileName, getCanonicalFileName)), ending, compilerOptions);
122136 if (!baseUrl && !paths || relativePreference === 0 /* RelativePreference.Relative */) {
122137 return relativePath;
122138 }
122139 var baseDirectory = ts.getNormalizedAbsolutePath(ts.getPathsBasePath(compilerOptions, host) || baseUrl, host.getCurrentDirectory());
122140 var relativeToBaseUrl = getRelativePathIfInDirectory(moduleFileName, baseDirectory, getCanonicalFileName);
122141 if (!relativeToBaseUrl) {
122142 return relativePath;
122143 }
122144 var importRelativeToBaseUrl = removeExtensionAndIndexPostFix(relativeToBaseUrl, ending, compilerOptions);
122145 var fromPaths = paths && tryGetModuleNameFromPaths(ts.removeFileExtension(relativeToBaseUrl), importRelativeToBaseUrl, paths);
122146 var nonRelative = fromPaths === undefined && baseUrl !== undefined ? importRelativeToBaseUrl : fromPaths;
122147 if (!nonRelative) {
122148 return relativePath;
122149 }
122150 if (relativePreference === 1 /* RelativePreference.NonRelative */) {
122151 return nonRelative;
122152 }
122153 if (relativePreference === 3 /* RelativePreference.ExternalNonRelative */) {
122154 var projectDirectory = compilerOptions.configFilePath ?
122155 ts.toPath(ts.getDirectoryPath(compilerOptions.configFilePath), host.getCurrentDirectory(), info.getCanonicalFileName) :
122156 info.getCanonicalFileName(host.getCurrentDirectory());
122157 var modulePath = ts.toPath(moduleFileName, projectDirectory, getCanonicalFileName);
122158 var sourceIsInternal = ts.startsWith(sourceDirectory, projectDirectory);
122159 var targetIsInternal = ts.startsWith(modulePath, projectDirectory);
122160 if (sourceIsInternal && !targetIsInternal || !sourceIsInternal && targetIsInternal) {
122161 // 1. The import path crosses the boundary of the tsconfig.json-containing directory.
122162 //
122163 // src/
122164 // tsconfig.json
122165 // index.ts -------
122166 // lib/ | (path crosses tsconfig.json)
122167 // imported.ts <---
122168 //
122169 return nonRelative;
122170 }
122171 var nearestTargetPackageJson = getNearestAncestorDirectoryWithPackageJson(host, ts.getDirectoryPath(modulePath));
122172 var nearestSourcePackageJson = getNearestAncestorDirectoryWithPackageJson(host, sourceDirectory);
122173 if (nearestSourcePackageJson !== nearestTargetPackageJson) {
122174 // 2. The importing and imported files are part of different packages.
122175 //
122176 // packages/a/
122177 // package.json
122178 // index.ts --------
122179 // packages/b/ | (path crosses package.json)
122180 // package.json |
122181 // component.ts <---
122182 //
122183 return nonRelative;
122184 }
122185 return relativePath;
122186 }
122187 if (relativePreference !== 2 /* RelativePreference.Shortest */)

Callers 2

getModuleSpecifierWorkerFunction · 0.85
computeModuleSpecifiersFunction · 0.85

Tested by

no test coverage detected