Skip to content

Commit

Permalink
support function
Browse files Browse the repository at this point in the history
  • Loading branch information
apiel committed May 27, 2019
1 parent 657c34a commit 010ce2d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 3 additions & 1 deletion factory/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { UndefinedTypeNodeParser } from "../src/NodeParser/UndefinedTypeNodePars
import { UnionNodeParser } from "../src/NodeParser/UnionNodeParser";
import { SubNodeParser } from "../src/SubNodeParser";
import { TopRefNodeParser } from "../src/TopRefNodeParser";
import { FunctionDeclarationTypeNodeParser } from "../src/NodeParser/FunctionDeclarationTypeNodeParser";


export function createParser(program: ts.Program, config: Config): NodeParser {
Expand Down Expand Up @@ -107,7 +108,8 @@ export function createParser(program: ts.Program, config: Config): NodeParser {
new TypeLiteralNodeParser(withJsDoc(chainNodeParser)),
))))

.addNodeParser(new ArrayNodeParser(chainNodeParser));
.addNodeParser(new ArrayNodeParser(chainNodeParser))
.addNodeParser(new FunctionDeclarationTypeNodeParser(typeChecker, chainNodeParser));

return withTopRef(chainNodeParser);
}
40 changes: 40 additions & 0 deletions src/NodeParser/FunctionDeclarationTypeNodeParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as ts from "typescript";
import { Context, NodeParser } from "../NodeParser";
import { SubNodeParser } from "../SubNodeParser";
import { BaseType } from "../Type/BaseType";
import { ObjectProperty, ObjectType } from "../Type/ObjectType";
import { getKey } from "../Utils/nodeKey";

export class FunctionDeclarationTypeNodeParser implements SubNodeParser {
public constructor(
private typeChecker: ts.TypeChecker,
private childNodeParser: NodeParser,
) {
}

public supportsNode(node: ts.FunctionDeclaration): boolean {
return node.kind === ts.SyntaxKind.FunctionDeclaration;
}
public createType(node: ts.FunctionDeclaration, context: Context): BaseType {
return new ObjectType(
this.getTypeId(node, context),
[],
this.getParameters(node, context),
false,
);
}

private getParameters(node: ts.FunctionDeclaration, context: Context): ObjectProperty[] {
return node.parameters.map(paramNode => {
return new ObjectProperty(
paramNode.name.getText(),
this.childNodeParser.createType(paramNode.type!, context),
!paramNode.questionToken,
);
});
}

private getTypeId(node: ts.Node, context: Context): string {
return `function-${getKey(node, context)}`;
}
}
3 changes: 2 additions & 1 deletion src/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export class SchemaGenerator {
if (
node.kind === ts.SyntaxKind.InterfaceDeclaration ||
node.kind === ts.SyntaxKind.EnumDeclaration ||
node.kind === ts.SyntaxKind.TypeAliasDeclaration
node.kind === ts.SyntaxKind.TypeAliasDeclaration ||
node.kind === ts.SyntaxKind.FunctionDeclaration
) {
if (!this.isExportType(node)) {
return;
Expand Down

0 comments on commit 010ce2d

Please sign in to comment.