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

Function computeModuleSpecifiers

internal/modulespecifiers/specifiers.go:359–483  ·  view source on GitHub ↗
(
	modulePaths []ModulePath,
	compilerOptions *core.CompilerOptions,
	importingSourceFile SourceFileForSpecifierGeneration,
	host ModuleSpecifierGenerationHost,
	userPreferences UserPreferences,
	options ModuleSpecifierOptions,
	forAutoImport bool,
)

Source from the content-addressed store, hash-verified

357}
358
359func computeModuleSpecifiers(
360 modulePaths []ModulePath,
361 compilerOptions *core.CompilerOptions,
362 importingSourceFile SourceFileForSpecifierGeneration,
363 host ModuleSpecifierGenerationHost,
364 userPreferences UserPreferences,
365 options ModuleSpecifierOptions,
366 forAutoImport bool,
367) ([]string, ResultKind) {
368 info := getInfo(importingSourceFile.FileName(), host)
369 preferences := getModuleSpecifierPreferences(userPreferences, host, compilerOptions, importingSourceFile, "")
370
371 var existingSpecifier string
372 for _, modulePath := range modulePaths {
373 targetPath := tspath.ToPath(modulePath.FileName, host.GetCurrentDirectory(), info.UseCaseSensitiveFileNames)
374 var existingImport *ast.StringLiteralLike
375 for _, importSpecifier := range importingSourceFile.Imports() {
376 resolvedModule := host.GetResolvedModuleFromModuleSpecifier(importingSourceFile, importSpecifier)
377 if resolvedModule.IsResolved() && tspath.ToPath(resolvedModule.ResolvedFileName, host.GetCurrentDirectory(), info.UseCaseSensitiveFileNames) == targetPath {
378 existingImport = importSpecifier
379 break
380 }
381 }
382 if existingImport != nil {
383 if preferences.relativePreference == RelativePreferenceNonRelative && tspath.PathIsRelative(existingImport.Text()) {
384 // If the preference is for non-relative and the module specifier is relative, ignore it
385 continue
386 }
387 existingMode := host.GetModeForUsageLocation(importingSourceFile, existingImport)
388 targetMode := options.OverrideImportMode
389 if targetMode == core.ResolutionModeNone {
390 targetMode = host.GetDefaultResolutionModeForFile(importingSourceFile)
391 }
392 if existingMode != targetMode && existingMode != core.ResolutionModeNone && targetMode != core.ResolutionModeNone {
393 // If the candidate import mode doesn't match the mode we're generating for, don't consider it
394 continue
395 }
396 existingSpecifier = existingImport.Text()
397 break
398 }
399 }
400
401 if existingSpecifier != "" {
402 return []string{existingSpecifier}, ResultKindNone
403 }
404
405 importedFileIsInNodeModules := core.Some(modulePaths, func(p ModulePath) bool { return p.IsInNodeModules })
406
407 // Module specifier priority:
408 // 1. "Bare package specifiers" (e.g. "@foo/bar") resulting from a path through node_modules to a package.json's "types" entry
409 // 2. Specifiers generated using "paths" from tsconfig
410 // 3. Non-relative specfiers resulting from a path through node_modules (e.g. "@foo/bar/path/to/file")
411 // 4. Relative paths
412 var pathsSpecifiers []string
413 var redirectPathsSpecifiers []string
414 var nodeModulesSpecifiers []string
415 var relativeSpecifiers []string
416

Callers 1

Calls 15

ToPathFunction · 0.92
PathIsRelativeFunction · 0.92
SomeFunction · 0.92
getInfoFunction · 0.85
lenFunction · 0.85
IsExcludedByRegexFunction · 0.85
getLocalModuleSpecifierFunction · 0.85
PathIsBareSpecifierFunction · 0.85
ContainsNodeModulesFunction · 0.85
FileNameMethod · 0.65

Tested by

no test coverage detected