Skip to content

Commit

Permalink
Add function: parseSlots
Browse files Browse the repository at this point in the history
  • Loading branch information
hbjORbj committed Feb 10, 2023
1 parent 58b8b22 commit 032dc3b
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions packages/api-docs-builder/utils/parseSlots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as ts from 'typescript';
import {
getSymbolDescription,
getSymbolJSDocTags,
stringifySymbol,
} from '../ApiBuilders/HookApiBuilder';
import { TypeScriptProject } from './createTypeScriptProject';

export interface Slot {
name: string;
description: string;
typeStr: string;
defaultValue?: string;
}

export default function parseSlots({
project,
componentName,
}: {
project: TypeScriptProject;
componentName: string;
}): Slot[] {
// Generate the params
let result: Slot[] = [];
const interfaceName = `${componentName}Slots`;

try {
const exportedSymbol = project.exports[interfaceName];
const type = project.checker.getDeclaredTypeOfSymbol(exportedSymbol);
// @ts-ignore
const typeDeclaration = type?.symbol?.declarations?.[0];
if (!typeDeclaration || !ts.isInterfaceDeclaration(typeDeclaration)) {
return [];
}

const slots: Record<string, Slot> = {};
// @ts-ignore
const propertiesOnProject = type.getProperties();

// @ts-ignore
propertiesOnProject.forEach((propertySymbol) => {
const tags = getSymbolJSDocTags(propertySymbol);
if (tags.ignore) {
return;
}

slots[propertySymbol.name] = {
name: propertySymbol.name,
description: getSymbolDescription(propertySymbol, project),
defaultValue: tags.default?.text?.[0].text,
typeStr: stringifySymbol(propertySymbol, project),
};
});

result = Object.values(slots).sort((a, b) => a.name.localeCompare(b.name));
} catch (e) {
console.error(`No declaration for ${interfaceName}`);
}

return result;
}

0 comments on commit 032dc3b

Please sign in to comment.