Skip to content

Commit

Permalink
migration optional params from TypeScript types in .d.ts properly (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinbarabash committed Aug 31, 2024
1 parent 96373b5 commit 2b8b079
Show file tree
Hide file tree
Showing 31 changed files with 115 additions and 82 deletions.
9 changes: 4 additions & 5 deletions src/Escalier.Codegen.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ open Escalier.Codegen.Printer
open Escalier.Codegen.Codegen
open Escalier.Parser
open Escalier.TypeChecker
open Escalier.TypeChecker.Infer

type CompileError = Prelude.CompileError

Expand Down Expand Up @@ -403,18 +402,18 @@ let CodegenFunction () =

// TODO(#349): Make function params that are primitive types immutable when migrating
// the types from .d.ts files.
// TODO(#350): Fix optional function params in functions migrated from .d.ts files.
// TODO(#351): Update getPropType to check for properties on the Extends type of an
// object type.
[<Fact(Skip = "TODO")>]
[<Fact>]
let CodegenAsyncFunction () =
let res =
result {
let src =
"""
let fetchJSON = async fn (url: string) {
let fetchJSON = async fn (mut url: string) {
let res = await fetch(url);
return res.json();
return res;
// return res.json();
};
"""

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
input:
let fetchJSON = async fn (url: string) {
let fetchJSON = async fn (mut url: string) {
let res = await fetch(url);
return res.json();
return res;
// return res.json();
};

output:
var fetchJSON = function (url) {
var res = await fetch(url);
return res.json();
return res;
};
7 changes: 5 additions & 2 deletions src/Escalier.Codegen/Codegen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ module rec Codegen =
let pat, _ = buildPattern ctx p.Pattern None

{ Pat = pat
Optional = p.Optional
TypeAnn = None
Loc = None })

Expand Down Expand Up @@ -977,6 +978,7 @@ module rec Codegen =
let pat, _ = buildPattern ctx p.Pattern None

{ Pat = pat
Optional = p.Optional
TypeAnn = None
Loc = None })

Expand Down Expand Up @@ -1124,13 +1126,14 @@ module rec Codegen =
let maxParamCount =
decls |> List.map (fun decl -> decl.Sig.ParamList.Length) |> List.max

let ps =
let ps: List<Param> =
seq { 0 .. maxParamCount - 1 }
|> Seq.map (fun i ->
{ Pat =
{ Param.Pat =
Pat.Ident
{ Id = { Name = $"__arg{i}__"; Loc = None }
Loc = None }
Optional = false
TypeAnn = None
Loc = None })
|> List.ofSeq
Expand Down
10 changes: 8 additions & 2 deletions src/Escalier.Codegen/Printer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,14 @@ module rec Printer =
failwith "TODO: printTsFnParam - objectPat"

match param.TypeAnn with
| Some(typeAnn) -> $"{pat}: {printTypeAnn ctx typeAnn}"
| None -> pat
| Some(typeAnn) ->
match param.Optional with
| true -> $"{pat}?: {printTypeAnn ctx typeAnn}"
| false -> $"{pat}: {printTypeAnn ctx typeAnn}"
| None ->
match param.Optional with
| true -> $"{pat}?"
| false -> pat

let printTsTypeParam (ctx: PrintCtx) (typeParam: TsTypeParam) : string =
let c =
Expand Down
6 changes: 3 additions & 3 deletions src/Escalier.Data/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ module Syntax =

type FuncParam =
{ Pattern: Pattern
TypeAnn: option<TypeAnn>
Optional: bool }
Optional: bool
TypeAnn: option<TypeAnn> }

override this.ToString() = this.Pattern.ToString()

Expand Down Expand Up @@ -420,7 +420,7 @@ module Syntax =

type IdentPat =
{ mutable Name: string
IsMut: bool
mutable IsMut: bool
Assertion: option<Common.QualifiedIdent> }

type EnumVariantPattern =
Expand Down
6 changes: 3 additions & 3 deletions src/Escalier.Interop.Tests/Migrate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ let ParseAndInferFuncDecl () =
let! env =
Infer.inferModule ctx env ast |> Result.mapError CompileError.TypeError

Assert.Value(env, "foo", "fn (mut x: number, mut y: string) -> boolean")
Assert.Value(env, "foo", "fn (x: number, y: string) -> boolean")
}

