Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JS/TS] Fix #4029: print formatting with anonymous records #4030

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* [Python] - Print root module and module function comments (by @alfonsogarciacaro)
* [JS/TS] - Fix anonymous record printing (#4029) (by @alfonsogarciacaro)

## 5.0.0-alpha.9 - 2025-01-28

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 @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* [Python] - Print root module and module function comments (by @alfonsogarciacaro)
* [JS/TS] - Fix anonymous record printing (#4029) (by @alfonsogarciacaro)

## 5.0.0-alpha.9 - 2025-01-28

Expand Down
2 changes: 1 addition & 1 deletion src/fable-library-ts/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function seqToString<T>(self: Iterable<T>): string {

export function toString(x: any, callStack = 0): string {
if (x != null && typeof x === "object") {
if (typeof x.toString === "function") {
if (typeof x.toString === "function" && x.toString !== Object.prototype.toString) {
return x.toString();
} else if (Symbol.iterator in x) {
return seqToString(x);
Expand Down
6 changes: 6 additions & 0 deletions tests/Js/Main/StringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ let tests = testList "Strings" [
$"Hi! My name is %s{person.Name} %s{person.Surname.ToUpper()}. I'm %i{person.Age} years old and I'm from %s{person.Country}!"
|> equal "Hi! My name is John DOE. I'm 32 years old and I'm from The United Kingdom!"

testCase "Printf %A works with anonymous records" <| fun () -> // See #4029
let person = {| FirstName = "John"; LastName = "Doe" |}
let s = sprintf "%A" person
System.Text.RegularExpressions.Regex.Replace(s.Replace("\"", ""), @"\s+", " ")
|> equal """{ FirstName = John LastName = Doe }"""

testCase "Interpolated strings keep empty lines" <| fun () ->
let s1 = $"""1

Expand Down
Loading