From c780c792111926cd0f50d7fd3519e493ba95245f Mon Sep 17 00:00:00 2001 From: Martin Karkowski Date: Sat, 22 Aug 2020 00:10:23 +0200 Subject: [PATCH] adding types --- lib/helpers/analyzeTypescriptFiles.ts | 99 +++++++++++++++++++-------- 1 file changed, 71 insertions(+), 28 deletions(-) diff --git a/lib/helpers/analyzeTypescriptFiles.ts b/lib/helpers/analyzeTypescriptFiles.ts index 0b83d53..fb72eb0 100644 --- a/lib/helpers/analyzeTypescriptFiles.ts +++ b/lib/helpers/analyzeTypescriptFiles.ts @@ -1,5 +1,45 @@ import { ClassDeclaration, Decorator, MethodDeclaration, Node, Project, PropertyDeclaration, SourceFile, Type } from "ts-morph"; +export type TypeInformation = { + isBaseType: boolean; + baseType: string; + simplifiedType?: string; + simplifiedSubType?: string; + typeImports?: { + path: string; + type: string; + }[]; + originalCode: string; +} + +export type ModifierInformation = { + declaration: MethodDeclaration | PropertyDeclaration, + modifiers: string[], + isPublic: boolean, + isPrivate: boolean, + isProtected: boolean, + isReadonly: boolean, +} + +export type ParameterInformation = ({ + name: string; + originalCode: string; + index: number; +} & TypeInformation); + +export type MethodInformation = { + declaration: MethodDeclaration; + params: ParameterInformation[]; + isAbstract: boolean; + name: string; + isAsync: boolean; + isGenerator: boolean; + isImplementation: boolean; + returnType: TypeInformation; +} + +export type PropertyInformation = ModifierInformation & TypeInformation & { name: string; declaration: PropertyDeclaration; }; + /** * Helperfunction, used to extract a Mapping of imported Files. This allows the user to rename the interfaces * to custom names. this will then be mapped to the original name @@ -48,8 +88,6 @@ export function getImportsOfFile(file: SourceFile) { } - - /** * Function to test if a class (cl) implements a specific interface named ifName. * @param cl The Class @@ -163,14 +201,7 @@ export function getType(node: Node, inputType: Type, text: string) { } // Define a Partial element of the Return-Value. - const ret: { - isBaseType: boolean, - baseType: string, - simplifiedType?: string, - simplifiedSubType?: string, - typeImports?: { path: string, type: string }[], - originalCode: string, - } = { + const ret: TypeInformation = { isBaseType: !!(inputType.compilerType as any).intrinsicName, baseType, originalCode: inputType.getText() @@ -206,7 +237,7 @@ export function isPropOfType(prop: PropertyDeclaration, reqType: string, caseSen * Function to generate a Property description. * @param prop The Property Declaration. */ -export function getPropertyDescription(prop: PropertyDeclaration) { +export function getPropertyDescription(prop: PropertyDeclaration): PropertyInformation { return Object.assign( // Use the Modifiers @@ -228,7 +259,13 @@ export function getPropertyDescription(prop: PropertyDeclaration) { ); } -export function getMatchingProperties(cl: ClassDeclaration, reqType: string, caseSensitive = true) { +/** + * Function to extract the Matching Properts + * @param cl The Class + * @param reqType The requested Type of the Element + * @param caseSensitive A Flage to use casesesitivy during the checkoup + */ +export function getMatchingProperties(cl: ClassDeclaration, reqType: string, caseSensitive = true): PropertyInformation[] { return cl.getProperties() // Firstly Filter the Properties, that they match the requested Type. .filter(prop => isPropOfType( @@ -314,9 +351,13 @@ export function isPropertyInjectedWith(prop: PropertyDeclaration, decorator: str return decorators.decorators.length > 0; } -export function getMethodInfo(declaration: MethodDeclaration) { +/** + * Helper Function to extract the Information of a Method. + * @param declaration Declartion of the Method + */ +export function getMethodInfo(declaration: MethodDeclaration): MethodInformation { - const abstract = declaration.isAbstract(); + const isAbstract = declaration.isAbstract(); const isAsync = declaration.isAsync(); const name = declaration.getName(); const isGenerator = declaration.isGenerator(); @@ -330,7 +371,7 @@ export function getMethodInfo(declaration: MethodDeclaration) { retTypeObj.getText() ) // Extract the Parameters of the Function - const params = declaration.getParameters().map((parameter, index) => { + const params: ParameterInformation[] = declaration.getParameters().map((parameter, index) => { return Object.assign( { // Name of the parameter @@ -351,7 +392,7 @@ export function getMethodInfo(declaration: MethodDeclaration) { return { declaration, params, - abstract, + isAbstract, name, isAsync, isGenerator, @@ -363,7 +404,7 @@ export function getMethodInfo(declaration: MethodDeclaration) { /** * Function to extrac the Modifiers of a declaration. - * @param declaration + * @param declaration The Declartion of the Class */ export function getModifiers(declaration: MethodDeclaration | PropertyDeclaration) { @@ -397,14 +438,16 @@ export function getModifiers(declaration: MethodDeclaration | PropertyDeclaratio modifiers.push('public') } - return { + const ret: ModifierInformation = { declaration, modifiers, isPublic: modifiers.includes('public'), isPrivate: modifiers.includes('private'), isProtected: modifiers.includes('protected'), isReadonly: modifiers.includes('readonly'), - } + }; + + return ret; } /** @@ -416,16 +459,16 @@ export function getModifiers(declaration: MethodDeclaration | PropertyDeclaratio * @param propertyType The requrired Type for the Property * @param propertyDecorator The Decorator for the Property */ -export function analyzeClasses(sources: SourceFile[], classDecorator: string, classInterface: string, methodDecorator: string, propertyType: string, propertyDecorator: string,){ +export function analyzeClasses(sources: SourceFile[], classDecorator: string, classInterface: string, methodDecorator: string, propertyType: string, propertyDecorator: string,) { const ret: { - className: string, - methods: any[], - properties: any[] + className: string; + methods: MethodInformation[]; + properties: PropertyInformation[]; }[] = []; // Iterate over the Files: - for (const file of sources){ + for (const file of sources) { // For Each File => Analyze the imported Files. // Create a Mapping File for the Improts. const importMapping = getImportsOfFile(file); @@ -433,12 +476,12 @@ export function analyzeClasses(sources: SourceFile[], classDecorator: string, cl // After all Imports has been detected => filter for all Classes that implement the provided classDecorator const relevantClasses = file.getClasses().filter(cl => { return ( - classInterface === '' || + classInterface === '' || isClassImplementingInterface(cl, classInterface, importMapping.aliasToOriginal) ) && ( - classDecorator === '' || - getDecorators(cl, classDecorator, importMapping.aliasToOriginal) - ) + classDecorator === '' || + getDecorators(cl, classDecorator, importMapping.aliasToOriginal) + ) }); // Now after each class is known => ierate over the relevant classes