Skip to content

Commit

Permalink
fix: if an argument is decorated with ? and undefined or null r…
Browse files Browse the repository at this point in the history
…emove the option type

[converter][web]

Fix #114
  • Loading branch information
MangelMaxime committed Jul 16, 2024
1 parent e75209f commit a413d14
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/Glutinum.Converter/Transform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ type TransformContext

member _.AddError(error: string) = reporter.Errors.Add error

let private unwrapOptionIfAlreadyOptional
(context: TransformContext)
(typ: GlueType)
(isOptional: bool)
=
// If the property is optional, we want to unwrap the option type
// This is to prevent generating a `string option option`
let typ' = transformType context typ

if isOptional then
match typ' with
| FSharpType.Option underlyingType -> underlyingType
| _ -> typ'
else
typ'

let private sanitizeNameAndPushScope
(name: string)
(context: TransformContext)
Expand Down Expand Up @@ -844,7 +860,11 @@ let private transformParameter
let context = context.PushScope(parameter.Name)

let typ =
let computedType = transformType context parameter.Type
let computedType =
unwrapOptionIfAlreadyOptional
context
parameter.Type
parameter.IsOptional

// In TypeScript, if an argument is marked as spread, users is forced to
// use an array. We want to remove the default transformation for that
Expand Down Expand Up @@ -1010,24 +1030,16 @@ module private TransformMembers =
if propertyInfo.IsPrivate && not propertyInfo.IsStatic then
None // F# interface can't have private properties
else
// If the property is optional, we want to unwrap the option type
// This is to prevent generating a `string option option`
let typ =
let typ' = transformType context propertyInfo.Type

if propertyInfo.IsOptional then
match typ' with
| FSharpType.Option underlyingType -> underlyingType
| _ -> typ'
else
typ'

{
Attributes = [ yield! xmlDocInfo.ObsoleteAttributes ]
Name = name
OriginalName = propertyInfo.Name
Parameters = []
Type = typ
Type =
unwrapOptionIfAlreadyOptional
context
propertyInfo.Type
propertyInfo.IsOptional
TypeParameters = []
IsOptional = propertyInfo.IsOptional
IsStatic = propertyInfo.IsStatic
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface PromiseLike<T> {
a(onfulfilled?: string | undefined | null);
b(onfulfilled?: string | undefined);
c(onfulfilled?: string | null);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module rec Glutinum

open Fable.Core
open Fable.Core.JsInterop
open System

[<AllowNullLiteral>]
[<Interface>]
type PromiseLike<'T> =
abstract member a: ?onfulfilled: string -> unit
abstract member b: ?onfulfilled: string -> unit
abstract member c: ?onfulfilled: string -> unit

(***)
#r "nuget: Fable.Core"
(***)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function a(onfulfilled?: string | undefined | null);
declare function b(onfulfilled?: string | undefined);
declare function c(onfulfilled?: string | null);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module rec Glutinum

open Fable.Core
open Fable.Core.JsInterop
open System

[<AbstractClass>]
[<Erase>]
type Exports =
[<Import("a", "REPLACE_ME_WITH_MODULE_NAME")>]
static member a (?onfulfilled: string) : unit = nativeOnly
[<Import("b", "REPLACE_ME_WITH_MODULE_NAME")>]
static member b (?onfulfilled: string) : unit = nativeOnly
[<Import("c", "REPLACE_ME_WITH_MODULE_NAME")>]
static member c (?onfulfilled: string) : unit = nativeOnly

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

0 comments on commit a413d14

Please sign in to comment.