Skip to content

Commit

Permalink
[TS] Sanitize DUs case names when generating constructor function (#4072
Browse files Browse the repository at this point in the history
)

Fix #4071
  • Loading branch information
MangelMaxime authored Mar 10, 2025
1 parent 048f27e commit 4a90f15
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

* [JS/TS] Make `nullArgCheck` report the same error message as on .NET (by @MangelMaxime)
* [TS] Sanitize DUs case names when generating constructor function (by @MangelMaxime)

## 5.0.0-alpha.11 - 2025-03-03

Expand Down
1 change: 1 addition & 0 deletions src/Fable.Compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

* [JS/TS] Make `nullArgCheck` report the same error message as on .NET (by @MangelMaxime)
* [TS] Sanitize DUs case names when generating constructor function (by @MangelMaxime)

## 5.0.0-alpha.11 - 2025-03-03

Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,7 @@ module Util =
if com.IsTypeScript then
match List.tryItem tag ent.UnionCases with
| Some case ->
match tryJsConstructorWithSuffix com ctx ent ("_" + case.Name) with
match tryJsConstructorWithSuffix com ctx ent ("_" + sanitizeName case.Name) with
| Some helperRef ->
let typeParams = makeTypeParamInstantiation com ctx genArgs

Expand Down Expand Up @@ -3718,7 +3718,7 @@ module Util =
)
)

let fnId = entName + "_" + case.Name |> Identifier.identifier
let fnId = entName + "_" + sanitizeName case.Name |> Identifier.identifier
// Don't use return type, TypeScript will infer it and sometimes we want to use
// the actual constructor type in case it implements an interface
// let returnType = AliasTypeAnnotation(Identifier.identifier(entName + UnionHelpers.UNION_SUFFIX), entParamsInst)
Expand Down
13 changes: 12 additions & 1 deletion tests/Js/Main/UnionTypeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ type T8 = T8
type T9 = T9
type 'a more = More of 'a

[<RequireQualifiedAccess>]
type LangCode =
| EN
| ``pt-Br``

let tests =
testList "Unions" [
testCase "Union cases matches with no arguments can be generated" <| fun () ->
Expand Down Expand Up @@ -261,4 +266,10 @@ let tests =
U9<T1 more, T2 more, T3 more, int, T5 more, T6 more, T7 more, T8 more, T9 more>> = !^42
x |> equal (U9.Case4 42)
#endif
]

testCase "Verify that union cases can have `-` in their names" <| fun () ->
// See https://github.com/fable-compiler/Fable/issues/4071
let x = LangCode.``pt-Br``

equal x LangCode.``pt-Br``
]

0 comments on commit 4a90f15

Please sign in to comment.