Skip to content

Commit

Permalink
fix: prevent infinite loop on recursive type
Browse files Browse the repository at this point in the history
[converter]

=== changelog ===
```ts
export interface DiagnosticCollection {
    forEach(callback: (collection: DiagnosticCollection) => any): void;
}
```

```fs
[<AllowNullLiteral>]
[<Interface>]
type DiagnosticCollection =
    abstract member forEach: callback: (DiagnosticCollection -> obj) -> unit
```
=== changelog ===

Fix #171
  • Loading branch information
MangelMaxime committed Dec 5, 2024
1 parent 9d66d46 commit 092b4f7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/Glutinum.Converter/Transform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -572,16 +572,19 @@ let rec private transformType (context: TransformContext) (glueType: GlueType) :
Name = mapTypeNameToFableCoreAwareName context typeReference
FullName = typeReference.FullName
TypeArguments = typeReference.TypeArguments |> List.map (transformType context)
Type =
context.TypeMemory
|> List.tryFind (fun glueType ->
match glueType with
| GlueType.Interface glueInterface ->
glueInterface.FullName = typeReference.FullName
| _ -> false
)
|> Option.map (transformType context)
|> Option.defaultValue FSharpType.Discard
Type = FSharpType.Discard
// We don't want to transform the type here, because if the type use itself
// we will end up in an infinite loop.
// Can be revisited if needed
// context.TypeMemory
// |> List.tryFind (fun glueType ->
// match glueType with
// | GlueType.Interface glueInterface ->
// glueInterface.FullName = typeReference.FullName
// | _ -> false
// )
// |> Option.map (transformType context)
// |> Option.defaultValue FSharpType.Discard
}
: FSharpTypeReference)
|> FSharpType.TypeReference
Expand Down
3 changes: 3 additions & 0 deletions tests/specs/references/regressions/recursive-type-1.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface DiagnosticCollection {
forEach(callback: (collection: DiagnosticCollection) => any): void;
}
15 changes: 15 additions & 0 deletions tests/specs/references/regressions/recursive-type-1.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module rec Glutinum

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

[<AllowNullLiteral>]
[<Interface>]
type DiagnosticCollection =
abstract member forEach: callback: (DiagnosticCollection -> obj) -> unit

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

0 comments on commit 092b4f7

Please sign in to comment.