eslint: `valid-typeof` does not report invalid comparisons to `undefined`

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:

{
  "rules": {
    "valid-typeof": 2
  }
}

What did you do? Please include the actual source code causing the issue.

// oops, this was probably supposed to be the string 'undefined'
if (typeof window === undefined) {
  foo();
}

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.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 18 (18 by maintainers)

Commits related to this issue

Most upvoted comments

I think the only way we can possibly do something that makes sense is to add an option like requireStringLiterals that ensures the RHS is always a string. Anything else is just guessing and not appropriate for this rule.

Sorry, I meant string literals.

👍 I’ll champion, marking as accepted! I’ll review #6923 later today.

Very well, that’s a valid argument. I marked this issue as evaluating and enhancement. Let’s see what the team thinks.

@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 typeof x === undefined are errors much more often than not.

undefined can be redefined everywhere, except in the global scope in ES5+.

That said, eslint certainly should be able to track the values of variables in the same file, and couldn’t var undefined, let undefined, or const undefined being present cause the requested warning to be skipped?

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 undefined can be reassigned when using ES3.