deno_lint icon indicating copy to clipboard operation
deno_lint copied to clipboard

Rule suggestion: no-compare-to-new

Open rotu opened this issue 10 months ago • 1 comments

Using ===, ==, or switch with an object or array literal is always false.

The below are all examples of erroneous comparisons that could be flagged:

  • x === {}
  • switch (x) { case {}: /* ... */ }
  • Object.is(x, {}) (assuming Object.is has not been deleted or tampered with)

Additionally, if the type of x is known, the following could be flagged:

  • x == {} if x is not an object
  • x.includes({}), x.indexOf({}), x.lastIndexOf({}) if x is an Array
  • x.has({}), x.get({}), x.delete({}) if x is a Map or WeakMap
  • x.has({}), x.delete({}) if x is a Set or WeakSet

Similar to this suggested eslint rule: https://github.com/eslint/eslint/issues/15222

rotu avatar Aug 08 '23 16:08 rotu

Seems reasonable 👍 contributions are welcome

bartlomieju avatar May 10 '24 00:05 bartlomieju