diff --git a/CHANGELOG.md b/CHANGELOG.md index bc20804..cc97828 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Glutinum.Converter/Utils/Naming.fs b/src/Glutinum.Converter/Utils/Naming.fs index 9a5f3ea..466fe3a 100644 --- a/src/Glutinum.Converter/Utils/Naming.fs +++ b/src/Glutinum.Converter/Utils/Naming.fs @@ -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 diff --git a/tests/specs/enums/literalStringEnumStartingWithDigit.d.ts b/tests/specs/enums/literalStringEnumStartingWithDigit.d.ts new file mode 100644 index 0000000..2228c69 --- /dev/null +++ b/tests/specs/enums/literalStringEnumStartingWithDigit.d.ts @@ -0,0 +1,2 @@ +export type GPUTextureViewDimension = + | "1d" diff --git a/tests/specs/enums/literalStringEnumStartingWithDigit.fsx b/tests/specs/enums/literalStringEnumStartingWithDigit.fsx new file mode 100644 index 0000000..354ee33 --- /dev/null +++ b/tests/specs/enums/literalStringEnumStartingWithDigit.fsx @@ -0,0 +1,13 @@ +module rec Glutinum + +open Fable.Core +open System + +[] +[] +type GPUTextureViewDimension = + | ``1d`` + +(***) +#r "nuget: Fable.Core" +(***)