Skip to content

Commit

Permalink
Fix type literal as return type for function + Add more type literal …
Browse files Browse the repository at this point in the history
…tests

Fix #58
  • Loading branch information
MangelMaxime committed Mar 24, 2024
1 parent dc1b1c7 commit 1685de8
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Glutinum.Converter/Transform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,14 @@ let private transformExports
|> List.collect (
function
| GlueType.Variable info ->
let name, context = sanitizeNameAndPushScope info.Name context

{
Attributes = [ FSharpAttribute.Import(info.Name, "module") ]
Name = Naming.sanitizeName info.Name
Name = name
Parameters = []
TypeParameters = []
Type = (transformType context) info.Type
Type = transformType context info.Type
IsOptional = false
IsStatic = true
Accessor = None
Expand All @@ -314,9 +316,11 @@ let private transformExports
|> List.singleton

| GlueType.FunctionDeclaration info ->
let name, context = sanitizeNameAndPushScope info.Name context

{
Attributes = [ FSharpAttribute.Import(info.Name, "module") ]
Name = Naming.sanitizeName info.Name
Name = name
Parameters =
info.Parameters |> List.map (transformParameter context)
TypeParameters =
Expand All @@ -332,6 +336,7 @@ let private transformExports

| GlueType.ClassDeclaration info ->
// TODO: Handle constructor overloads
let name, context = sanitizeNameAndPushScope info.Name context

info.Constructors
|> List.map (fun (GlueConstructor parameters) ->
Expand All @@ -345,7 +350,7 @@ let private transformExports
FSharpAttribute.EmitMacroConstructor
info.Name
]
Name = Naming.sanitizeName info.Name
Name = name
Parameters =
parameters |> List.map (transformParameter context)
TypeParameters =
Expand Down Expand Up @@ -1200,6 +1205,7 @@ let rec private transformToFsharp
| GlueType.Discard
| GlueType.TupleType _
| GlueType.IntersectionType _
| GlueType.TypeLiteral _
| GlueType.ThisType _ -> FSharpType.Discard
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Test {
now(): { day: number, month: number, year: number }
}
32 changes: 32 additions & 0 deletions tests/specs/references/typeLiteral/returnType/classMethod.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module rec Glutinum

open Fable.Core
open System

[<Erase>]
type Exports =
interface end

[<AllowNullLiteral>]
type Test =
abstract member now: unit -> Test.now

module Test =

[<Global>]
[<AllowNullLiteral>]
type now
[<ParamObject; Emit("$0")>]
(
day: float,
month: float,
year: float
) =

member val day : float = nativeOnly with get, set
member val month : float = nativeOnly with get, set
member val year : float = nativeOnly with get, set

(***)
#r "nuget: Fable.Core"
(***)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function Test(): { a: string, b: number }
25 changes: 25 additions & 0 deletions tests/specs/references/typeLiteral/returnType/function.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module rec Glutinum

open Fable.Core
open System

[<Erase>]
type Exports =
[<Import("Test", "module")>]
static member Test () : Test = nativeOnly

[<Global>]
[<AllowNullLiteral>]
type Test
[<ParamObject; Emit("$0")>]
(
a: string,
b: float
) =

member val a : string = nativeOnly with get, set
member val b : float = nativeOnly with get, set

(***)
#r "nuget: Fable.Core"
(***)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Test = {
now(): { day: number, month: number, year: number }
}
28 changes: 28 additions & 0 deletions tests/specs/references/typeLiteral/returnType/typeAliasMethod.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module rec Glutinum

open Fable.Core
open System

[<AllowNullLiteral>]
type Test =
abstract member now: unit -> Test.now

module Test =

[<Global>]
[<AllowNullLiteral>]
type now
[<ParamObject; Emit("$0")>]
(
day: float,
month: float,
year: float
) =

member val day : float = nativeOnly with get, set
member val month : float = nativeOnly with get, set
member val year : float = nativeOnly with get, set

(***)
#r "nuget: Fable.Core"
(***)
3 changes: 3 additions & 0 deletions tests/specs/references/typeLiteral/typeAlias/arguments.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Test {
date(config: { day: number, month: number, year: number }): string
}
34 changes: 34 additions & 0 deletions tests/specs/references/typeLiteral/typeAlias/arguments.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module rec Glutinum

open Fable.Core
open System

[<Erase>]
type Exports =
interface end

[<AllowNullLiteral>]
type Test =
abstract member date: config: Test.date.config -> string

module Test =

module date =

[<Global>]
[<AllowNullLiteral>]
type config
[<ParamObject; Emit("$0")>]
(
day: float,
month: float,
year: float
) =

member val day : float = nativeOnly with get, set
member val month : float = nativeOnly with get, set
member val year : float = nativeOnly with get, set

(***)
#r "nuget: Fable.Core"
(***)

0 comments on commit 1685de8

Please sign in to comment.