-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
util.format('%s', o) fails to call String(o) in certain cases #30333
Labels
Comments
cpcallen
added a commit
to cpcallen/bugs
that referenced
this issue
Nov 8, 2019
The documentation says that util.format('%s', obj) will call String(obj) iff obj has a user-defined .toString method, but in v12.12.0 it only calls String(obj) if either obj or obj.constructor.prototype has an own property named toString, causing inherited toString implementations to be ignored. Submitted as nodejs/node#30333
BridgeAR
added
confirmed-bug
Issues with confirmed bugs.
util
Issues and PRs related to the built-in util module.
labels
Nov 8, 2019
@cpcallen thank you for the bug report! This is indeed not intended and it seems like we are missing a couple of test cases (so thank you very much for highlighting the different solutions). I will have a look at it next week. |
It's okay on |
Yes; as noted above it was introduced in |
3 tasks
4 tasks
cpcallen
added a commit
to google/CodeCity
that referenced
this issue
Nov 22, 2019
Work around nodejs/node#30333, introduced in node.js v12.3.0.
cpcallen
added a commit
to google/CodeCity
that referenced
this issue
Nov 28, 2019
Work around nodejs/node#30333, introduced in node.js v12.3.0.
cpcallen
added a commit
to google/CodeCity
that referenced
this issue
Nov 28, 2019
Work around nodejs/node#30333, introduced in node.js v12.3.0. (cherry picked from commit de7e974)
cpcallen
added a commit
to google/CodeCity
that referenced
this issue
Nov 29, 2019
Work around nodejs/node#30333, introduced in node.js v12.3.0. (cherry picked from commit de7e974)
targos
pushed a commit
that referenced
this issue
Dec 1, 2019
This makes sure that `util.format('%s', object)` will always call a user defined `toString` function. It was formerly not the case when the object had the function declared on the super class. At the same time this also makes sure that getters won't be triggered accessing the `constructor` property. PR-URL: #30343 Fixes: #30333 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
MylesBorins
pushed a commit
that referenced
this issue
Jan 30, 2020
This makes sure that `util.format('%s', object)` will always call a user defined `toString` function. It was formerly not the case when the object had the function declared on the super class. At the same time this also makes sure that getters won't be triggered accessing the `constructor` property. Backport-PR-URL: #31431 PR-URL: #30343 Fixes: #30333 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
BethGriggs
pushed a commit
that referenced
this issue
Feb 6, 2020
This makes sure that `util.format('%s', object)` will always call a user defined `toString` function. It was formerly not the case when the object had the function declared on the super class. At the same time this also makes sure that getters won't be triggered accessing the `constructor` property. Backport-PR-URL: #31431 PR-URL: #30343 Fixes: #30333 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
abhishekumar-tyagi
pushed a commit
to abhishekumar-tyagi/node
that referenced
this issue
May 5, 2024
This makes sure that `util.format('%s', object)` will always call a user defined `toString` function. It was formerly not the case when the object had the function declared on the super class. At the same time this also makes sure that getters won't be triggered accessing the `constructor` property. PR-URL: nodejs/node#30343 Fixes: nodejs/node#30333 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
I was very surprised to discover that in node v12,
util.format('%s', v)
does not consistently callString(v)
. Checking the docs, I read:This is a surprising (and in my view undesirable) change from the previous behaviour, which was to consistently call
String
in response to%s
. It also appears to be a semantically breaking change introduced, via #27621 and #27799, in 12.3.0, despite that not being a new major version. If so, it should be entirely reverted (and good riddance, in my opinion).If there is some compelling argument for why the new behaviour is actually desirable, then at least the code should be made consistent with the documentation or vice versa.
In particular, whether a user defined toString function is called depends in non-documented and non-obvious ways on the value of the
.constructor
property of the object, which is otherwise normally of no significance:Actual output:
Expected / documented output:
Consistently applying the documented behaviour would go some way to remedying the breaking nature of this change, since at least user supplied
.toString
implementations would always be called, as was previously the case.The text was updated successfully, but these errors were encountered: