Skip to content

Commit

Permalink
fix: don't wrap type inside of a ResizeArray if the argument is spreaded
Browse files Browse the repository at this point in the history
[converter]

Fix #74
  • Loading branch information
MangelMaxime committed Apr 8, 2024
1 parent ddc18b3 commit 53b0958
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
16 changes: 15 additions & 1 deletion src/Glutinum.Converter/Transform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,20 @@ let private transformParameter
=
let context = context.PushScope(parameter.Name)

let typ =
let computedType = transformType context parameter.Type

// 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
// array and use the underlying type instead
// By default, an array is transformed to ResizeArray in F#
if parameter.IsSpread then
match computedType with
| FSharpType.ResizeArray underlyingType -> underlyingType
| _ -> computedType
else
computedType

{
Attributes =
[
Expand All @@ -461,7 +475,7 @@ let private transformParameter
]
Name = Naming.sanitizeName parameter.Name
IsOptional = parameter.IsOptional
Type = transformType context parameter.Type
Type = typ
}

let private transformAccessor (accessor: GlueAccessor) : FSharpAccessor =
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/references/class/constructorsWithSpread.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Exports =
[<Import("Logger", "module"); EmitConstructor>]
static member Logger () : Logger = nativeOnly
[<Import("Logger", "module"); EmitConstructor>]
static member Logger ([<ParamArray>] args: ResizeArray<string> []) : Logger = nativeOnly
static member Logger ([<ParamArray>] args: string []) : Logger = nativeOnly

[<AllowNullLiteral>]
[<Interface>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Class.mergeOptions($0)"""
emitJsExpr (initHookFn) $$"""
import { Class } from "module";
Class.addInitHook($0)"""
static member inline addInitHook (methodName: string, [<ParamArray>] args: ResizeArray<obj> []): obj =
static member inline addInitHook (methodName: string, [<ParamArray>] args: obj []): obj =
emitJsExpr (methodName, args) $$"""
import { Class } from "module";
Class.addInitHook($0, $1)"""
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/references/functionType/spreadArgument.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ open System
[<Interface>]
type Log =
[<Emit("$0($1...)")>]
abstract member Invoke: prefix: string * [<ParamArray>] args: ResizeArray<obj> [] -> obj
abstract member Invoke: prefix: string * [<ParamArray>] args: obj [] -> obj

(***)
#r "nuget: Fable.Core"
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/references/typeLiteral/constructSignature.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ open System
[<Interface>]
type Test =
[<EmitConstructor>]
abstract member Create: [<ParamArray>] args: ResizeArray<obj> [] -> obj
abstract member Create: [<ParamArray>] args: obj [] -> obj

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

0 comments on commit 53b0958

Please sign in to comment.