Skip to content

Commit

Permalink
fable-compiler#428, fable-compiler#429 --noresizearray, --convertpropfns
Browse files Browse the repository at this point in the history
  • Loading branch information
davedawkins committed Oct 30, 2021
1 parent 5af1560 commit 4525ea2
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 103 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/config.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Config

let mutable EmitResizeArray = true
let mutable ConvertPropertyFunctions = false

let Reset() =
EmitResizeArray <- true
ConvertPropertyFunctions <- false

module OptionNames =
let NoEmitResizeArray = "--noresizearray"
let ConvertPropertyFunctions = "--convertpropfns"
40 changes: 25 additions & 15 deletions src/print.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let printType (tp: FsType): string =
| FsType.Mapped mp ->
mp.Name
| FsType.Array at ->
sprintf "ResizeArray<%s>" (printType at)
sprintf (if Config.EmitResizeArray then "ResizeArray<%s>" else "%s[]") (printType at)
| FsType.Union un ->
if un.Types.Length = 1 then
sprintf "%s%s" (printType un.Types.[0]) (if un.Option then " option" else "")
Expand Down Expand Up @@ -79,7 +79,7 @@ let printFunctionType (f: FsFunction) =
// this bracket isn't always necessary for example:
// `const f: (value?: string) => void`
// in F#: `abstract f: (string) option -> unit`
// but it's easier to always put into brackets, than to discrimate
// but it's easier to always put into brackets, than to discrimate
// when it's needed (tuple, function) and when not (simple type).
sprintf "(%s) option" t
else
Expand All @@ -103,7 +103,7 @@ let printEnumType (en: FsEnum): string =
| FsEnumCaseType.String -> "string"
| FsEnumCaseType.Unknown -> "obj"

let printFunction (fn: FsFunction): string =
let printFunctionName (fn: FsFunction) (name:string): string =
let line = ResizeArray()

match fn.Kind with
Expand All @@ -115,7 +115,7 @@ let printFunction (fn: FsFunction): string =
| FsFunctionKind.StringParam emit ->
sprintf "[<Emit \"%s\">] " emit |> line.Add

sprintf "abstract %s" fn.Name.Value |> line.Add
sprintf "abstract %s" name |> line.Add

// parameters
let prms =
Expand Down Expand Up @@ -146,7 +146,17 @@ let printFunction (fn: FsFunction): string =

line |> String.concat ""

let printFunction (fn: FsFunction): string =
printFunctionName fn (fn.Name.Value)

let printProperty (pr: FsProperty): string =
match Config.ConvertPropertyFunctions, pr.Type with
| true, FsType.Function fn ->
printFunctionName fn pr.Name
| _ ->
printPropertyDefault pr

