-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: add custom eslint rule against global pollution
This adds a custom eslint rule to make sure the globals are not polluted in any way by adding extra properties to them in any way. Besides that it also removes access to `global` by marking some globals as available to eslint.
- Loading branch information
Showing
5 changed files
with
40 additions
and
5 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
// This file is compiled as if it's wrapped in a function with arguments | ||
// passed by node::NewContext() | ||
/* global global */ | ||
|
||
'use strict'; | ||
|
||
// https://github.com/nodejs/node/issues/14909 | ||
if (global.Intl) { | ||
delete global.Intl.v8BreakIterator; | ||
if (typeof Intl !== 'undefined') { | ||
delete Intl.v8BreakIterator; | ||
} | ||
|
||
// https://github.com/nodejs/node/issues/21219 | ||
if (global.Atomics) { | ||
delete global.Atomics.wake; | ||
if (typeof Atomics !== 'undefined') { | ||
delete Atomics.wake; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @fileOverview This rule makes sure that the globals don't get polluted by | ||
* adding new entries to `global`. | ||
* @author Ruben Bridgewater <ruben@bridgewater.de> | ||
*/ | ||
|
||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
const filename = context.getFilename(); | ||
|
||
return { | ||
'Program:exit': function() { | ||
const globalScope = context.getScope(); | ||
const variable = globalScope.set.get('global'); | ||
if (variable && | ||
!filename.includes('bootstrap') && | ||
!filename.includes('repl.js')) { | ||
const msg = 'Please do not pollute the global scope'; | ||
variable.references.forEach((reference) => { | ||
context.report({ | ||
node: reference.identifier, | ||
message: msg | ||
}); | ||
}); | ||
} | ||
} | ||
}; | ||
}; |