-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
valid-typeof
does not report invalid comparisons to undefined
#6698
Comments
How should we handle the case like this: var undefined = "undefined";
if (typeof window === undefined) {
foo();
} ESLint is a static analysis tool. We can't track values of variables, and |
That said, eslint certainly should be able to track the values of variables in the same file, and couldn't |
Thanks for the explanation @ilyavolodin; I had forgotten that it was possible to redeclare Based on this table, it seems like all modern browsers don't allow |
Unfortunately, tracking value in JS is not quite that easy: eval("var und" + "efined = \"undefined\""); //or a million other ways to set a value of a variable
if (typeof window === undefined) {
foo();
} We currently don't have a way to reliably track variable values. It's something we want to look into in the future, but nothing is currently available for this. @not-an-aardvark We don't set requirements for JS support, there are plenty of sites that still have to support IE8 (or even IE6 in some cases) out there, so we can't assume that everyone is writing ES5+ (I know I don't, most of the time). |
@ilyavolodin would it not be reasonable to say that if a user is doing something like that, they are welcome to turn off the linter rule? |
@ilyavolodin Fair enough. While I understand your reasoning, it's unfortunate that we have to account for that, since I'm guessing statements such as |
@ljharb That's one way to go, but there are much simpler cases too: var undefined = require('./my-file'); In any case, while we can track variable declarations currently through Escope, it doesn't track variable values. As I said, that's something we want to look into in the future. For now we just don't have the tools required to do this. |
I think this would still be useful, even if there exists some edge cases it would be a false positive on. Anyone doing eslint isn't just to catch errors, and most rules don't apply to every scenario, which is why they're configurable. If this was an option - especially defaulted to off - then any users who wish to redefine |
Very well, that's a valid argument. I marked this issue as evaluating and enhancement. Let's see what the team thinks. |
I definitely think And I would argue that this concern (wanting to compare against string) has nothing whatsoever to do with the value of |
I think the only way we can possibly do something that makes sense is to add an option like |
Ping on this issue; is there enough support for it that a PR to add the |
I can get behind that option. 👍 from me. |
I'll support an option for comparing to literals only. 👍 |
@ilyavolodin Literals === string literals, or any literals? Not much point in allowing comparisons to boolean or numeric literals, no? |
Sorry, I meant string literals. |
I'll 👍 a |
👍 I'll champion, marking as accepted! I'll review #6923 later today. |
What version of ESLint are you using? 3.1.0
What parser (default, Babel-ESLint, etc.) are you using? default
Please show your full configuration:
What did you do? Please include the actual source code causing the issue.
What did you expect to happen?
I expect ESLint to report an error, since the global
undefined
value is not a valid typeof comparison.What actually happened? Please include the actual, raw output from ESLint.
ESLint did not detect any errors.
The text was updated successfully, but these errors were encountered: