-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools: stricter eslint rule for globals
PR-URL: #20567 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
- Loading branch information
1 parent
bd13193
commit 2361f64
Showing
4 changed files
with
53 additions
and
38 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 was deleted.
Oops, something went wrong.
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,50 @@ | ||
'use strict'; | ||
|
||
// This rule makes sure that no Globals are going to be used in /lib. | ||
// That could otherwise result in problems with the repl. | ||
|
||
module.exports = function(context) { | ||
|
||
function flagIt(msg, fix) { | ||
return (reference) => { | ||
context.report({ | ||
node: reference.identifier, | ||
message: msg, | ||
fix: (fixer) => { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
const useStrict = /'use strict';\n\n?/g; | ||
const hasUseStrict = !!useStrict.exec(sourceCode.text); | ||
const firstLOC = sourceCode.ast.range[0]; | ||
const rangeNeedle = hasUseStrict ? useStrict.lastIndex : firstLOC; | ||
|
||
return fixer.insertTextBeforeRange([rangeNeedle], `${fix}\n`); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
return { | ||
'Program:exit': function() { | ||
const globalScope = context.getScope(); | ||
let variable = globalScope.set.get('Buffer'); | ||
if (variable) { | ||
const fix = "const { Buffer } = require('buffer');"; | ||
const msg = `Use ${fix} at the beginning of this file`; | ||
variable.references.forEach(flagIt(msg, fix)); | ||
} | ||
variable = globalScope.set.get('URL'); | ||
if (variable) { | ||
const fix = "const { URL } = require('url');"; | ||
const msg = `Use ${fix} at the beginning of this file`; | ||
variable.references.forEach(flagIt(msg, fix)); | ||
} | ||
variable = globalScope.set.get('URLSearchParams'); | ||
if (variable) { | ||
const fix = "const { URLSearchParams } = require('url');"; | ||
const msg = `Use ${fix} at the beginning of this file`; | ||
variable.references.forEach(flagIt(msg, fix)); | ||
} | ||
} | ||
}; | ||
}; |