Skip to content

Commit 61af16e

Browse files
authored
[JS/TS] Fixed quotation for union string cases (#4420)
1 parent a4286f0 commit 61af16e

5 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/Fable.Cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
* [JS/TS] Fix `Unchecked.defaultof<'T>` for struct types with fields returning `undefined` instead of zero-initialized values (by @MangelMaxime)
13+
* [JS/TS] Fixed quotation for union string cases (by @MangelMaxime)
1314
* [Python] Fix `Unchecked.defaultof<'T>` for struct types with fields returning incorrect values instead of zero-initialized instances (by @MangelMaxime)
1415
* [Python] Fix `Unchecked.defaultof<char>` returning `""` (empty string) instead of `"\u0000"` (null character) (by @MangelMaxime)
1516
* [Python] Improve `Unchecked.defaultof<_>` for declared entities (by @MangelMaxime)

src/Fable.Compiler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
* [JS/TS] Fix `Unchecked.defaultof<'T>` for struct types with fields returning `undefined` instead of zero-initialized values (by @MangelMaxime)
13+
* [JS/TS] Fixed quotation for union string cases (by @MangelMaxime)
1314
* [Python] Fix `Unchecked.defaultof<'T>` for struct types with fields returning incorrect values instead of zero-initialized instances (by @MangelMaxime)
1415
* [Python] Fix `Unchecked.defaultof<char>` returning `""` (empty string) instead of `"\u0000"` (null character) (by @MangelMaxime)
1516
* [Python] Improve `Unchecked.defaultof<_>` for declared entities (by @MangelMaxime)

src/fable-library-ts/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Fixed
11+
12+
* [JS/TS] Fixed quotation for union string cases (by @MangelMaxime)
13+
1014
## 2.0.0-rc.4 - 2026-03-19
1115

1216
### Fixed

src/fable-library-ts/Types.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,23 @@ export function toString(x: any, callStack = 0): string {
3939
}
4040

4141
export function unionToString(name: string, fields: any[]) {
42+
function unionFieldToString(x: any): string {
43+
if (typeof x === "string") {
44+
return '"' + x + '"';
45+
}
46+
return toString(x);
47+
}
48+
4249
if (fields.length === 0) {
4350
return name;
4451
} else {
4552
let fieldStr;
4653
let withParens = true;
4754
if (fields.length === 1) {
48-
fieldStr = toString(fields[0]);
55+
fieldStr = unionFieldToString(fields[0]);
4956
withParens = fieldStr.indexOf(" ") >= 0;
5057
} else {
51-
fieldStr = fields.map((x: any) => toString(x)).join(", ");
58+
fieldStr = fields.map((x: any) => unionFieldToString(x)).join(", ");
5259
}
5360
return name + (withParens ? " (" : " ") + fieldStr + (withParens ? ")" : "");
5461
}

tests/Js/Main/UnionTypeTests.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ type LangCode =
9797
| EN
9898
| ``pt-Br``
9999

100+
type S = S of string
101+
100102
let tests =
101103
testList "Unions" [
102104
testCase "Union cases matches with no arguments can be generated" <| fun () ->
@@ -272,4 +274,8 @@ let tests =
272274
let x = LangCode.``pt-Br``
273275

274276
equal x LangCode.``pt-Br``
277+
278+
testCase "sprintf formats strings cases correctly" <| fun () ->
279+
let s = sprintf "%A" (S "1")
280+
equal s "S \"1\""
275281
]

0 commit comments

Comments
 (0)