import type { ComponentContext } from "@alloy-js/core"; import { SourceFile as CoreSourceFile, createNamedContext, createScope, Scope, Show, SourceDirectoryContext, useContext, useScope, type Children, } from "@alloy-js/core"; import { join } from "pathe"; import { getSourceDirectoryData } from "../source-directory-data.js"; import { TSModuleScope } from "../symbols/index.js"; import { ImportStatements } from "./ImportStatement.js"; import { PackageContext } from "./PackageDirectory.js"; import { Reference } from "./Reference.js"; import { SingleLineCommentBlock } from "./SingleLineCommentBlock.jsx"; export interface SourceFileContext { scope: TSModuleScope; } export const SourceFileContext: ComponentContext = createNamedContext("@alloy-js/typescript SourceFile"); export function useSourceFile() { return useContext(SourceFileContext)!; } export interface SourceFileProps { path: string; children?: Children; header?: Children; headerComment?: string; export?: boolean | string; } export function SourceFile(props: SourceFileProps) { const directoryContext = useContext(SourceDirectoryContext)!; const sdData = getSourceDirectoryData(directoryContext); const currentDir = directoryContext.path; const path: string = join(currentDir, props.path); const parent = useScope(); const scope = createScope(TSModuleScope, path, parent, { binder: parent?.binder, }); sdData.modules.add(scope); const pkg = useContext(PackageContext); if (pkg) { pkg.scope.addModule(scope); } const sfContext: SourceFileContext = { scope, }; if (props.export) { if (pkg) { if (typeof props.export === "boolean") { pkg.scope.addExport(path, scope); } else { pkg.scope.addExport(props.export, scope); } } } const header = props.header || props.headerComment ? ( ) : undefined; return ( 0}> {props.children} ); } export interface SourceFileHeaderProps { header?: Children; headerComment?: string; } function SourceFileHeader(props: SourceFileHeaderProps) { return ( <> {props.headerComment} {props.header} ); }