forked from vega/ts-json-schema-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters