Skip to content

Commit

Permalink
fix: add support for literal enums with @, <, >, (space)
Browse files Browse the repository at this point in the history
[converter][web]

Fix #133
  • Loading branch information
MangelMaxime committed Oct 18, 2024
1 parent 297c7f9 commit 883829c
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Glutinum.Converter/Utils/Naming.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ let private escapeName (name: string) : string =
name.Contains("-")
|| name.Contains("$")
|| name.Contains("#")
|| name.Contains("<")
|| name.Contains(">")
|| name.Contains(" ")
|| startWithDigit name
|| Keywords.fsharp.Contains name
then
Expand All @@ -36,10 +39,13 @@ let removeSurroundingQuotes (text: string) : string =

let replaceDot (text: string) : string = text.Replace(".", "_")

let replaceAt (text: string) : string = text.Replace("@", "_AT_")

type SanitizeNameResult = { Name: string; IsDifferent: bool }

let sanitizeNameWithResult (name: string) : SanitizeNameResult =
let sanitizedName = name |> replaceDot |> removeSurroundingQuotes
let sanitizedName =
name |> replaceDot |> replaceAt |> removeSurroundingQuotes

// Check if the name is different after sanitization
// This is used to check if the value is different from the default Fable computed value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type Name =
| 'an@nymous>';
14 changes: 14 additions & 0 deletions tests/specs/references/enums/literalStringEnumWithAtSymbol.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module rec Glutinum

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

[<RequireQualifiedAccess>]
[<StringEnum(CaseRules.None)>]
type Name =
| [<CompiledName("an@nymous>")>] ``an_AT_nymous>``

(***)
#r "nuget: Fable.Core"
(***)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type Name =
| '<anonymous';
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module rec Glutinum

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

[<RequireQualifiedAccess>]
[<StringEnum(CaseRules.None)>]
type Name =
| ``<anonymous``

(***)
#r "nuget: Fable.Core"
(***)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type Name =
| 'anonymous>';
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module rec Glutinum

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

[<RequireQualifiedAccess>]
[<StringEnum(CaseRules.None)>]
type Name =
| ``anonymous>``

(***)
#r "nuget: Fable.Core"
(***)
2 changes: 2 additions & 0 deletions tests/specs/references/enums/literalStringEnumWithSpace.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type Name =
| 'anony mous';
14 changes: 14 additions & 0 deletions tests/specs/references/enums/literalStringEnumWithSpace.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module rec Glutinum

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

[<RequireQualifiedAccess>]
[<StringEnum(CaseRules.None)>]
type Name =
| ``anony mous``

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

0 comments on commit 883829c

Please sign in to comment.