let printPropertyDefault (pr: FsProperty): string =
sprintf "%sabstract %s: %s%s%s%s%s"
( match pr.Kind with
| FsPropertyKind.Regular -> ""
Expand All @@ -173,11 +183,11 @@ let printProperty (pr: FsProperty): string =
t

match pr.Accessor with
| ReadOnly ->
| ReadOnly ->
optionInBrackets pr.Type
| WriteOnly | ReadWrite ->
match pr.Type with
| FsType.Function _ ->
| FsType.Function _ ->
sprintf "(%s)" t
| _ -> optionInBrackets pr.Type
)
Expand Down Expand Up @@ -262,14 +272,14 @@ let printComments (lines: ResizeArray<string>) (indent: string) (comments: FsCom
match attributes with
| [] -> name
| _ ->
let attrs =
attributes
let attrs =
attributes
|> List.map (fun (name, value) -> sprintf "%s=\"%s\"" name value)
|> String.concat " "
sprintf "%s %s" name attrs

match content with
| [] ->
| [] ->
sprintf "<%s />" nameWithAttributes
|> printLine
| [ line ] ->
Expand Down Expand Up @@ -299,7 +309,7 @@ let printComments (lines: ResizeArray<string>) (indent: string) (comments: FsCom
| FsComment.SeeAlso link -> printTag "seealso" [((match link.Type with | HRef -> "href" | CRef -> "cref"), link.Target)] link.Content
| FsComment.TypeParam tp -> printTag "typeparam" [("name", tp.Name)] tp.Content
| FsComment.Example e -> printTag "example" [] e
| FsComment.Exception e ->
| FsComment.Exception e ->
// exception REQUIRES a type -- otherwise it isn't shown (in VS) or produces a doc parsing error in Ionide
// BUT: it doesn't matter what's in the attribute...
// -> use empty type entry when no type specified
Expand Down Expand Up @@ -332,7 +342,7 @@ let printAttributes (lines: ResizeArray<string>) (indent: string) (attrs: FsAttr
match attr.Arguments with
| [] -> name
| args ->
let args =
let args =
args
|> List.map formatArgument
|> String.concat ", "
Expand Down Expand Up @@ -430,9 +440,9 @@ let rec printModule (lines: ResizeArray<string>) (indent: string) (md: FsModule)
match inf.Members with
| [FsType.Enum en] ->
// type literal -> comments are in parent Alias which was transformed into Interface
let en =
{ en with
Name = inf.Name
let en =
{ en with
Name = inf.Name
Comments = inf.Comments @ en.Comments
}
printEnum lines indent en
Expand Down
2 changes: 2 additions & 0 deletions src/ts2fable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ let parseArgs (args: string[]) =
let exports = args |> getArgs (fun s -> s = "-e" || s = "--exports")
let fsPaths = args |> Array.filter (fun s -> s.EndsWith ".fs") |> Array.toList
let tsPaths = args |> Array.filter (fun s -> s.EndsWith ".ts") |> Array.toList
Config.EmitResizeArray <- not (args |> Array.contains (Config.OptionNames.NoEmitResizeArray))
Config.ConvertPropertyFunctions <- args |> Array.contains (Config.OptionNames.ConvertPropertyFunctions)
if List.isEmpty fsPaths then failwithf "Please provide the path to the F# file to be written."
if List.isEmpty tsPaths then failwithf "Please provide the path to a TypeScript file."
// print ts2fable version
Expand Down
3 changes: 2 additions & 1 deletion src/ts2fable.fsproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<!--
<!--
Symbol required when cli build with fable: TS2FABLE_STANDALONE
(Not required in .NET build)
Expand All @@ -10,6 +10,7 @@
-->
</PropertyGroup>
<ItemGroup>
<Compile Include="config.fs" />
<Compile Include="node/fileSystem.fs" />
<Compile Include="TypeScript.fs" />
<Compile Include="keywords.fs" />
Expand Down
7 changes: 7 additions & 0 deletions test/fragments/regressions/#428-noresizearray.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//--noresizearray
declare class Appwrite {
account: {
createDocument: string[]
};
}
export { Appwrite };
18 changes: 18 additions & 0 deletions test/fragments/regressions/#428-noresizearray.expected.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// ts2fable 0.0.0
module rec ``#428-noresizearray``
open System
open Fable.Core
open Fable.Core.JS


type [<AllowNullLiteral>] IExports =
abstract Appwrite: AppwriteStatic

type [<AllowNullLiteral>] Appwrite =
abstract account: AppwriteAccount with get, set

type [<AllowNullLiteral>] AppwriteStatic =
[<EmitConstructor>] abstract Create: unit -> Appwrite

type [<AllowNullLiteral>] AppwriteAccount =
abstract createDocument: string[] with get, set
7 changes: 7 additions & 0 deletions test/fragments/regressions/#429-convertpropfns.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//--convertpropfns
declare class Appwrite {
account: {
createDocument: (name: string) => Document;
};
}
export { Appwrite };
18 changes: 18 additions & 0 deletions test/fragments/regressions/#429-convertpropfns.expected.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// ts2fable 0.8.0
module rec ``#429-convertpropfns``
open System
open Fable.Core
open Fable.Core.JS


type [<AllowNullLiteral>] IExports =
abstract Appwrite: AppwriteStatic

type [<AllowNullLiteral>] Appwrite =
abstract account: AppwriteAccount with get, set

type [<AllowNullLiteral>] AppwriteStatic =
[<EmitConstructor>] abstract Create: unit -> Appwrite

type [<AllowNullLiteral>] AppwriteAccount =
abstract createDocument: name: string -> Document
Loading

0 comments on commit 4525ea2

Please # to comment.