Assert.True(Result.isOk res)
Expand Down Expand Up @@ -257,7 +257,7 @@ let ParseAndInferClassDecl () =
Assert.Type(
env,
"Foo",
"{bar fn (self: Self, mut x: number, mut y: string) -> boolean, baz: string, ...}"
"{bar fn (self: Self, x: number, y: string) -> boolean, baz: string, ...}"
)
}

Expand Down Expand Up @@ -291,7 +291,7 @@ let ParseAndInferClassDeclWithStatics () =
Assert.Value(
env,
"Foo",
"{new fn () -> Foo, bar fn (mut x: number, mut y: string) -> boolean, baz: string}"
"{new fn () -> Foo, bar fn (x: number, y: string) -> boolean, baz: string}"
)

Assert.Type(env, "Foo", "{...}")
Expand Down
2 changes: 1 addition & 1 deletion src/Escalier.Interop.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ let InferOverloadedFunctionsFromLibDOM () =
Assert.Value(
env,
"scroll",
"fn (mut options: ScrollToOptions) -> undefined & fn (mut x: number, mut y: number) -> undefined"
"fn (mut options?: ScrollToOptions) -> undefined & fn (x: number, y: number) -> undefined"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ output: Success: { Body =
{ Pat = Ident { Id = { Name = "bar"
Loc = None }
Loc = None }
Optional = false
TypeAnn =
Some
{ TypeAnn =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ output: Success: { Body =
{ Pat = Ident { Id = { Name = "bar"
Loc = None }
Loc = None }
Optional = false
TypeAnn =
Some
{ TypeAnn =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ output: Success: { Body =
[{ Pat = Ident { Id = { Name = "string"
Loc = None }
Loc = None }
Optional = false
TypeAnn =
Some { TypeAnn = TsKeywordType { Kind = TsStringKeyword
Loc = None }
Expand All @@ -21,6 +22,7 @@ output: Success: { Body =
{ Pat = Ident { Id = { Name = "radix"
Loc = None }
Loc = None }
Optional = true
TypeAnn =
Some { TypeAnn = TsKeywordType { Kind = TsNumberKeyword
Loc = None }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ output: Success: { Body =
[{ Pat = Ident { Id = { Name = "x"
Loc = None }
Loc = None }
Optional = false
TypeAnn =
Some { TypeAnn = TsKeywordType { Kind = TsStringKeyword
Loc = None }
Expand All @@ -40,6 +41,7 @@ output: Success: { Body =
[{ Pat = Ident { Id = { Name = "a"
Loc = None }
Loc = None }
Optional = false
TypeAnn =
Some
{ TypeAnn =
Expand All @@ -52,6 +54,7 @@ output: Success: { Body =
{ Pat = Ident { Id = { Name = "b"
Loc = None }
Loc = None }
Optional = false
TypeAnn =
Some
{ TypeAnn =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ output: Success: { Body =
[{ Pat = Ident { Id = { Name = "object"
Loc = None }
Loc = None }
Optional = false
TypeAnn =
Some
{ TypeAnn =
Expand Down
40 changes: 27 additions & 13 deletions src/Escalier.Interop/Migrate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,9 @@ module rec Migrate =
let kind =
match pat with
| TsFnParamPat.Ident { Id = ident } ->
if ident.Name = "input" then
printfn $"input is mutable: {isMutable}"

PatternKind.Ident
{ Name = ident.Name
IsMut = isMutable
Expand Down Expand Up @@ -754,9 +757,28 @@ module rec Migrate =
{ Kind = kind; Span = DUMMY_SPAN }

let migrateParam (param: Param) : Syntax.FuncParam =
{ Pattern = migratePat param.Pat
TypeAnn = Option.map migrateTypeAnn param.TypeAnn
Optional = false }
let pat = migratePat param.Pat

let typeAnn =
match param.TypeAnn with
| Some typeAnn ->
let typeAnn = migrateTypeAnn typeAnn

match typeAnn.Kind with
| TypeAnnKind.Keyword KeywordTypeAnn.Number
| TypeAnnKind.Keyword KeywordTypeAnn.String
| TypeAnnKind.Keyword KeywordTypeAnn.Boolean ->
match pat.Kind with
| PatternKind.Ident ident -> ident.IsMut <- false
| _ -> ()
| _ -> ()

Some typeAnn
| None -> None

{ Pattern = pat
TypeAnn = typeAnn
Optional = param.Optional }

let migrateClassDecl (ctx: Ctx) (decl: ClassDecl) : Syntax.ClassDecl =
let { Declare = declare
Expand Down Expand Up @@ -796,9 +818,7 @@ module rec Migrate =
| TsParamPropParam.Assign assignPat ->
Pat.Assign assignPat

{ Pat = pat
TypeAnn = tsParamProp.TypeAnn
Loc = tsParamProp.Loc })
failwith "TODO: migrateClassDecl - TsParamProp")

let fnSig: FuncSig =
{ TypeParams = None
Expand Down Expand Up @@ -953,13 +973,7 @@ module rec Migrate =
(fun (tpd: TsTypeParamDecl) -> List.map migrateTypeParam tpd.Params)
f.TypeParams

let paramList: list<FuncParam> =
List.map
(fun (p: Param) ->
{ Pattern = migratePat p.Pat
TypeAnn = Option.map migrateTypeAnn p.TypeAnn
Optional = false })
f.Params
let paramList: list<FuncParam> = List.map migrateParam f.Params

let retType =
match f.ReturnType with
Expand Down
1 change: 1 addition & 0 deletions src/Escalier.Interop/Parser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ module Parser =
pipe3 pat (opt (strWs "?")) (opt (strWs ":" >>. tsTypeAnn))
<| fun p optional typeAnn ->
{ Pat = p
Optional = optional.IsSome
TypeAnn = typeAnn
Loc = None }

Expand Down
1 change: 1 addition & 0 deletions src/Escalier.Interop/TypeScript.fs
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ module rec TypeScript =
// TODO: Decorators
// decorators: list<Decorator>
Pat: Pat
Optional: bool
TypeAnn: option<TsTypeAnn>
Loc: option<SourceLocation> }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ output: Ok
Span = { Start = (Ln: 1, Col: 15)
Stop = (Ln: 1, Col: 16) }
InferredType = None }
TypeAnn = None
Optional = false };
Optional = false
TypeAnn = None };
{ Pattern =
{ Kind = Ident { Name = "y"
IsMut = false
Assertion = None }
Span = { Start = (Ln: 1, Col: 18)
Stop = (Ln: 1, Col: 19) }
InferredType = None }
TypeAnn = None
Optional = false }]
Optional = false
TypeAnn = None }]
ReturnType = None
Throws = None
IsAsync = false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ output: Ok
{ Start = (Ln: 4, Col: 12)
Stop = (Ln: 4, Col: 16) }
InferredType = None }
TypeAnn = None
Optional = false }
Optional = false
TypeAnn = None }
ParamList =
[{ Pattern =
{ Kind = Ident { Name = "msg"
Expand All @@ -63,14 +63,14 @@ output: Ok
Span = { Start = (Ln: 4, Col: 18)
Stop = (Ln: 4, Col: 21) }
InferredType = None }
Optional = false
TypeAnn =
Some
{ Kind = Keyword String
Span =
{ Start = (Ln: 4, Col: 23)
Stop = (Ln: 4, Col: 29) }
InferredType = None }
Optional = false }]
InferredType = None } }]
ReturnType = None
Throws = None
IsAsync = false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ output: Ok
Span = { Start = (Ln: 4, Col: 15)
Stop = (Ln: 4, Col: 19) }
InferredType = None }
TypeAnn = None
Optional = false }
Optional = false
TypeAnn = None }
Body =
Some
(Block
Expand Down Expand Up @@ -104,8 +104,8 @@ output: Ok
Span = { Start = (Ln: 7, Col: 15)
Stop = (Ln: 7, Col: 23) }
InferredType = None }
TypeAnn = None
Optional = false }
Optional = false
TypeAnn = None }
Param =
{ Pattern =
{ Kind = Ident { Name = "msg"
Expand All @@ -114,13 +114,13 @@ output: Ok
Span = { Start = (Ln: 7, Col: 25)
Stop = (Ln: 7, Col: 28) }
InferredType = None }
Optional = false
TypeAnn =
Some
{ Kind = Keyword String
Span = { Start = (Ln: 7, Col: 30)
Stop = (Ln: 7, Col: 36) }
InferredType = None }
Optional = false }
InferredType = None } }
Body =
Some
(Block
Expand Down
Loading

0 comments on commit 2b8b079

Please sign in to comment.