diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-05-07 02:18:23 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-05-18 15:27:29 +0200 |
commit | e0c71ca3eb645457a7e6e369bf8ad4c1a62902c2 (patch) | |
tree | ecdc0ba6eac67e764e6c36300535a39220dff659 /tools | |
parent | 7afb73715f161ea14924eceb6f03b502f32ac8fd (diff) | |
download | android-node-v8-e0c71ca3eb645457a7e6e369bf8ad4c1a62902c2.tar.gz android-node-v8-e0c71ca3eb645457a7e6e369bf8ad4c1a62902c2.tar.bz2 android-node-v8-e0c71ca3eb645457a7e6e369bf8ad4c1a62902c2.zip |
tools: stricter eslint rule for globals
PR-URL: https://github.com/nodejs/node/pull/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>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/eslint-rules/require-buffer.js | 35 | ||||
-rw-r--r-- | tools/eslint-rules/require-globals.js | 50 |
2 files changed, 50 insertions, 35 deletions
diff --git a/tools/eslint-rules/require-buffer.js b/tools/eslint-rules/require-buffer.js deleted file mode 100644 index b12b9ce04e..0000000000 --- a/tools/eslint-rules/require-buffer.js +++ /dev/null @@ -1,35 +0,0 @@ -'use strict'; -const BUFFER_REQUIRE = 'const { Buffer } = require(\'buffer\');'; - -module.exports = function(context) { - - function flagIt(reference) { - const msg = `Use ${BUFFER_REQUIRE} at the beginning of this file`; - - 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], - `${BUFFER_REQUIRE}\n`); - } - }); - } - - return { - 'Program:exit': function() { - const globalScope = context.getScope(); - const variable = globalScope.set.get('Buffer'); - if (variable) { - variable.references.forEach(flagIt); - } - } - }; -}; diff --git a/tools/eslint-rules/require-globals.js b/tools/eslint-rules/require-globals.js new file mode 100644 index 0000000000..bc49ff6c87 --- /dev/null +++ b/tools/eslint-rules/require-globals.js @@ -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)); + } + } + }; +}; |