quick-lint / quick-lint-js

quick-lint-js finds bugs in JavaScript programs
https://quick-lint-js.com
GNU General Public License v3.0
1.55k stars 192 forks source link

incorrect diagnostic: `using a '.' after a '?.' might fail` #1192

Closed vegerot closed 10 months ago

vegerot commented 10 months ago

minimal example:

undefined?.a.b

quick-lint reports: using a '.' after a '?.' might fail, since '?.' might return 'undefined'., however this is not true.

Running the above code shows that optional chaining short-circuits the entire property access expression to undefined, not just the first property access.

❯ deno
Deno 1.39.4
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
> undefined?.a.b.c.d
undefined

idk if there's a "correct" version of this diagnostic, or if we should just delete the whole thing

vegerot commented 10 months ago

A better version of this diagnostic would be to check for non-null assertions, which are incorrect.

undefined?.a!.b
strager commented 10 months ago

Fixed in Git commit 434d08514d2ab0bc70b5ecb40f1e1e7c1256b6ae. I will ship the fix in version 3.2.0.

strager commented 8 months ago

Fix released in version 3.2.0.