thechangelog / changelog.com

Changelog is news and podcast for developers. This is our open source platform.

Home Page:https://changelog.com/posts/changelog-is-open-source

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bugs in negated `in` expressions

gorosgobe opened this issue · comments

in and instanceof expressions in JS

a in obj;
a instanceof C;

can be negated by grouping them and applying the ! operator, i.e.

!(a in obj);
!(a instanceof C);

Applying the ! operator incorrectly (on the LHS operand) leads to bugs:

!a in obj; // will evaluate to false, unless obj has a "true" or "false" key
!a instanceof C; // will evaluate to false, unless C overrides instanceof with a @@hasInstance method

For more information, please see these MDN docs and the no-unsafe-negation recommended Eslint rule.

I have found a potentially problematic instance of the above bugs in your codebase:

if (!"execCommand" in document) return event;

@gorosgobe Beautiful issue. Its gorgeous and wonderfully documented

I did a search of the repo looking for any other cases which looks to only identify the file mentioned.

Thank you for submitting this @gorosgobe!

WDYT @jerodsanto?