Skip to content

Commit

Permalink
refactor: regroup Partial and Record under an UtilityType categ…
Browse files Browse the repository at this point in the history
…ory to avoid polluting too much the AST

[converter][web]

Fix #110
  • Loading branch information
MangelMaxime committed Aug 24, 2024
1 parent 8e60ee7 commit 70a7ea9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 64 deletions.
15 changes: 11 additions & 4 deletions src/Glutinum.Converter/GlueAST.fs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ type GlueRecord =
ValueType: GlueType
}

[<RequireQualifiedAccess>]
type GlueUtilityType =
| Partial of GlueInterface
| Record of GlueRecord
// | ReadOnly of GlueType

[<RequireQualifiedAccess>]
type GlueType =
| Discard
Expand All @@ -275,8 +281,6 @@ type GlueType =
| ModuleDeclaration of GlueModuleDeclaration
| ClassDeclaration of GlueClassDeclaration
| TypeReference of GlueTypeReference
| Partial of GlueInterface
| Record of GlueRecord
| Array of GlueType
| FunctionType of GlueFunctionType
| TypeParameter of string
Expand All @@ -289,6 +293,7 @@ type GlueType =
| Unknown
| ExportDefault of GlueType
| TemplateLiteral
| UtilityType of GlueUtilityType

member this.Name =
match this with
Expand Down Expand Up @@ -324,11 +329,13 @@ type GlueType =
| IntersectionType _
| IndexedAccessType _
| Union _
| Partial _
| FunctionType _
| TupleType _
| OptionalType _ // TODO: Should we take the name of the underlying type and add option to it?
| Discard
| ExportDefault _
| Record _
| Unknown -> "obj"
| UtilityType utilityType ->
match utilityType with
| GlueUtilityType.Partial _
| GlueUtilityType.Record _ -> "obj"
6 changes: 4 additions & 2 deletions src/Glutinum.Converter/Reader/TypeNode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ module UtilityType =
HeritageClauses = []
}
: GlueInterface)
|> GlueType.Partial
|> GlueUtilityType.Partial
|> GlueType.UtilityType

| _ -> GlueType.Discard

Expand All @@ -144,7 +145,8 @@ module UtilityType =
ValueType = typeArguments.[1]
}
: GlueRecord)
|> GlueType.Record
|> GlueUtilityType.Record
|> GlueType.UtilityType

let readTypeNode
(reader: ITypeScriptReader)
Expand Down
79 changes: 41 additions & 38 deletions src/Glutinum.Converter/Transform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -577,23 +577,6 @@ let rec private transformType
| GlueType.NamedTupleType namedTupleType ->
transformType context namedTupleType.Type

| GlueType.Partial interfaceInfo ->
transformInterface context interfaceInfo
|> Interface.makePartial context.CurrentScopeName
|> FSharpType.Interface
|> context.ExposeType

// Get fullname
// Store type in the exposed types memory
({
Name = context.FullName
FullName = context.FullName
TypeArguments = []
Type = FSharpType.Discard
}
: FSharpTypeReference)
|> FSharpType.TypeReference

| GlueType.Discard -> FSharpType.Object

| GlueType.Literal glueLiteral ->
Expand Down Expand Up @@ -639,15 +622,34 @@ let rec private transformType
})
|> FSharpType.Mapped

| GlueType.Record recordInfo ->
transformRecord context context.CurrentScopeName [] recordInfo
|> context.ExposeType
| GlueType.UtilityType utilityType ->
match utilityType with
| GlueUtilityType.Partial interfaceInfo ->
transformInterface context interfaceInfo
|> Interface.makePartial context.CurrentScopeName
|> FSharpType.Interface
|> context.ExposeType

({
Name = context.FullName
TypeParameters = []
})
|> FSharpType.Mapped
// Get fullname
// Store type in the exposed types memory
({
Name = context.FullName
FullName = context.FullName
TypeArguments = []
Type = FSharpType.Discard
}
: FSharpTypeReference)
|> FSharpType.TypeReference

| GlueUtilityType.Record recordInfo ->
transformRecord context context.CurrentScopeName [] recordInfo
|> context.ExposeType

({
Name = context.FullName
TypeParameters = []
})
|> FSharpType.Mapped

| GlueType.TypeAliasDeclaration typeAliasDeclaration ->
({
Expand Down Expand Up @@ -1953,18 +1955,20 @@ let private transformTypeAliasDeclaration
: FSharpTypeAlias)
|> FSharpType.TypeAlias

| GlueType.Partial interfaceInfo ->
transformInterface context interfaceInfo
// Use the alias name instead of the original interface name
|> Interface.makePartial typeAliasName
|> FSharpType.Interface
| GlueType.UtilityType utilityType ->
match utilityType with
| GlueUtilityType.Partial interfaceInfo ->
transformInterface context interfaceInfo
// Use the alias name instead of the original interface name
|> Interface.makePartial typeAliasName
|> FSharpType.Interface

| GlueType.Record recordInfo ->
transformRecord
context
typeAliasName
glueTypeAliasDeclaration.TypeParameters
recordInfo
| GlueUtilityType.Record recordInfo ->
transformRecord
context
typeAliasName
glueTypeAliasDeclaration.TypeParameters
recordInfo

| GlueType.FunctionType functionType ->
{
Expand Down Expand Up @@ -2151,7 +2155,6 @@ let rec private transformToFsharp

| GlueType.FunctionType _
| GlueType.TypeParameter _
| GlueType.Partial _
| GlueType.Array _
| GlueType.TypeReference _
| GlueType.FunctionDeclaration _
Expand All @@ -2166,10 +2169,10 @@ let rec private transformToFsharp
| GlueType.TupleType _
| GlueType.IntersectionType _
| GlueType.TypeLiteral _
| GlueType.Record _
| GlueType.OptionalType _
| GlueType.NamedTupleType _
| GlueType.TemplateLiteral
| GlueType.UtilityType _
| GlueType.ThisType _ -> FSharpType.Discard
)

Expand Down
42 changes: 22 additions & 20 deletions src/Glutinum.Web/Pages/Editors.GlueAST.GlueASTViewer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -428,28 +428,30 @@ type GlueASTViewer =
]
context

| GlueType.Partial partialInfo ->
ASTViewer.renderNode
"Partial"
[
GlueASTViewer.Name partialInfo.Name
GlueASTViewer.Members partialInfo.Members
GlueASTViewer.TypeParameters partialInfo.TypeParameters
]
context

| GlueType.Record recordInfo ->
ASTViewer.renderNode
"Record"
[
ASTViewer.renderNode "KeyType" [
GlueASTViewer.GlueType recordInfo.KeyType
| GlueType.UtilityType utilityType ->
match utilityType with
| GlueUtilityType.Partial partialInfo ->
ASTViewer.renderNode
"Partial"
[
GlueASTViewer.Name partialInfo.Name
GlueASTViewer.Members partialInfo.Members
GlueASTViewer.TypeParameters partialInfo.TypeParameters
]
ASTViewer.renderNode "ValueType" [
GlueASTViewer.GlueType recordInfo.ValueType
context

| GlueUtilityType.Record recordInfo ->
ASTViewer.renderNode
"Record"
[
ASTViewer.renderNode "KeyType" [
GlueASTViewer.GlueType recordInfo.KeyType
]
ASTViewer.renderNode "ValueType" [
GlueASTViewer.GlueType recordInfo.ValueType
]
]
]
context
context

| GlueType.ThisType typeName ->
ASTViewer.renderNode
Expand Down

0 comments on commit 70a7ea9

Please sign in to comment.