From a59b3d3e135ab5ab39e87797a6b3dc2915219fc2 Mon Sep 17 00:00:00 2001 From: atanasster Date: Fri, 16 Jul 2021 00:11:45 +0300 Subject: [PATCH] chore: move declaration modifiers --- misc/structured-types/src/SymbolParser.ts | 23 +++--------------- misc/structured-types/src/ts-utils.ts | 29 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/misc/structured-types/src/SymbolParser.ts b/misc/structured-types/src/SymbolParser.ts index c2fffe857..458b24c8b 100644 --- a/misc/structured-types/src/SymbolParser.ts +++ b/misc/structured-types/src/SymbolParser.ts @@ -38,6 +38,7 @@ import { ISymbolParser, getInitializer, typeKind, + declarationModifiers, } from './ts-utils'; import { cleanJSDocText, @@ -510,26 +511,8 @@ export class SymbolParser implements ISymbolParser { const declaration = symbolDeclaration; - if ((declaration as ts.ParameterDeclaration).questionToken) { - prop.optional = true; - } - if (declaration?.modifiers) { - for (const m of declaration.modifiers) { - if (m.kind === ts.SyntaxKind.PrivateKeyword) { - prop.visibility = 'private'; - } else if (m.kind === ts.SyntaxKind.ProtectedKeyword) { - prop.visibility = 'protected'; - } else if (m.kind === ts.SyntaxKind.PublicKeyword) { - prop.visibility = 'public'; - } else if (m.kind === ts.SyntaxKind.StaticKeyword) { - prop.static = true; - } else if (m.kind === ts.SyntaxKind.ReadonlyKeyword) { - prop.readonly = true; - } else if (m.kind === ts.SyntaxKind.AbstractKeyword) { - prop.abstract = true; - } - } - } + declarationModifiers(prop, declaration); + if (declaration) { prop.displayName = getDeclarationName(declaration); } diff --git a/misc/structured-types/src/ts-utils.ts b/misc/structured-types/src/ts-utils.ts index b809ef84e..cc7e2e4db 100644 --- a/misc/structured-types/src/ts-utils.ts +++ b/misc/structured-types/src/ts-utils.ts @@ -309,6 +309,35 @@ export const getInitializer = ( type NodeCallback = (m: ts.PropertyDeclaration) => boolean; type NodeFind = (callback: NodeCallback) => ts.PropertyDeclaration | undefined; +export const declarationModifiers = ( + prop: PropType, + declaration?: ts.Declaration, +): PropType => { + if (declaration) { + if ((declaration as ts.ParameterDeclaration).questionToken) { + prop.optional = true; + } + if (declaration.modifiers) { + for (const m of declaration.modifiers) { + if (m.kind === ts.SyntaxKind.PrivateKeyword) { + prop.visibility = 'private'; + } else if (m.kind === ts.SyntaxKind.ProtectedKeyword) { + prop.visibility = 'protected'; + } else if (m.kind === ts.SyntaxKind.PublicKeyword) { + prop.visibility = 'public'; + } else if (m.kind === ts.SyntaxKind.StaticKeyword) { + prop.static = true; + } else if (m.kind === ts.SyntaxKind.ReadonlyKeyword) { + prop.readonly = true; + } else if (m.kind === ts.SyntaxKind.AbstractKeyword) { + prop.abstract = true; + } + } + } + } + return prop; +}; + const findFileNode = (node: ts.Node): ts.SourceFile | undefined => { if (ts.isSourceFile(node)) { return node;