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

improv: display String objects similar to node #17761

Merged

Conversation

Pranav2612000
Copy link
Contributor

@Pranav2612000 Pranav2612000 commented Feb 27, 2025

What does this PR do?

This commit adds special condition to print String Objects as [String "<string>"] in green. This is the same as node.
This fixes #17663

  • Code changes

How did you verify your code works?

Ran a script locally to ensure that the output is similar to Node
image

  • I included a test for the new code, or an existing test covers it

@Pranav2612000 Pranav2612000 force-pushed the improv/node-parity-logs-for-String-Object branch from efa5248 to 731c48b Compare February 27, 2025 13:16
@190n 190n added the release-notes This PR should be mentioned in release notes for the Bun version in which it lands label Feb 27, 2025
@@ -16,6 +16,7 @@ Infinity
-Infinity
Symbol(Symbol Description)
2000-06-27T02:24:34.304Z
[String: 'Hello']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment on console-log.js

Comment on lines 2139 to 2143
} else if (jsType == .StringObject) {
if (enable_ansi_colors) {
writer.print(comptime Output.prettyFmt("<r><green>", enable_ansi_colors), .{});
}
writer.print("[String: '", .{});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prints differently than Node.js if the string contains characters which need to be escaped:

// log.js
console.log(new String("\n"));
console.log(new String("'"));
console.log(new String("'\""));
console.log(new String("'\"`"));
$ node log.js
[String: '\n']
[String: "'"]
[String: `'"`]
[String: '\'"`']
$ bun-debug log.js
[String: '
']
[String: ''']
[String: ''"']
[String: ''"`']

I think it's acceptable, at least for now, to not exactly match Node's decision for which style of quote to use. But we should try to ensure that the thing after String: is a valid JS string literal. A decent compromise might be, if the string contains any characters that need to be escaped, to use similar logic to the if (this.quote_strings case on line 2114 which prints it as a JSON string (so it will always use double quotes).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it seems like Bun's util.inspect code already handles these edge cases escaping new String correctly, so maybe you could rely on the implementation there?

Copy link
Contributor Author

@Pranav2612000 Pranav2612000 Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it seems like Bun's util.inspect code already handles these edge cases escaping new String correctly, so maybe you could rely on the implementation there?

Do you mean this function

function strEscape(str) {
?

I'm guessing just the quotes part would be enough. Right? Or do we also need to do all the regex replaces ( example )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On reflection I think it will be much easier to use the JSON format case that I first mentioned:

A decent compromise might be, if the string contains any characters that need to be escaped, to use similar logic to the if (this.quote_strings case on line 2114 which prints it as a JSON string (so it will always use double quotes).

rather than call into, or duplicate the logic of, a bunch of JavaScript code. This won't match Node.js perfectly but it'll be better than status quo. I do think the most important part is just that the thing after String: is any kind of valid string literal, which it isn't always in the current state of your PR but it will be if you detect when escaping is needed and do a JSON-style escape with double quotes.

@190n
Copy link
Contributor

190n commented Feb 27, 2025

btw, set environment variable BUN_DEBUG_ALL=0 to make your local build less spammy.

@Pranav2612000 Pranav2612000 force-pushed the improv/node-parity-logs-for-String-Object branch 2 times, most recently from 9624aa4 to 1ec6ade Compare March 2, 2025 14:01
@Pranav2612000
Copy link
Contributor Author

@190n Is there anything else I can do to make this PR merge ready?

@190n
Copy link
Contributor

190n commented Mar 6, 2025 via email

This commit adds special condition to print String Objects as `[String
"<string>"]` in green. This is similar to how node handles String Object
@Pranav2612000 Pranav2612000 force-pushed the improv/node-parity-logs-for-String-Object branch from 92b3745 to e86aa06 Compare March 6, 2025 15:26
@nektro nektro merged commit 6e99733 into oven-sh:main Mar 6, 2025
66 of 70 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes This PR should be mentioned in release notes for the Bun version in which it lands
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wrong console logging for new String("t")
3 participants