Skip to content

util: fix formatting of objects with built-in Symbol.toPrimitive #57832

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

islandryu
Copy link
Contributor

Fixes: #57818

Imo, built-in [Symbol.toPrimitive] can make use of inspect, just like built-in toString, because for built-in objects like Symbol and Date, their [Symbol.toPrimitive] and toString() return the same result.

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module. labels Apr 11, 2025
@legendecas legendecas added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Apr 11, 2025
Copy link

codecov bot commented Apr 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.23%. Comparing base (93644d5) to head (5b0ac9b).
Report is 40 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #57832      +/-   ##
==========================================
- Coverage   90.24%   90.23%   -0.02%     
==========================================
  Files         630      630              
  Lines      185197   185481     +284     
  Branches    36309    36367      +58     
==========================================
+ Hits       167136   167372     +236     
- Misses      10984    10996      +12     
- Partials     7077     7113      +36     
Files with missing lines Coverage Δ
lib/internal/util/inspect.js 99.91% <100.00%> (-0.05%) ⬇️

... and 55 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

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

The rough implementation seems good!
What I would like to change to land this, is to improve the details (performance and potentially correctness edge cases).

Can you please add a few tests that also check for behavior on prototypes? We need a few test cases there. An object that inherits from a built-in that has either or method as property, both, and the same for an inheritance where it's inheriting from a user defined class.

Copy link
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the quick follow-ups! :)

@BridgeAR BridgeAR added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 15, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 15, 2025
@nodejs-github-bot
Copy link
Collaborator

@BridgeAR BridgeAR added commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. commit-queue Add this label to land a pull request using GitHub Actions. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. labels Apr 15, 2025
@nodejs-github-bot nodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels Apr 15, 2025
@nodejs-github-bot
Copy link
Collaborator

Commit Queue failed
- Loading data for nodejs/node/pull/57832
✔  Done loading data for nodejs/node/pull/57832
----------------------------------- PR info ------------------------------------
Title      util: fix formatting of objects with built-in Symbol.toPrimitive (#57832)
   ⚠  Could not retrieve the email or name of the PR author's from user's GitHub profile!
Branch     islandryu:fix/format -> nodejs:main
Labels     util, author ready, needs-ci, commit-queue-squash
Commits    6
 - util: fix formatting of objects with built-in Symbol.toPrimitive
 - add symbol test
 - handle the case toPrimitive is defined in prototype
 - add test
 - add comment
 - fix to not call unnecessary hasOwnProperty
Committers 1
 - islandryu <[email protected]>
PR-URL: https://github.com/nodejs/node/pull/57832
Fixes: https://github.com/nodejs/node/issues/57818
Reviewed-By: Ruben Bridgewater <[email protected]>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/57832
Fixes: https://github.com/nodejs/node/issues/57818
Reviewed-By: Ruben Bridgewater <[email protected]>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Fri, 11 Apr 2025 12:37:28 GMT
   ✔  Approvals: 1
   ✔  - Ruben Bridgewater (@BridgeAR) (TSC): https://github.com/nodejs/node/pull/57832#pullrequestreview-2763991051
   ✘  This PR needs to wait 70 more hours to land (or 0 hours if there is one more approval)
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2025-04-15T00:35:13Z: https://ci.nodejs.org/job/node-test-pull-request/66272/
- Querying data for job/node-test-pull-request/66272/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu
https://github.com/nodejs/node/actions/runs/14471235480

Comment on lines +2198 to +2200
function returnFalse() {
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

is this faster than nulling out hasOwnToPrimitive and checking for that, avoiding the function call?

Copy link
Member

Choose a reason for hiding this comment

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

That likely depends on a) the inlining and b) if it is more common to have to check for both methods (if that's the case, any additional check is more expensive).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-failed An error occurred while landing this pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Date changed representation when using utils.format() with "%s"
5 participants