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

Function processEnding

internal/modulespecifiers/specifiers.go:636–705  ·  view source on GitHub ↗
(
	fileName string,
	allowedEndings []ModuleSpecifierEnding,
	options *core.CompilerOptions,
	host ModuleSpecifierGenerationHost,
)

Source from the content-addressed store, hash-verified

634}
635
636func processEnding(
637 fileName string,
638 allowedEndings []ModuleSpecifierEnding,
639 options *core.CompilerOptions,
640 host ModuleSpecifierGenerationHost,
641) string {
642 if tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionJson, tspath.ExtensionMjs, tspath.ExtensionCjs}) {
643 return fileName
644 }
645
646 noExtension := tspath.RemoveFileExtension(fileName)
647 if fileName == noExtension {
648 return fileName
649 }
650
651 jsPriority := slices.Index(allowedEndings, ModuleSpecifierEndingJsExtension)
652 tsPriority := slices.Index(allowedEndings, ModuleSpecifierEndingTsExtension)
653 if tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionMts, tspath.ExtensionCts}) && tsPriority != -1 && tsPriority < jsPriority {
654 return fileName
655 }
656 if tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionDmts, tspath.ExtensionDcts}) {
657 inputExt := tspath.GetDeclarationFileExtension(fileName)
658 ext := GetJSExtensionForDeclarationFileExtension(inputExt)
659 return tspath.RemoveExtension(fileName, inputExt) + ext
660 }
661 if tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionMts, tspath.ExtensionCts}) {
662 return noExtension + getJSExtensionForFile(fileName, options)
663 }
664 if !tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionDts}) && tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionTs}) && strings.Contains(fileName, ".d.") {
665 // `foo.d.json.ts` and the like - remap back to `foo.json`
666 if result := TryGetRealFileNameForNonJSDeclarationFileName(fileName); result != "" {
667 return result
668 }
669 }
670
671 switch allowedEndings[0] {
672 case ModuleSpecifierEndingMinimal:
673 withoutIndex := strings.TrimSuffix(noExtension, "/index")
674 if host != nil && withoutIndex != noExtension && tryGetAnyFileFromPath(host, withoutIndex) {
675 // Can't remove index if there's a file by the same name as the directory.
676 // Probably more callers should pass `host` so we can determine this?
677 return noExtension
678 }
679 return withoutIndex
680 case ModuleSpecifierEndingIndex:
681 return noExtension
682 case ModuleSpecifierEndingJsExtension:
683 return noExtension + getJSExtensionForFile(fileName, options)
684 case ModuleSpecifierEndingTsExtension:
685 // For now, we don't know if this import is going to be type-only, which means we don't
686 // know if a .d.ts extension is valid, so use no extension or a .js extension
687 if tspath.IsDeclarationFileName(fileName) {
688 extensionlessPriority := -1
689 for i, e := range allowedEndings {
690 if e == ModuleSpecifierEndingMinimal || e == ModuleSpecifierEndingIndex {
691 extensionlessPriority = i
692 break
693 }

Callers 5

getLocalModuleSpecifierFunction · 0.85
validateEndingFunction · 0.85

Calls 11

FileExtensionIsOneOfFunction · 0.92
RemoveFileExtensionFunction · 0.92
RemoveExtensionFunction · 0.92
IsDeclarationFileNameFunction · 0.92
AssertNeverFunction · 0.92
getJSExtensionForFileFunction · 0.85
tryGetAnyFileFromPathFunction · 0.85
ContainsMethod · 0.65

Tested by

no test coverage detected