MCPcopy Create free account
hub / github.com/microsoft/typescript-go / ProcessEntrypointEnding

Function ProcessEntrypointEnding

internal/modulespecifiers/util.go:400–502  ·  view source on GitHub ↗

ProcessEntrypointEnding processes a pre-computed module specifier from a package.json exports entrypoint according to the entrypoint's Ending type and the user's preferred endings.

(
	entrypoint *module.ResolvedEntrypoint,
	prefs UserPreferences,
	host ModuleSpecifierGenerationHost,
	options *core.CompilerOptions,
	importingSourceFile SourceFileForSpecifierGeneration,
	allowedEndings []ModuleSpecifierEnding,
)

Source from the content-addressed store, hash-verified

398// ProcessEntrypointEnding processes a pre-computed module specifier from a package.json exports
399// entrypoint according to the entrypoint's Ending type and the user's preferred endings.
400func ProcessEntrypointEnding(
401 entrypoint *module.ResolvedEntrypoint,
402 prefs UserPreferences,
403 host ModuleSpecifierGenerationHost,
404 options *core.CompilerOptions,
405 importingSourceFile SourceFileForSpecifierGeneration,
406 allowedEndings []ModuleSpecifierEnding,
407) string {
408 specifier := entrypoint.ModuleSpecifier
409 if entrypoint.Ending == module.EndingFixed {
410 return specifier
411 }
412
413 if len(allowedEndings) == 0 {
414 allowedEndings = GetAllowedEndingsInPreferredOrder(
415 prefs,
416 host,
417 options,
418 importingSourceFile,
419 "",
420 host.GetDefaultResolutionModeForFile(importingSourceFile),
421 )
422 }
423
424 preferredEnding := allowedEndings[0]
425
426 // Handle declaration file extensions
427 dtsExtension := tspath.GetDeclarationFileExtension(specifier)
428 if dtsExtension != "" {
429 switch preferredEnding {
430 case ModuleSpecifierEndingTsExtension, ModuleSpecifierEndingJsExtension:
431 // Map .d.ts -> .js, .d.mts -> .mjs, .d.cts -> .cjs
432 jsExtension := GetJSExtensionForDeclarationFileExtension(dtsExtension)
433 return tspath.ChangeAnyExtension(specifier, jsExtension, []string{dtsExtension}, false)
434 case ModuleSpecifierEndingMinimal, ModuleSpecifierEndingIndex:
435 if entrypoint.Ending == module.EndingChangeable {
436 // .d.mts/.d.cts must keep an extension; rewrite to .mjs/.cjs instead of dropping
437 if dtsExtension == tspath.ExtensionDts {
438 specifier = tspath.RemoveExtension(specifier, dtsExtension)
439 if preferredEnding == ModuleSpecifierEndingMinimal {
440 specifier = strings.TrimSuffix(specifier, "/index")
441 }
442 return specifier
443 }
444 jsExtension := GetJSExtensionForDeclarationFileExtension(dtsExtension)
445 return tspath.ChangeAnyExtension(specifier, jsExtension, []string{dtsExtension}, false)
446 }
447 // EndingExtensionChangeable - can only change extension, not remove it
448 jsExtension := GetJSExtensionForDeclarationFileExtension(dtsExtension)
449 return tspath.ChangeAnyExtension(specifier, jsExtension, []string{dtsExtension}, false)
450 }
451 return specifier
452 }
453
454 // Handle .ts/.tsx/.mts/.cts extensions
455 if tspath.FileExtensionIsOneOf(specifier, []string{tspath.ExtensionTs, tspath.ExtensionTsx, tspath.ExtensionMts, tspath.ExtensionCts}) {
456 switch preferredEnding {
457 case ModuleSpecifierEndingTsExtension:

Callers 1

GetModuleSpecifierMethod · 0.92

Calls 10

ChangeAnyExtensionFunction · 0.92
RemoveExtensionFunction · 0.92
FileExtensionIsOneOfFunction · 0.92
TryGetJSExtensionForFileFunction · 0.92
RemoveFileExtensionFunction · 0.92
lenFunction · 0.85

Tested by

no test coverage detected