From 40605f2e28388c443f2c0056959c97b6ddceff06 Mon Sep 17 00:00:00 2001 From: Edoardo Scibona <12040076+velut@users.noreply.github.com> Date: Thu, 18 Jan 2024 21:20:58 +0100 Subject: [PATCH] feat: add ExtractedContainerDeclaration type --- src/container-declarations.ts | 57 +++++++++++++++++++++++------------ src/extract-file-module.ts | 4 +-- src/extract-namespace.ts | 7 ++--- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/container-declarations.ts b/src/container-declarations.ts index e4a38a52..5c921c91 100644 --- a/src/container-declarations.ts +++ b/src/container-declarations.ts @@ -9,16 +9,19 @@ import { import { ambientModulesDeclarations } from "./ambient-modules-declarations"; import { exportEqualsDeclarations } from "./export-equals-declarations"; import { exportedDeclarations } from "./exported-declarations"; -import { extractClass } from "./extract-class"; -import { extractEnum } from "./extract-enum"; +import { extractClass, type ExtractedClass } from "./extract-class"; +import { extractEnum, type ExtractedEnum } from "./extract-enum"; import { extractExpression } from "./extract-expression"; import { extractFileModule } from "./extract-file-module"; -import { extractFunction } from "./extract-function"; +import { extractFunction, type ExtractedFunction } from "./extract-function"; import { extractFunctionExpression } from "./extract-function-expression"; -import { extractInterface } from "./extract-interface"; -import { extractNamespace } from "./extract-namespace"; -import { extractTypeAlias } from "./extract-type-alias"; -import { extractVariable } from "./extract-variable"; +import { extractInterface, type ExtractedInterface } from "./extract-interface"; +import { extractNamespace, type ExtractedNamespace } from "./extract-namespace"; +import { + extractTypeAlias, + type ExtractedTypeAlias, +} from "./extract-type-alias"; +import { extractVariable, type ExtractedVariable } from "./extract-variable"; import { extractVariableAssignmentExpression } from "./extract-variable-assignment-expression"; import { globalAmbientDeclarations } from "./global-ambient-declarations"; import { isClass } from "./is-class"; @@ -41,13 +44,25 @@ export type ContainerDeclarationsOptions = { extractAmbientModules?: boolean; }; +export type ExtractedContainerDeclaration = + | ExtractedVariable + | ExtractedFunction + | ExtractedClass + | ExtractedInterface + | ExtractedEnum + | ExtractedTypeAlias + | ExtractedNamespace; + +export type ExtractedContainerDeclarationKind = + ExtractedContainerDeclaration["kind"]; + export const containerDeclarations = async ({ project, container, containerName, maxDepth, extractAmbientModules = false, -}: ContainerDeclarationsOptions) => { +}: ContainerDeclarationsOptions): Promise => { const foundDeclarations = [ ...exportedDeclarations(containerName, container), ...exportEqualsDeclarations(containerName, container), @@ -60,24 +75,26 @@ export const containerDeclarations = async ({ ]; const containerDeclarations = []; for (const { containerName, exportName, declaration } of foundDeclarations) { - containerDeclarations.push( - await extractDeclaration( - containerName, - exportName, - declaration, - maxDepth, - ), + const extractedDeclaration = await extractDeclaration( + containerName, + exportName, + declaration, + maxDepth, ); + if (!extractedDeclaration) { + continue; + } + containerDeclarations.push(extractedDeclaration); } return orderBy(containerDeclarations, "id"); }; -const extractDeclaration = ( +const extractDeclaration = async ( containerName: string, exportName: string, declaration: ExportedDeclarations, maxDepth: number, -) => { +): Promise => { if (isVariable(declaration)) { return extractVariable(containerName, exportName, declaration); } @@ -112,7 +129,7 @@ const extractDeclaration = ( if (isNamespace(declaration) && maxDepth > 0) { // TODO: // Skip merged or nested namespace declarations // TODO: extract inner declarations - const innerDeclarations: unknown[] = []; + const innerDeclarations: ExtractedContainerDeclaration[] = []; return extractNamespace( containerName, exportName, @@ -124,7 +141,7 @@ const extractDeclaration = ( // From `import * as ns from module; export { ns };` // or from `export * as ns from module`. // TODO: extract inner declarations - const innerDeclarations: unknown[] = []; + const innerDeclarations: ExtractedContainerDeclaration[] = []; return extractFileModule( containerName, exportName, @@ -132,5 +149,5 @@ const extractDeclaration = ( innerDeclarations, ); } - return { id: "TODO:" }; + return undefined; }; diff --git a/src/extract-file-module.ts b/src/extract-file-module.ts index 1cde56f9..9431e755 100644 --- a/src/extract-file-module.ts +++ b/src/extract-file-module.ts @@ -1,4 +1,5 @@ import { SourceFile, SyntaxKind } from "ts-morph"; +import type { ExtractedContainerDeclaration } from "./container-declarations"; import type { ExtractedNamespace } from "./extract-namespace"; import { formatSignature } from "./format-signature"; import { id } from "./id"; @@ -8,8 +9,7 @@ export const extractFileModule = async ( containerName: string, exportName: string, declaration: SourceFile, - // TODO: - declarations: unknown[], + declarations: ExtractedContainerDeclaration[], ): Promise => ({ kind: "namespace", id: id(containerName, "namespace", exportName), diff --git a/src/extract-namespace.ts b/src/extract-namespace.ts index dbcff9fe..dbc2b849 100644 --- a/src/extract-namespace.ts +++ b/src/extract-namespace.ts @@ -1,4 +1,5 @@ import { ModuleDeclaration } from "ts-morph"; +import type { ExtractedContainerDeclaration } from "./container-declarations"; import { docs } from "./docs"; import { formatSignature } from "./format-signature"; import { headText } from "./head-text"; @@ -13,16 +14,14 @@ export type ExtractedNamespace = { file: string; line: number; signature: string; - // TODO: - declarations: unknown[]; + declarations: ExtractedContainerDeclaration[]; }; export const extractNamespace = async ( containerName: string, exportName: string, declaration: ModuleDeclaration, - // TODO: - declarations: unknown[], + declarations: ExtractedContainerDeclaration[], ): Promise => ({ kind: "namespace", id: id(containerName, "namespace", exportName),