Skip to content

Commit

Permalink
String enums starting with a number should be escaped with backtick
Browse files Browse the repository at this point in the history
Fix #43
  • Loading branch information
MangelMaxime committed Feb 15, 2024
1 parent 0344c84 commit 49a7f4d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Optional argument of F# Method are prefixed with `?` instead of suffixing them with `option`
* Sanitize names coming from TypeScript by removing surrounding quotes (`"`, `'`)
* String enums containing a `-` should be escaped with backtick ([GH-44](https://github.com/glutinum-org/cli/issues/44))
* String enums starting with a number should be escaped with backtick ([GH-43](https://github.com/glutinum-org/cli/issues/43))

### Added

Expand Down
9 changes: 8 additions & 1 deletion src/Glutinum.Converter/Utils/Naming.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ let nameEqualsDefaultFableValue (name: string) (value: string) : bool =
let nameNotEqualsDefaultFableValue (name: string) (value: string) : bool =
not (nameEqualsDefaultFableValue name value)

let startWithDigit (name: string) : bool =
name.Length > 0 && Char.IsDigit name.[0]

let escapeName (name: string) : string =
if Keywords.fsharp.Contains name || name.Contains("-") then
if
name.Contains("-")
|| startWithDigit name
|| Keywords.fsharp.Contains name
then
$"``%s{name}``"
else
name
Expand Down
2 changes: 2 additions & 0 deletions tests/specs/enums/literalStringEnumStartingWithDigit.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type GPUTextureViewDimension =
| "1d"
13 changes: 13 additions & 0 deletions tests/specs/enums/literalStringEnumStartingWithDigit.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module rec Glutinum

open Fable.Core
open System

[<RequireQualifiedAccess>]
[<StringEnum(CaseRules.None)>]
type GPUTextureViewDimension =
| ``1d``

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

0 comments on commit 49a7f4d

Please sign in to comment.