Skip to content

Commit

Permalink
Add support for TupleType
Browse files Browse the repository at this point in the history
  • Loading branch information
MangelMaxime committed Mar 14, 2024
1 parent 826cb34 commit 4f6202c
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
abstract member log: (bool -> float -> MyObject) with get, set
```

* Add support for `TupleType`

### Changed

* Replace `Boolean` with `bool`
Expand Down
1 change: 0 additions & 1 deletion src/Glutinum.Build/Tasks/Test/Specs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ let private testFile (hiearchyLevel: int) (content: string) =
import {{ expect, test }} from 'vitest'
import {{ generateBindingFile }} from '%s{generateFilePath}'
// import {{ sum }} from './sum'
import {{ dirname }} from "dirname-filename-esm";
import path from 'node:path';
Expand Down
1 change: 1 addition & 0 deletions src/Glutinum.Converter/FsharpAST.fs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ type FSharpType =
// | Class of FSharpClass
| Discard
| TypeReference of FSharpTypeReference
| Tuple of FSharpType list
| TypeParameter of string
| ResizeArray of FSharpType
| ThisType of typeName: string
Expand Down
2 changes: 2 additions & 0 deletions src/Glutinum.Converter/GlueAST.fs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ type GlueType =
| FunctionType of GlueFunctionType
| TypeParameter of string
| ThisType of typeName: string
| TupleType of GlueType list

member this.Name =
match this with
Expand Down Expand Up @@ -223,4 +224,5 @@ type GlueType =
| Union _
| Partial _
| FunctionType _
| TupleType _
| Discard -> "obj"
8 changes: 6 additions & 2 deletions src/Glutinum.Converter/Printer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ let private sanitizeEnumCaseName (name: string) =
$"``{name}``"
| _ -> name

let attributeToText (fsharpAttribute: FSharpAttribute) =
let private attributeToText (fsharpAttribute: FSharpAttribute) =
match fsharpAttribute with
| FSharpAttribute.Text text -> text
| FSharpAttribute.EmitSelfInvoke -> "[<Emit(\"$0($1...)\")>]"
Expand Down Expand Up @@ -139,7 +139,7 @@ let private printCompactAttributesAndNewLine
printer.Write(attributesText)
printer.NewLine

let printAttributes
let private printAttributes
(printer: Printer)
(fsharpAttributes: FSharpAttribute list)
=
Expand All @@ -166,6 +166,9 @@ let rec private printType (fsharpType: FSharpType) =

| FSharpType.ThisType name -> name

| FSharpType.Tuple types ->
types |> List.map printType |> String.concat " * "

| FSharpType.Function functionInfo ->
match functionInfo.Parameters with
| [] ->
Expand Down Expand Up @@ -486,6 +489,7 @@ let rec print (printer: Printer) (fsharpTypes: FSharpType list) =
| FSharpType.TypeParameter _
| FSharpType.Discard
| FSharpType.Function _
| FSharpType.Tuple _
| FSharpType.ThisType _ -> ()

print printer tail
Expand Down
11 changes: 11 additions & 0 deletions src/Glutinum.Converter/Reader/TypeNode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@ let readTypeNode

GlueType.ThisType typ.symbol.name

| Ts.SyntaxKind.TupleType ->
let tupleTypeNode = typeNode :?> Ts.TupleTypeNode

tupleTypeNode.elements
|> Seq.toList
|> List.map (fun element ->
let element = unbox<Ts.TypeNode> element
reader.ReadTypeNode element
)
|> GlueType.TupleType

| _ ->
generateReaderError
"type node"
Expand Down
16 changes: 16 additions & 0 deletions src/Glutinum.Converter/Transform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ let private transformPrimitive
| GluePrimitive.Null -> FSharpPrimitive.Null
| GluePrimitive.Undefined -> FSharpPrimitive.Null

let private transformTupleType (glueTypes: GlueType list) : FSharpType =
glueTypes |> List.map transformType |> FSharpType.Tuple

let rec private transformType (glueType: GlueType) : FSharpType =
match glueType with
| GlueType.Primitive primitiveInfo ->
transformPrimitive primitiveInfo |> FSharpType.Primitive

| GlueType.ThisType typeName -> FSharpType.ThisType typeName

| GlueType.TupleType glueTypes -> transformTupleType glueTypes

| GlueType.Union(GlueTypeUnion cases) ->
let optionalTypes, others =
cases
Expand Down Expand Up @@ -780,6 +785,16 @@ let private transformTypeAliasDeclaration
}
|> FSharpType.Interface

| GlueType.TupleType glueTypes ->
({
Name = typeAliasName
Type = transformTupleType glueTypes
TypeParameters =
transformTypeParameters glueTypeAliasDeclaration.TypeParameters
}
: FSharpTypeAlias)
|> FSharpType.TypeAlias

| _ -> FSharpType.Discard

let private transformModuleDeclaration
Expand Down Expand Up @@ -851,6 +866,7 @@ let rec private transformToFsharp (glueTypes: GlueType list) : FSharpType list =
| GlueType.Primitive _
| GlueType.KeyOf _
| GlueType.Discard
| GlueType.TupleType _
| GlueType.ThisType _ -> FSharpType.Discard
)

Expand Down
1 change: 1 addition & 0 deletions tests/specs/references/tupleType/mixedArgumentsType.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function t(a: [number, string, boolean, string | undefined, string | boolean]): void;
13 changes: 13 additions & 0 deletions tests/specs/references/tupleType/mixedArgumentsType.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module rec Glutinum

open Fable.Core
open System

[<Erase>]
type Exports =
[<Import("t", "module")>]
static member t (a: float * string * bool * string option * U2<string, bool>) : unit = nativeOnly

(***)
#r "nuget: Fable.Core"
(***)
2 changes: 2 additions & 0 deletions tests/specs/references/tupleType/severalArguments.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export function t2(a: [number, number]): void;
export function t3(a: [number, number, number]): void;
15 changes: 15 additions & 0 deletions tests/specs/references/tupleType/severalArguments.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module rec Glutinum

open Fable.Core
open System

[<Erase>]
type Exports =
[<Import("t2", "module")>]
static member t2 (a: float * float) : unit = nativeOnly
[<Import("t3", "module")>]
static member t3 (a: float * float * float) : unit = nativeOnly

(***)
#r "nuget: Fable.Core"
(***)
1 change: 1 addition & 0 deletions tests/specs/references/tupleType/singleArgument.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function transformation(coefficients: [number]): void;
13 changes: 13 additions & 0 deletions tests/specs/references/tupleType/singleArgument.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module rec Glutinum

open Fable.Core
open System

[<Erase>]
type Exports =
[<Import("transformation", "module")>]
static member transformation (coefficients: float) : unit = nativeOnly

(***)
#r "nuget: Fable.Core"
(***)
1 change: 1 addition & 0 deletions tests/specs/references/typeAlias/tupleType.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type LatLng = [number, number]
11 changes: 11 additions & 0 deletions tests/specs/references/typeAlias/tupleType.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module rec Glutinum

open Fable.Core
open System

type LatLng =
float * float

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

0 comments on commit 4f6202c

Please sign in to comment.