MCPcopy Create free account
hub / github.com/Effect-TS/effect / resolveBarrelImport

Function resolveBarrelImport

packages/tools/jsdocs/src/Jsdocs.ts:744–790  ·  view source on GitHub ↗
(
  metadata: PackageMetadata,
  filename: string
)

Source from the content-addressed store, hash-verified

742}
743
744function resolveBarrelImport(
745 metadata: PackageMetadata,
746 filename: string
747): Result<{ readonly barrel: ParsedJSDocBarrelImport | null; readonly flatNames: ReadonlyArray<string> }, string> {
748 let directory = path.dirname(filename)
749 while (isPathInside(metadata.sourceRoot, directory)) {
750 const indexPath = path.join(directory, "index.ts")
751 if (indexPath !== filename && fs.existsSync(indexPath)) {
752 const expectedExport = `./${normalizePathName(path.relative(directory, filename))}`
753 const parsed = parseBarrelExports(indexPath)
754 if (parsed._tag === "Failure") {
755 return parsed
756 }
757 const barrelSubpath = normalizePathName(path.relative(metadata.sourceRoot, directory))
758 const exportSubpath = barrelSubpath === "" ? "." : `./${barrelSubpath}`
759 const exportTarget = barrelSubpath === "" ? "./src/index.ts" : `./src/${barrelSubpath}/index.ts`
760 if (isPackageSubpathExported(metadata.exports, exportSubpath, exportTarget)) {
761 const moduleSpecifier = packageSpecifier(metadata.name, barrelSubpath)
762 const flatNames = Array.from(parsed.value.named.get(expectedExport) ?? [])
763 const namespace = parsed.value.namespace.get(expectedExport)
764 if (namespace !== undefined) {
765 return {
766 _tag: "Success",
767 value: { barrel: { type: "namespace", module: moduleSpecifier, name: namespace }, flatNames }
768 }
769 }
770 if (parsed.value.flat.has(expectedExport)) {
771 return {
772 _tag: "Success",
773 value: { barrel: { type: "flat", module: moduleSpecifier }, flatNames }
774 }
775 }
776 if (flatNames.length > 0) {
777 return {
778 _tag: "Success",
779 value: { barrel: { type: "flat", module: moduleSpecifier }, flatNames }
780 }
781 }
782 }
783 }
784 if (directory === metadata.sourceRoot) {
785 break
786 }
787 directory = path.dirname(directory)
788 }
789 return { _tag: "Success", value: { barrel: null, flatNames: [] } }
790}
791
792function resolveJSDocImports(
793 cwd: string,

Callers 1

resolveJSDocImportsFunction · 0.85

Calls 9

isPathInsideFunction · 0.85
parseBarrelExportsFunction · 0.85
isPackageSubpathExportedFunction · 0.85
packageSpecifierFunction · 0.85
fromMethod · 0.80
normalizePathNameFunction · 0.70
getMethod · 0.65
hasMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected