Skip to content

Commit

Permalink
feat: add ExtractedContainerDeclaration type
Browse files Browse the repository at this point in the history
  • Loading branch information
velut committed Jan 18, 2024
1 parent 2b0611a commit 40605f2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
57 changes: 37 additions & 20 deletions src/container-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<ExtractedContainerDeclaration[]> => {
const foundDeclarations = [
...exportedDeclarations(containerName, container),
...exportEqualsDeclarations(containerName, container),
Expand All @@ -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<ExtractedContainerDeclaration | undefined> => {
if (isVariable(declaration)) {
return extractVariable(containerName, exportName, declaration);
}
Expand Down Expand Up @@ -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,
Expand All @@ -124,13 +141,13 @@ 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,
declaration,
innerDeclarations,
);
}
return { id: "TODO:" };
return undefined;
};
4 changes: 2 additions & 2 deletions src/extract-file-module.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -8,8 +9,7 @@ export const extractFileModule = async (
containerName: string,
exportName: string,
declaration: SourceFile,
// TODO:
declarations: unknown[],
declarations: ExtractedContainerDeclaration[],
): Promise<ExtractedNamespace> => ({
kind: "namespace",
id: id(containerName, "namespace", exportName),
Expand Down
7 changes: 3 additions & 4 deletions src/extract-namespace.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<ExtractedNamespace> => ({
kind: "namespace",
id: id(containerName, "namespace", exportName),
Expand Down

0 comments on commit 40605f2

Please sign in to comment.