From 4a90f15523df8e06960bdad7101bc3fbb5985430 Mon Sep 17 00:00:00 2001 From: Maxime Mangel Date: Mon, 10 Mar 2025 22:23:26 +0100 Subject: [PATCH] [TS] Sanitize DUs case names when generating constructor function (#4072) Fix #4071 --- src/Fable.Cli/CHANGELOG.md | 1 + src/Fable.Compiler/CHANGELOG.md | 1 + src/Fable.Transforms/Fable2Babel.fs | 4 ++-- tests/Js/Main/UnionTypeTests.fs | 13 ++++++++++++- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Fable.Cli/CHANGELOG.md b/src/Fable.Cli/CHANGELOG.md index 762c842ec2..521745a349 100644 --- a/src/Fable.Cli/CHANGELOG.md +++ b/src/Fable.Cli/CHANGELOG.md @@ -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 diff --git a/src/Fable.Compiler/CHANGELOG.md b/src/Fable.Compiler/CHANGELOG.md index a84aef339e..a4cce698d9 100644 --- a/src/Fable.Compiler/CHANGELOG.md +++ b/src/Fable.Compiler/CHANGELOG.md @@ -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 diff --git a/src/Fable.Transforms/Fable2Babel.fs b/src/Fable.Transforms/Fable2Babel.fs index d2e51ddee0..088a5a129d 100644 --- a/src/Fable.Transforms/Fable2Babel.fs +++ b/src/Fable.Transforms/Fable2Babel.fs @@ -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 @@ -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) diff --git a/tests/Js/Main/UnionTypeTests.fs b/tests/Js/Main/UnionTypeTests.fs index 25a58a03a1..534f67666c 100644 --- a/tests/Js/Main/UnionTypeTests.fs +++ b/tests/Js/Main/UnionTypeTests.fs @@ -92,6 +92,11 @@ type T8 = T8 type T9 = T9 type 'a more = More of 'a +[] +type LangCode = + | EN + | ``pt-Br`` + let tests = testList "Unions" [ testCase "Union cases matches with no arguments can be generated" <| fun () -> @@ -261,4 +266,10 @@ let tests = U9> = !^42 x |> equal (U9.Case4 42) #endif - ] \ No newline at end of file + + 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`` + ]