Skip to content

Commit

Permalink
TypeScript interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
rrdelaney committed Mar 25, 2018
1 parent 8338e63 commit 5046d9d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
57 changes: 57 additions & 0 deletions bindings/typescript.re
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,26 @@ and functionDeclaration = {
parameters: array(node),
type_: node
}
and interfaceDeclaration = {
pos: int,
end_: int,
modifiers: array(node),
name: node,
typeParameters: array(node),
members: array(node)
}
and propertySignature = {
pos: int,
end_: int,
modifiers: array(node),
name: node,
questionToken: option(node),
type_: node
}
and questionToken = {
pos: int,
end_: int
}
and sourceFile = {
pos: int,
end_: int,
Expand Down Expand Up @@ -668,6 +688,9 @@ and node =
| NumberKeyword(keyword)
| Identifier(identifier)
| FunctionDeclaration(functionDeclaration)
| InterfaceDeclaration(interfaceDeclaration)
| PropertySignature(propertySignature)
| QuestionToken(questionToken)
| SourceFile(sourceFile)
| Parameter(parameter)
| Unknown(int);
Expand All @@ -688,6 +711,9 @@ module Decoder = {
(Internal.SyntaxKind.stringKeyword, stringKeyword),
(Internal.SyntaxKind.identifier, identifier),
(Internal.SyntaxKind.functionDeclaration, functionDeclaration),
(Internal.SyntaxKind.interfaceDeclaration, interfaceDeclaration),
(Internal.SyntaxKind.propertySignature, propertySignature),
(Internal.SyntaxKind.questionToken, questionToken),
(Internal.SyntaxKind.sourceFile, sourceFile),
(Internal.SyntaxKind.parameter, parameter)
]
Expand Down Expand Up @@ -749,6 +775,37 @@ module Decoder = {
type_: json |> field("type", node)
}
)
and interfaceDeclaration = json =>
InterfaceDeclaration(
Json.Decode.{
pos: json |> field("pos", int),
end_: json |> field("end", int),
modifiers: json |> withDefault([||], field("modifiers", array(node))),
name: json |> field("name", node),
members: json |> field("members", array(node)),
typeParameters:
json |> withDefault([||], field("typeParameters", array(node)))
}
)
and propertySignature = json =>
PropertySignature(
Json.Decode.{
pos: json |> field("pos", int),
end_: json |> field("end", int),
modifiers: json |> withDefault([||], field("modifiers", array(node))),
name: json |> field("name", node),
type_: json |> field("type", node),
questionToken:
json |> field("questionToken", Json.Decode.optional(node))
}
)
and questionToken = json =>
QuestionToken(
Json.Decode.{
pos: json |> field("pos", int),
end_: json |> field("end", int)
}
)
and sourceFile = json =>
SourceFile(
Json.Decode.{
Expand Down
28 changes: 27 additions & 1 deletion src/typescriptBsType.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@ exception ExpectedIdentifier;
let getName =
fun
| Typescript.FunctionDeclaration({name: Identifier({text})}) => text
| Typescript.InterfaceDeclaration({name: Identifier({text})}) => text
| Typescript.Parameter({name: Identifier({text})}) => text
| Typescript.Identifier({text}) => text
| _ => raise(ExpectedIdentifier);

let rec typescriptAstToBsType =
let rec propertySignatureToObjectProperty =
(prop: Typescript.propertySignature) => (
getName(prop.name),
typescriptAstToBsType(prop.type_),
switch prop.questionToken {
| Some(_) => true
| None => false
}
)
and typescriptAstToBsType =
fun
| Typescript.Parameter(parameter) => typescriptAstToBsType(parameter.type_)
| Typescript.NumberKeyword(_) => BsTypeAst.Number
| Typescript.StringKeyword(_) => BsTypeAst.String
| _ => BsTypeAst.Any;

let rec typescriptAstToBsTypeAst =
Expand All @@ -35,4 +46,19 @@ let rec typescriptAstToBsTypeAst =
returnType: typescriptAstToBsType(func.type_)
})
)
| Typescript.InterfaceDeclaration(interface) =>
BsTypeAst.InterfaceDecl(
getName(interface.name),
[],
BsTypeAst.Object(
interface.members
|> Array.to_list
|> List.map(
fun
| Typescript.PropertySignature(p) =>
propertySignatureToObjectProperty(p)
| _ => raise(Not_found)
)
)
)
| _ => BsTypeAst.Noop;

0 comments on commit 5046d9d

Please sign in to comment.