question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Don't add event as global when browser: true

See original GitHub issue

Right now when you configure your env with “browser: true” you will get a event variable automatically added to your globals so this would lint perfectly despite event not being defined (tested in the online demo):

/*eslint-env browser */

function a() {
  var b = event.target;
  return b;
}

a();

I think that this is incorrect as the event variable is only defined inside an event context, so its not totally global and may end up preventing eslint form catching some potential undefined variables.

Also, and the actual reason why I discovered this, in some contexts the event parameter and the event global may not be the same thing. For example in React they are using some fake nodes to dispatch events (I find it a bit odd but this or another similar code may still be a valid use case) and they do a bind to override the event value as seen here: https://github.com/facebook/react/blob/8c75e792abf54f34b7a36a777826f104d1e0fb34/src/shared/utils/ReactErrorUtils.js#L68 . In that case if you use the global event instead of the parameter you will get a fake <react></react> node instead of the target and you won’t figure out until one hour later 😄

For that cases if you don’t think that removing the global event for the browsers is a good idea, potentially for compatibility reasons, maybe adding an specific rule to forbid the use of global event variables would be a good idea so eslint can handle this situations a bit better.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:27 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
rhys-vdwcommented, May 24, 2016

Just throwing in my 2c here. Global event fails in both modern versions of IE and Firefox (but passes in Chrome). Since I do all my development in chrome this one just slipped by ESLint and (thankfully) ended up being caught in a PR review:

  onMouseDown() { // <-- missing `event` argument
    if (event.button === 0 && event.target === this.$element.get(0)) {
      this.startTranslate(event);
    }
  }

I’d certainly prefer a linter error if an undefined event exists in scope. My take is that if you’re doing this deliberately, then you can add manually the event global to your config (or just reference it explicitly as window.event). Alternatively offer some additional configuration option like browser-legacy or similar?

Seems silly to allow code that will break in modern browsers in preference for permitting code that is only required by old browsers.

1reaction
axelsoncommented, Aug 30, 2016

Note, you can use the no-restricted-globals rule now: http://eslint.org/docs/rules/no-restricted-globals

Read more comments on GitHub >

github_iconTop Results From Across the Web

Consider marking event handler as 'passive' to make the page ...
If you specify passive: true and preventDefault() gets called, it results in an error and default is not prevented. Specifying passive is not...
Read more >
EventTarget.addEventListener() - Web APIs | MDN
It works on any event target, not just HTML or SVG elements. The method addEventListener() works by adding a function, or an object...
Read more >
Introduction to browser events - The Modern JavaScript Tutorial
An event is a signal that something has happened. All DOM nodes generate such signals (but events are not limited to DOM).
Read more >
Event listeners not working? 3 key areas to troubleshoot
Browsers provide a very neat system of events. This is how applications can provide custom behaviour for clicks, keypresses, or in response to...
Read more >
Handling Events :: Eloquent JavaScript
Each browser event handler is registered in a context. In the previous example we called addEventListener on the window object to register a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found