Skip to content

Commit

Permalink
Add support for ConstructSignature in literal type
Browse files Browse the repository at this point in the history
Fix #59
  • Loading branch information
MangelMaxime committed Mar 25, 2024
1 parent 1685de8 commit 7a20291
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
```

* Add support for argument spread operator ([GH-57](https://github.com/glutinum-org/cli/issues/57))
* Add support for `{ new (...args: any): any}` (`ConstructSignaure`) ([GH-59](https://github.com/glutinum-org/cli/issues/59))

### Changed

Expand Down
3 changes: 3 additions & 0 deletions src/Glutinum.Converter/FsharpAST.fs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ type FSharpAttribute =
| CompiledName of string
| RequireQualifiedAccess
| EmitConstructor
/// <summary>
/// Generates <c>[&lt;Emit("new $0.className($1...)")&gt;]"</c> attribute.
/// </summary>
| EmitMacroConstructor of className: string
| EmitIndexer
| Global
Expand Down
7 changes: 7 additions & 0 deletions src/Glutinum.Converter/GlueAST.fs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,20 @@ type GlueMethodSignature =
Type: GlueType
}

type GlueConstructSignature =
{
Parameters: GlueParameter list
Type: GlueType
}

[<RequireQualifiedAccess>]
type GlueMember =
| Method of GlueMethod
| Property of GlueProperty
| CallSignature of GlueCallSignature
| IndexSignature of GlueIndexSignature
| MethodSignature of GlueMethodSignature
| ConstructSignature of GlueConstructSignature

type GlueInterface =
{
Expand Down
11 changes: 11 additions & 0 deletions src/Glutinum.Converter/Reader/Declaration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ let readDeclaration
: GlueMethodSignature)
|> GlueMember.MethodSignature

| Ts.SyntaxKind.ConstructSignature ->
let constructSignature =
declaration :?> Ts.ConstructSignatureDeclaration

({
Parameters = reader.ReadParameters constructSignature.parameters
Type = reader.ReadTypeNode constructSignature.``type``
}
: GlueConstructSignature)
|> GlueMember.ConstructSignature

| _ ->
Utils.generateReaderError
"named declaration"
Expand Down
31 changes: 31 additions & 0 deletions src/Glutinum.Converter/Transform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,24 @@ module private TransformMembers =
Accessibility = FSharpAccessibility.Public
}
|> FSharpMember.Method

| GlueMember.ConstructSignature constructSignature ->
let name, context = sanitizeNameAndPushScope "Create" context

{
Attributes = [ FSharpAttribute.EmitConstructor ]
Name = name
Parameters =
constructSignature.Parameters
|> List.map (transformParameter context)
Type = transformType context constructSignature.Type
TypeParameters = []
IsOptional = false
IsStatic = false
Accessor = None
Accessibility = FSharpAccessibility.Public
}
|> FSharpMember.Method
)

let toFSharpParameters
Expand Down Expand Up @@ -602,6 +620,17 @@ module private TransformMembers =
Type = transformType context callSignatureInfo.Type
}
: FSharpParameter

| GlueMember.ConstructSignature constructSignature ->
let name, context = sanitizeNameAndPushScope "Create" context

{
Attributes = []
Name = name
IsOptional = false
Type = transformType context constructSignature.Type
}
: FSharpParameter
)

let private transformInterface
Expand Down Expand Up @@ -730,6 +759,7 @@ module TypeAliasDeclaration =
|> Some
// Doesn't make sense to have a case for call signature
| GlueMember.CallSignature _
| GlueMember.ConstructSignature _
// Doesn't make sense to have a case for index signature
// because index signature is used because we don't know the name
// of the properties and so it is used only to describe the
Expand Down Expand Up @@ -958,6 +988,7 @@ let private transformTypeAliasDeclaration
| GlueMember.Method { Type = typ }
| GlueMember.Property { Type = typ }
| GlueMember.CallSignature { Type = typ }
| GlueMember.ConstructSignature { Type = typ }
| GlueMember.MethodSignature { Type = typ }
| GlueMember.IndexSignature { Type = typ } ->
match typ with
Expand Down
6 changes: 6 additions & 0 deletions src/Glutinum.Web/Pages/Editors.GlueAST.GlueASTViewer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ type GlueASTViewer =
GlueASTViewer.Type propertyInfo.Type
]

| GlueMember.ConstructSignature constructSignature ->
ASTViewer.renderNode "ConstructSignature" [
GlueASTViewer.Parameters constructSignature.Parameters
GlueASTViewer.Type constructSignature.Type
]

static member private Members(members: GlueMember list) =
members
|> List.map GlueASTViewer.GlueMember
Expand Down
1 change: 1 addition & 0 deletions tests/specs/references/typeLiteral/constructSignature.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Test = { new (...args: any[]): any; }
13 changes: 13 additions & 0 deletions tests/specs/references/typeLiteral/constructSignature.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module rec Glutinum

open Fable.Core
open System

[<AllowNullLiteral>]
type Test =
[<EmitConstructor>]
abstract member Create: [<ParamArray>] args: ResizeArray<obj> [] -> obj

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

0 comments on commit 7a20291

Please sign in to comment.