Skip to content

Commit

Permalink
fix: Prevent infite loop when a class has a reference to a union type…
Browse files Browse the repository at this point in the history
… which reference the class itself

[converter]
  • Loading branch information
MangelMaxime committed Mar 28, 2024
1 parent c86a6d6 commit 6d19aae
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Glutinum.Converter/Reader/UnionTypeNode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ let rec private readUnionTypeCases
if ts.isParenthesizedTypeNode node then
let parenthesizedTypeNode = node :?> Ts.ParenthesizedTypeNode

let i = 0
removeParenthesizedType parenthesizedTypeNode.``type``
else
node
Expand Down Expand Up @@ -95,8 +94,23 @@ let rec private readUnionTypeCases
|> Some
else
let declaration = declarations.[0]

reader.ReadNode declaration |> List.singleton |> Some
// TODO: This is an optimitic approach
// But we should revisit how TypeReference is handled because of recursive types
match declaration.kind with
| Ts.SyntaxKind.TypeAliasDeclaration ->
reader.ReadNode declaration |> List.singleton |> Some

| _ ->
let fullName = checker.getFullyQualifiedName symbol

({
Name = typeReferenceNode.getText ()
FullName = fullName
TypeArguments = []
})
|> GlueType.TypeReference
|> List.singleton
|> Some

| None ->
let typ = checker.getTypeOfSymbol symbol
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export declare class MyClass {
contains(otherBoundsOrLatLng: MyUnion | string): boolean;
}

export type MyUnion = MyClass | string;
21 changes: 21 additions & 0 deletions tests/specs/references/class/classWithRecursiveUnionToItself.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module rec Glutinum

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

[<Erase>]
type Exports =
interface end

[<AllowNullLiteral>]
[<Interface>]
type MyClass =
abstract member contains: otherBoundsOrLatLng: U2<MyUnion, string> -> bool

type MyUnion =
U2<MyClass, string>

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

0 comments on commit 6d19aae

Please sign in to comment.