MCPcopy Index your code
hub / github.com/microsoft/typescript-go / getLocalModuleSpecifier

Function getLocalModuleSpecifier

internal/modulespecifiers/specifiers.go:485–634  ·  view source on GitHub ↗
(
	moduleFileName string,
	info Info,
	compilerOptions *core.CompilerOptions,
	host ModuleSpecifierGenerationHost,
	importMode core.ResolutionMode,
	preferences ModuleSpecifierPreferences,
	pathsOnly bool,
)

Source from the content-addressed store, hash-verified

483}
484
485func getLocalModuleSpecifier(
486 moduleFileName string,
487 info Info,
488 compilerOptions *core.CompilerOptions,
489 host ModuleSpecifierGenerationHost,
490 importMode core.ResolutionMode,
491 preferences ModuleSpecifierPreferences,
492 pathsOnly bool,
493) string {
494 paths := compilerOptions.Paths
495 rootDirs := compilerOptions.RootDirs
496
497 if pathsOnly && paths == nil {
498 return ""
499 }
500
501 sourceDirectory := info.SourceDirectory
502
503 allowedEndings := preferences.getAllowedEndingsInPreferredOrder(importMode)
504 var relativePath string
505 if len(rootDirs) > 0 {
506 relativePath = tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, allowedEndings, compilerOptions, host)
507 }
508 if len(relativePath) == 0 {
509 relativePath = processEnding(ensurePathIsNonModuleName(tspath.GetRelativePathFromDirectory(sourceDirectory, moduleFileName, tspath.ComparePathsOptions{
510 UseCaseSensitiveFileNames: host.UseCaseSensitiveFileNames(),
511 CurrentDirectory: host.GetCurrentDirectory(),
512 })), allowedEndings, compilerOptions, host)
513 }
514
515 if (paths == nil && !compilerOptions.GetResolvePackageJsonImports()) || preferences.relativePreference == RelativePreferenceRelative {
516 if pathsOnly {
517 return ""
518 }
519 return relativePath
520 }
521
522 root := compilerOptions.GetPathsBasePath(host.GetCurrentDirectory())
523 baseDirectory := tspath.GetNormalizedAbsolutePath(root, host.GetCurrentDirectory())
524 relativeToBaseUrl := getRelativePathIfInSameVolume(moduleFileName, baseDirectory, host.UseCaseSensitiveFileNames())
525 if len(relativeToBaseUrl) == 0 {
526 if pathsOnly {
527 return ""
528 }
529 return relativePath
530 }
531
532 var fromPackageJsonImports string
533 if !pathsOnly {
534 fromPackageJsonImports = tryGetModuleNameFromPackageJsonImports(
535 moduleFileName,
536 sourceDirectory,
537 compilerOptions,
538 host,
539 importMode,
540 prefersTsExtension(allowedEndings),
541 )
542 }

Callers 2

computeModuleSpecifiersFunction · 0.85

Calls 15

ContainsPathMethod · 0.95
PathIsRelativeFunction · 0.92
ToPathFunction · 0.92
GetDirectoryPathFunction · 0.92
lenFunction · 0.85
processEndingFunction · 0.85

Tested by

no test coverage detected