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.52k stars 191 forks source link

12$: diagnostic: typeof operator should warn when comparing to invalid string literals #1213

Open CoderMuffin opened 5 months ago

CoderMuffin commented 5 months ago

When performing checks like typeof x == "string", it is perfectly possible to misspell it as something like typeof x == "strng" (missing i), which will always return false. Maybe there should be a warning for this?

More generally, expressions of the form typeof <a> [== or != or === or !==] <b> where b is a string and not "undefined", "object", "boolean", "number", "bigint", "string", "symbol" or "function"

I would be happy to implement this if approved and no-one else wants to :)

strager commented 3 months ago

This is a great idea! I'll put 12$ on it.

milanjijo commented 3 months ago

Hey Strager, I would love to try and tackle this issue!

singalhimanshu commented 2 months ago

@milanjijo are you working on this task?

singalhimanshu commented 2 months ago

@strager Is there any way to currently check a string literal against this list ("undefined", "object", "boolean", "number", "bigint", "string", "symbol" or "function") in the code?

CoderMuffin commented 2 months ago

@singalhimanshu yes, you can compare the Expression::Literal->span()->string_view() to each element of the array. The detection code for a similar diagnostic which might help can be seen here: src/quick-lint-js/fe/parse.cpp:524