forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add restricted globals package (facebook#2286)
* Add restricted globals package * Use new package in eslint-config * Add eslint-restricted-globals dependency * Fixes * Update dependencies * Update test and README * Use jest * tweaks * Add lint/test to CI * Fix lint
- Loading branch information
Showing
7 changed files
with
169 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# confusing-browser-globals | ||
|
||
A curated list of browser globals that commonly cause confusion and are not recommended to use without an explicit `window.` qualifier. | ||
|
||
## Motivation | ||
|
||
Some global variables in browser are likely to be used by people without the intent of using them as globals, such as `status`, `name`, `event`, etc. | ||
|
||
For example: | ||
|
||
```js | ||
handleClick() { // missing `event` argument | ||
this.setState({ | ||
text: event.target.value // uses the `event` global: oops! | ||
}); | ||
} | ||
``` | ||
|
||
This package exports a list of globals that are often used by mistake. You can feed this list to a static analysis tool like ESLint to prevent their usage without an explicit `window.` qualifier. | ||
|
||
|
||
## Installation | ||
|
||
``` | ||
npm install --save confusing-browser-globals | ||
``` | ||
|
||
|
||
## Usage | ||
|
||
If you use Create React App, you don't need to configure anything, as this rule is already included in the default `eslint-config-react-app` preset. | ||
|
||
If you maintain your own ESLint configuration, you can do this: | ||
|
||
```js | ||
var restrictedGlobals = require('confusing-browser-globals'); | ||
|
||
module.exports = { | ||
rules: { | ||
'no-restricted-globals': ['error'].concat(restrictedGlobals), | ||
} | ||
}; | ||
``` | ||
|
||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = [ | ||
'addEventListener', | ||
'blur', | ||
'close', | ||
'closed', | ||
'confirm', | ||
'defaultStatus', | ||
'defaultstatus', | ||
'event', | ||
'external', | ||
'find', | ||
'focus', | ||
'frameElement', | ||
'frames', | ||
'history', | ||
'innerHeight', | ||
'innerWidth', | ||
'length', | ||
'location', | ||
'locationbar', | ||
'menubar', | ||
'moveBy', | ||
'moveTo', | ||
'name', | ||
'onblur', | ||
'onerror', | ||
'onfocus', | ||
'onload', | ||
'onresize', | ||
'onunload', | ||
'open', | ||
'opener', | ||
'opera', | ||
'outerHeight', | ||
'outerWidth', | ||
'pageXOffset', | ||
'pageYOffset', | ||
'parent', | ||
'print', | ||
'removeEventListener', | ||
'resizeBy', | ||
'resizeTo', | ||
'screen', | ||
'screenLeft', | ||
'screenTop', | ||
'screenX', | ||
'screenY', | ||
'scroll', | ||
'scrollbars', | ||
'scrollBy', | ||
'scrollTo', | ||
'scrollX', | ||
'scrollY', | ||
'self', | ||
'status', | ||
'statusbar', | ||
'stop', | ||
'toolbar', | ||
'top', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "confusing-browser-globals", | ||
"version": "1.0.0", | ||
"description": "A list of browser globals that are often used by mistake instead of local variables", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"repository": "facebookincubator/create-react-app", | ||
"keywords": [ | ||
"eslint", | ||
"globals" | ||
], | ||
"files": [ | ||
"index.js" | ||
], | ||
"devDependencies": { | ||
"jest": "22.0.6" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/* eslint-env jest */ | ||
|
||
'use strict'; | ||
|
||
let globals = require('./index'); | ||
|
||
it('should return an Array of globals', () => { | ||
expect(Array.isArray(globals)).toBe(true); | ||
}); | ||
|
||
it('should contain "event" variable', () => { | ||
expect(globals).toContain('event'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters