-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
doc: document asserts Weak(Map|Set) behavior #18248
Conversation
Right now it is not documentated that WeakMap entries are not compared. This might result in some confusion. This adds a note about the behavior in `assert.deepStrictEqual`. This documentation is also references in `util.isDeepStrictEqual`, so we do not have to document it again for that function as the underlying algorithm is the same.
doc/api/assert.md
Outdated
@@ -254,6 +255,16 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 }); | |||
// OK, because it is the same symbol on both objects. | |||
assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 }); | |||
// Fails because symbol1 !== symbol2! | |||
|
|||
const weakMap1 = new WeakMap(); | |||
const weakMap2 = new WeakMap([1, 1]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WeakMap throws an error in this code. I guess this example is WeakSet
.
doc/api/assert.md
Outdated
@@ -870,6 +881,8 @@ second argument. This might lead to difficult-to-spot errors. | |||
[`Set`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Set | |||
[`Symbol`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Symbol | |||
[`TypeError`]: errors.html#errors_class_typeerror | |||
[`WeakMap`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeapMap |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
linter errors:
doc/api/assert.md
884:1-884:99 warning Found unused definition no-unused-definitions remark-lint
885:1-885:99 warning Found unused definition no-unused-definitions remark-lint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/WeapMap
-> /WeakMap
-----^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
🙈 comments addressed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer we fix the actual behavior. If Map
are compared by entries WeakMap
should be compared by entries too.
doc/api/assert.md
Outdated
@@ -213,6 +213,8 @@ are recursively evaluated also by the following rules. | |||
* Map keys and Set items are compared unordered. | |||
* Recursion stops when both sides differ or both sides encounter a circular | |||
reference. | |||
* [`WeakMap`][] and [`WeakSet`][] will return true, no matter what values they | |||
contain. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sentence is not clear. Also can you use the present instead of the future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
['WeakMap'][] and ['WeakSet'][] comparison does not rely on their values. See below for further details.
?
@mcollina it is not possible to compare WeakMap and WeakSet entries. That is the whole point about this PR. You can not look into the WeakMap. You can only check if a entry exists or not but you have to know what entry that is. |
I'd prefer if the behavior was changed to throw as suggested in #18228. In that case, this should not land on master. |
Mini poll: 👍 document the current behavior (this PR) Please note that this applies to both: |
The only thing I'm not ok with is to document the current behavior. |
@mcollina in #18228 (comment) I mentioned why I do not like to always fail (as in return false). That was the reason for this PR. I guess I am fine with throwing. It might be a bit confusing for some users but that is the case one way or the other. |
I'm fine with documenting this behavior as a warning to users. I'd just rather not land that warning on master at all. |
I don't see why WeakMap and WeakSet should be treated differently from regular objects. Thus, documentation only is the best option IMO. |
I personally also think that WeakMap and WeakSet should be treated as any other regular object. The only reason why I would be OK with throwing is that many people seem to be confused about the behavior and that might clarify it right away. It might of course also lead to some trouble. |
I'm for documenting the current behavior. |
Documenting is definitely something we should land in 8 and 9.
I disagree, people look at the docs when they see an exception or undocumented behavior. However this will be mostly silent. I compare two things that I expect to contain the same keys, but they are not and the test will not fail. Comparing the attached properties of a WeakMap is definitely not the behavior a user is looking for. |
this situation is similar to However, util.isDeepStrictEqual returns If we define but i feel it is not """strict equal""" ... According to the api docs, If we follow the sentence, I think we would be better to return If we could change this function behavior drastically, throw an Error on uncomparable object like (NaN, WeakMap, WeakSet). Anyway, we need to add some documentation for this function. |
My preference would be:
|
@yosuke-furukawa I am not sure in what way And I already tried to point out that using |
Ok, it still seems highly controversial what to do and what not. The only relatively clear thing is that always returning false / always failing the check in case So either we should document the behavior or throw. @nodejs/tsc I think this is something for your agenda. Please read the arguments closely. |
Right now it is not documentated that WeakMap entries are not compared. This might result in some confusion. This adds a note about the behavior in `assert.deepStrictEqual`. This documentation is also references in `util.isDeepStrictEqual`, so we do not have to document it again for that function as the underlying algorithm is the same. PR-URL: nodejs#18248 Fixes: nodejs#18228 Refs: nodejs#18228 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
Landed in 936eea5 The question to the TSC still stands nevertheless. |
But we could change this function behavior to truly
firstly I think this is the better behavior than current behavior. Because most JS engineer using WeakMap and WeakSet know that they do not have a list API and those collections are depends on GC. WeakCollection has non-determinism. The strict compare is impossible, so false.
IMO It is not weird, cause WeakMap|Set are special collection about comparison, we need special handles.
This is a kind of type error. it means "we do not support WeakMap|WeakSet on this function".
Returning undefined is a compromised plan. It means "no answer (neither true nor false)". |
Should this be backported to |
Right now it is not documentated that WeakMap entries are not compared. This might result in some confusion. This adds a note about the behavior in `assert.deepStrictEqual`. This documentation is also references in `util.isDeepStrictEqual`, so we do not have to document it again for that function as the underlying algorithm is the same. PR-URL: nodejs#18248 Fixes: nodejs#18228 Refs: nodejs#18228 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
Backport opened in #19230 |
Right now it is not documentated that WeakMap entries are not compared. This might result in some confusion. This adds a note about the behavior in `assert.deepStrictEqual`. This documentation is also references in `util.isDeepStrictEqual`, so we do not have to document it again for that function as the underlying algorithm is the same. Backport-PR-URL: #19230 PR-URL: #18248 Fixes: #18228 Refs: #18228 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
Right now it is not documentated that WeakMap entries are not compared. This might result in some confusion. This adds a note about the behavior in `assert.deepStrictEqual`. This documentation is also references in `util.isDeepStrictEqual`, so we do not have to document it again for that function as the underlying algorithm is the same. Backport-PR-URL: #19230 PR-URL: #18248 Fixes: #18228 Refs: #18228 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
Can we remove tsc-* labels? |
I would say so. It is still a matter of finding the right way to continue but that should be done in the corresponding issue. |
Right now it is not documentated that WeakMap entries are not compared. This might result in some confusion. This adds a note about the behavior in `assert.deepStrictEqual`. This documentation is also references in `util.isDeepStrictEqual`, so we do not have to document it again for that function as the underlying algorithm is the same. PR-URL: nodejs#18248 Fixes: nodejs#18228 Refs: nodejs#18228 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
Requested backport to 8.x in #19230 |
Right now it is not documentated that WeakMap entries are not
compared. This might result in some confusion. This adds a note
about the behavior in
assert.deepStrictEqual
. This documentationis also references in
util.isDeepStrictEqual
, so we do not haveto document it again for that function as the underlying algorithm
is the same.
Refs: #18228
Closes: #18228
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
doc