adding analyzis of callbacks etc.

This commit is contained in:
Martin Karkowski 2020-08-23 11:23:38 +02:00
parent 829a860c22
commit b5694723d2
3 changed files with 74 additions and 27 deletions

View File

@ -176,7 +176,7 @@ function _getType(node: Node, inputType: Type, text: string) {
if (result.length > 0) {
// Update the Imported Types.
typeImports = result.map(item => {
typeImports.push(... result.map(item => {
const text = item.toString();
// Regex, to extract the path of the Import.
@ -189,10 +189,24 @@ function _getType(node: Node, inputType: Type, text: string) {
path,
identifier
};
});
}));
} else if (Node.isFunctionDeclaration(node) || Node.isFunctionTypeNode(node) || Node.isFunctionLikeDeclaration(node)) {
baseType = "function"
baseType = "function";
// Iterate over the parameter
node.getParameters().map(parameter => {
const defintion = _getType(
parameter.getTypeNode(),
parameter.getType(),
parameter.getType().getText()
)
if (!defintion.isBaseType) {
// Add the Elements to the Import.
typeImports.push(...defintion.typeImports)
}
})
}
// Define the a Simplified Subtype.
@ -206,7 +220,8 @@ function _getType(node: Node, inputType: Type, text: string) {
const ret: TypeInformation = {
isBaseType: !!(inputType.compilerType as any).intrinsicName,
baseType,
originalCode
originalCode,
typeImports
}
// Add the Additional elements if required.
@ -297,18 +312,11 @@ export function getDescription(declaration: PropertyDeclaration | InterfaceDecla
typeImports,
}
// // Add the Additional elements if required.
// if (!ret.isBaseType) {
// ret.simplifiedType = simplifiedType;
// ret.simplifiedSubType = simplifiedSubType;
// ret.typeImports = typeImports;
// }
// // Return the Type.
// Return the Type.
return ret;
} else if (Node.isFunctionDeclaration(declaration)) {
} else if (Node.isParameterDeclaration(declaration)) {
//
} else if (Node.isMethodDeclaration(declaration)) {
const isAbstract = declaration.isAbstract();
const isAsync = declaration.isAsync();

View File

@ -51,23 +51,61 @@ export function transformClasses(sourceFiles: SourceFile[], options: {
}
}
let fileWithTypesForClass = ''
// Iterate over the Methods
for (const method of relevantClass.methods){
if (!method.returnType.isBaseType) {
for (const {identifier, path} of (method.returnType.typeImports || [])){
// Only if the import isnt the Base Type, add it to the List.
if (identifier !== options.propertyType) {
// Check if the Imported Type has been Adden multiple Times.
if (mappingTypeToImport[identifier] === undefined) {
mappingTypeToImport[identifier] = new Set<string>();
}
mappingTypeToImport[identifier].add(path);
requiredImports.add(identifier);
}
}
}
for (const parm of method.params){
if (!parm.isBaseType){
for (const {identifier, path} of (parm.typeImports || [])){
// Only if the import isnt the Base Type, add it to the List.
if (identifier !== options.propertyType) {
// Check if the Imported Type has been Adden multiple Times.
if (mappingTypeToImport[identifier] === undefined) {
mappingTypeToImport[identifier] = new Set<string>();
}
mappingTypeToImport[identifier].add(path);
requiredImports.add(identifier);
}
}
}
}
}
let fileWithTypesForClass = `// Automatic extracted Interfaces for the Class "` + relevantClass.className + `"\n// Generated on: ` + (new Date()).toISOString();
// List containing the already used Types
const importedBaseTypes = new Set<string>();
// Iterate over the Imports
for (const reqType of requiredImports) {
if (mappingTypeToImport[reqType] && mappingTypeToImport[reqType].size === 1) {
// Extract the Path of the File
const pathToFile = Array.from(mappingTypeToImport[reqType].values())[0] + '.ts';
const declarations = getDeclarationByName(fileMapping, pathToFile, reqType);
const declarations = getDeclarationByName(fileMapping, pathToFile, reqType).filter(item => !importedBaseTypes.has(item.baseType));
declarations.map(item => importedBaseTypes.add(item.baseType));
fileWithTypesForClass += declarations.map(item =>
item.originalCode
).reduce(
(prev, current, idx) =>
// If it is the First Element => Skipp the New Line Property idx === 0 ? current:
prev + '\n\n' + current,
`// Automatic extracted Interfaces for the Class "` + relevantClass.className + `"
// Generated on: ` + (new Date()).toISOString()
(prev, current, idx) => prev + '\n\n' + current, ''
);
} else if (mappingTypeToImport[reqType].size > 1) {

View File

@ -3,6 +3,10 @@ import { exportsElementsToDispatcher, exportMethodToDispatcher, exportPropertyTo
import { IF01 } from "./testExternalDescriptor";
import { IF02 } from "./testExternalDescriptorReference";
export interface IF00 {
hello: 'world'
}
exportsElementsToDispatcher('test-api')
export class CLWithInterface {
@exportPropertyToDispatcher
@ -10,18 +14,15 @@ export class CLWithInterface {
@exportPropertyToDispatcher
exportedAttributeComplex = new nopeObservable<{
element01: IF02,
element01: number,
num: number
}>({
element01: {
name: '',
num: 0
},
element01: 0,
num: 0
});
@exportMethodToDispatcher()
async exportedFunction(a: number, b: number, operator: (a: number,b: number) => Promise<number>){
async exportedFunction(a: number, b: number, operator: (a: IF00,b: IF01) => Promise<IF02>){
return await operator(a,b);
}