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>
This commit is contained in:
parent
7afb73715f
commit
e0c71ca3eb
@ -20,7 +20,7 @@ rules:
|
|||||||
- selector: "NewExpression[callee.name=/Error$/]:not([callee.name=/^(AssertionError|NghttpError)$/])"
|
- selector: "NewExpression[callee.name=/Error$/]:not([callee.name=/^(AssertionError|NghttpError)$/])"
|
||||||
message: "Use an error exported by the internal/errors module."
|
message: "Use an error exported by the internal/errors module."
|
||||||
# Custom rules in tools/eslint-rules
|
# Custom rules in tools/eslint-rules
|
||||||
node-core/require-buffer: error
|
node-core/require-globals: error
|
||||||
node-core/buffer-constructor: error
|
node-core/buffer-constructor: error
|
||||||
node-core/no-let-in-for-declaration: error
|
node-core/no-let-in-for-declaration: error
|
||||||
node-core/lowercase-name-for-primitive: error
|
node-core/lowercase-name-for-primitive: error
|
||||||
|
@ -5,7 +5,7 @@ const common = require('../common');
|
|||||||
common.skipIfEslintMissing();
|
common.skipIfEslintMissing();
|
||||||
|
|
||||||
const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
|
const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
|
||||||
const rule = require('../../tools/eslint-rules/require-buffer');
|
const rule = require('../../tools/eslint-rules/require-globals');
|
||||||
const ruleTester = new RuleTester({
|
const ruleTester = new RuleTester({
|
||||||
parserOptions: { ecmaVersion: 6 },
|
parserOptions: { ecmaVersion: 6 },
|
||||||
env: { node: true }
|
env: { node: true }
|
||||||
@ -18,7 +18,7 @@ const useStrict = '\'use strict\';\n\n';
|
|||||||
const bufferModule = 'const { Buffer } = require(\'buffer\');\n';
|
const bufferModule = 'const { Buffer } = require(\'buffer\');\n';
|
||||||
const mockComment = '// Some Comment\n//\n// Another Comment\n\n';
|
const mockComment = '// Some Comment\n//\n// Another Comment\n\n';
|
||||||
const useBuffer = 'Buffer;';
|
const useBuffer = 'Buffer;';
|
||||||
ruleTester.run('require-buffer', rule, {
|
ruleTester.run('require-globals', rule, {
|
||||||
valid: [
|
valid: [
|
||||||
'foo',
|
'foo',
|
||||||
'const Buffer = require("Buffer"); Buffer;',
|
'const Buffer = require("Buffer"); Buffer;',
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
50
tools/eslint-rules/require-globals.js
Normal file
50
tools/eslint-rules/require-globals.js
Normal file
@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user