Skip to content

Commit

Permalink
chore: map instead of foreach
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Apr 10, 2024
1 parent f1a824c commit b366313
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
44 changes: 21 additions & 23 deletions src/lib/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
ArrowFunction,
CompilerOptions,
Declaration,
EnumDeclaration,
FunctionDeclaration,
Node,
PropertyName,
Expand All @@ -11,8 +12,7 @@ import type {
TypeChecker,
Symbol as TypeScriptSymbol,
VariableDeclaration,
VariableStatement,
EnumDeclaration,
VariableStatement
} from 'typescript';
import {
ModifierFlags,
Expand All @@ -26,15 +26,15 @@ import {
getCombinedModifierFlags,
isArrowFunction,
isClassDeclaration,
isEnumDeclaration,
isFunctionDeclaration,
isIdentifier,
isInterfaceDeclaration,
isMethodDeclaration,
isModuleDeclaration,
isPropertySignature,
isTypeAliasDeclaration,
isVariableStatement,
isEnumDeclaration,
isVariableStatement
} from 'typescript';
import type {BuildOptions, DocEntry, DocEntryConstructor, DocEntryType} from './types';

Expand All @@ -59,36 +59,34 @@ const serializeSymbol = ({

const serializeEnum = ({
checker,
symbol,
symbol
}: {
checker: TypeChecker;
symbol: TypeScriptSymbol;
doc_type?: DocEntryType;
}): DocEntry => {
const node = symbol.valueDeclaration as EnumDeclaration
const properties: DocEntry[] = []
node.members.forEach(member=>{
const type = member.initializer?.getText()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const documentation = displayPartsToString(member.symbol.getDocumentationComment(checker))
const property: DocEntry = {
const {members} = symbol.valueDeclaration as EnumDeclaration;

const properties: DocEntry[] = members.map((member) => {
const documentation = displayPartsToString(
(member as unknown as {symbol: TypeScriptSymbol}).symbol.getDocumentationComment(checker)
);

const type = member.initializer?.getText();

return {
name: member.name.getText(),
}
if (type){
property.type = type
}
if (documentation){
property.documentation = documentation
}
properties.push(property)
})
...(type !== undefined && {type}),
...(documentation !== undefined && documentation !== '' && {documentation})
};
});

return {
name: symbol.getName(),
documentation: displayPartsToString(symbol.getDocumentationComment(checker)),
properties,
jsDocs: symbol.getJsDocTags(),
doc_type: 'enum',
doc_type: 'enum'
};
};

Expand Down
9 changes: 8 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type {CompilerOptions, JSDocTagInfo} from 'typescript';

export type DocEntryType = 'function' | 'method' | 'class' | 'const' | 'interface' | 'type' | 'enum';
export type DocEntryType =
| 'function'
| 'method'
| 'class'
| 'const'
| 'interface'
| 'type'
| 'enum';

export type DocEntryConstructor = Pick<DocEntry, 'parameters' | 'returnType' | 'documentation'> & {
visibility: 'private' | 'public';
Expand Down

0 comments on commit b366313

Please sign in to comment.