eslint: no-undef does not catch Promise

Update (from DelvarWorld):

Since this is the first google result for “eslint promise is not defined” I feel like it would be helpful to explicitly state the solution to this error here:

add "env": { "es6": true } to your .eslintrc.


The no-undef rule allows references to the global variable “Promise” even if it’s not marked as a global (which is problematic because IE does not have a global Promise.)

Here’s a test case, which can be added to the invalid section of no-undef.js.

        { code: "new Promise();", errors: [{ message: "\"Promise\" is not defined.", type: "Identifier"}] },

If the bug is accepted I’m happy to create a patch.

ESList version: 1.6.0 (ca3cc886f0eb65088c4cbe0585a74427f93422aa)

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 23
  • Comments: 19 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Since this is the first google result for “eslint promise is not defined” I feel like it would be helpful to explicitly state the solution to this error here:

add "env": { "es6": true } to your .eslintrc.

Alternatively, define the global in .eslintrc:

{
  "globals": {"Promise": true}
}

@sindresorhus we make a lot of fixes based on one user’s request, it’s not the number of people with the problem that matters, it’s the severity of the problem.

In this case, we have a situation where we’re getting a false negative that is pretty nasty (ESLint is failing to detect an undefined variable). Further, we have zero options for allowing individual users to fix the problem. Right now, there is no way to get var x = new Promise() to say “Promise is undefined”, and that’s something I’m not comfortable with, especially as I’m still peresonally supporting a web application that needs to target IE9 at work.

Keep in mind, we’re also talking about environments like MongoDB, wsh, AppleScript, etc., that might lag very far behind other JS environments in terms of updating to ES6.

Realistically, such a change will have minimal impact on current ESLint users. Most are already using the es6 environment to opt-in to ES6 functionality. At some point in the future (ESLint 3, ESLint 4), we’ll likely end up making ES6 the default and give people some way to opt-out if they don’t want it.

@pmcelhaney the latest version is edition 6, not 5.1.

I think I’ve achieved some enlightenment around this issue.

First, the only reasonable approach that doesn’t require a lot of hacks is to remove ES6 globals from builtin into an es6 environment. This will work as-is for anyone currently using the es6 environment, so it will only affect those who are not.

This is still a breaking change, so we’d have to wait for 2.0.0 to do it.

Ultimately, I had to meditate on the original issue description and came to the conclusion that missing an undefined identifier is a Bad Thing™ for a linter to do.

So, we can make this change, it will just have to wait for 2.0.0.