eslint-plugin-piggyback
Contains the no-restricted-global-extend
rule:
Disallow extending the global scope by extending objects that reference it (e.g. 'window')
(AKA "piggybacking" :stuck_out_tongue:)
Motivation
Not too long ago, the common way separating your code into "modules" was to define an object on the global scope in one script and access it from another script.
Nowadays, with the advent of module systems (AMD,CJS,ES6), you might want to avoid "polluting" the global scope.
In order to enforce this with ESLint, you'll want to use the built-in rule no-undef
which will catch any variable not defined within the script.
(function() {
Foo.bar();
})():
It's common to have some variables that are defined elsewhere but should not throw any errors, such as window
if you're writing front-end code.
(function() {
window.location.reload();
})();
In the case above browser
is set as the environment because you want to access window.location
which is valid because it exists in any browser (you could also just set /*global window*/
).
But once you do this anyone can assign and access other properties on window
without triggering a lint violation.
(function() {
window.Foo.bar();
})();
This is where no-restricted-global-extend
comes in. It can identify cases where you're using an object which references the global scope, and it will alert you if the property you're accessing is not available in the global scope.
This rule can be used in unison with no-undef
. See the 'Usage' section below on how to configure it.
(function() {
window.location.reload();
window.console.log();
window.Foo.bar();
})();
Installation
You'll first need to install ESLint:
$ npm i eslint --save-dev
Next, install eslint-plugin-piggyback
:
$ npm install eslint-plugin-piggyback --save-dev
Note: If you installed ESLint globally (using the -g
flag) then you must also install eslint-plugin-piggyback
globally.
Usage
Add piggyback
to the plugins section of your .eslintrc
configuration file. You can omit the eslint-plugin-
prefix:
{
"plugins": [
"piggyback"
]
}
Then configure the 'no-restricted-global-extend' under the rules section.
You must specify which identifiers reference the global scope in your environment (e.g. 'window').
{
"rules": {
"piggyback/no-restricted-global-extend": [2, "window"]
}
}
Running tests
Run mocha tests with npm test