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

[New] compare .toString, .valueOf, and [Symbol.toPrimitive] completions #79

Merged
merged 1 commit into from
Oct 5, 2023

Conversation

arasmussen
Copy link
Contributor

@arasmussen arasmussen commented Sep 27, 2023

If val or other are objects:

  • Compares val.toString() and other.toString()
  • Compares val.valueOf() and other.valueOf()
  • Uses es-to-primitive to perform the above checks, with two separate hints (String and Number)
  • Also considers that toString and valueOf can throw, and makes sure both or neither arguments throw

Test plan:

  • Adds test coverage for all of the above
  • tests + lint pass, test coverage does not regress

…pletions

Co-authored-by: Andrew Rasmussen <andrew@canny.io>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
@arasmussen arasmussen mentioned this pull request Sep 27, 2023
@socket-security
Copy link

New dependencies detected. Learn more about Socket for GitHub ↗︎

Packages Version New capabilities Transitives Size Publisher
es-to-primitive 1.2.1 None +0 40.4 kB ljharb

@arasmussen
Copy link
Contributor Author

arasmussen commented Sep 27, 2023

@ljharb hmm how do you want to proceed here? If we're adding toString comparisons then technically this test should be flipped. Alternatively, if you want to maintain backwards compatibility, we could skip the toString comparison if we detect typeof val === 'function'?

> const foo = Object(function(){});
undefined
> const bar = Object(function () {});
undefined
> foo.toString()
'function(){}'
> bar.toString()
'function () {}'

test/why.js Outdated Show resolved Hide resolved
why.js Outdated Show resolved Hide resolved
test/why.js Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Sep 28, 2023

Codecov Report

All modified lines are covered by tests ✅

Files Coverage Δ
why.js 99.54% <100.00%> (+0.08%) ⬆️

📢 Thoughts on this report? Let us know!.

@arasmussen
Copy link
Contributor Author

@ljharb updated PR per your feedback. pretty sure the one test failure is flaky but don't think I'm able to re-run.

test/native.js Outdated Show resolved Hide resolved
why.js Outdated Show resolved Hide resolved
why.js Outdated Show resolved Hide resolved
@arasmussen
Copy link
Contributor Author

@ljharb thanks for the feedback! ready for another pass, hopefully last one 😄

var valueIsFn = typeof value === 'function';
var otherIsFn = typeof other === 'function';

if (!valueIsFn || !otherIsFn) {
Copy link
Member

Choose a reason for hiding this comment

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

why this line? surely either a function or an object could both have toStrings or valueOfs that need comparing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

otherwise functions with same arity/name/body are equal despite whitespace between signature and body will fail because:

> const foo = Object(function(){});
undefined
> const bar = Object(function () {});
undefined
> foo.toString()
'function(){}'
> bar.toString()
'function () {}'

our options are to include this line, or delete that test. including this line is more backwards compatible.

Copy link
Member

Choose a reason for hiding this comment

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

aha, hmm. that means this new functionality wouldn't apply to functions, which is weird. at the least, let's document that in the readme?

an alternative, tho, would be to skip the check for a function whenever the function's toString is === to Function.prototype.toString?

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Looks great overall; I've rebased and squashed it.

why.js Outdated Show resolved Hide resolved
@ljharb ljharb changed the title [Fix] Compare object.toString and object.valueOf, including throws [New] compare .toString, .valueOf, and [Symbol.toPrimitive] completions Oct 5, 2023
@ljharb ljharb merged commit fa8d0ae into inspect-js:main Oct 5, 2023
306 of 309 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants