Don't add event as global when browser: true
See original GitHub issueRight 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:
- Created 8 years ago
- Reactions:1
- Comments:27 (10 by maintainers)
Top GitHub Comments
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:
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 theevent
global to your config (or just reference it explicitly aswindow.event
). Alternatively offer some additional configuration option likebrowser-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.
Note, you can use the
no-restricted-globals
rule now: http://eslint.org/docs/rules/no-restricted-globals