Skip to content

Commit 2361f64

Browse files
BridgeARMylesBorins
authored andcommitted
tools: stricter eslint rule for globals
PR-URL: #20567 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent bd13193 commit 2361f64

File tree

4 files changed

+53
-38
lines changed

4 files changed

+53
-38
lines changed

lib/.eslintrc.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ rules:
2020
- selector: "NewExpression[callee.name=/Error$/]:not([callee.name=/^(AssertionError|NghttpError)$/])"
2121
message: "Use an error exported by the internal/errors module."
2222
# Custom rules in tools/eslint-rules
23-
node-core/require-buffer: error
23+
node-core/require-globals: error
2424
node-core/buffer-constructor: error
2525
node-core/no-let-in-for-declaration: error
2626
node-core/lowercase-name-for-primitive: error

test/parallel/test-eslint-require-buffer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const common = require('../common');
55
common.skipIfEslintMissing();
66

77
const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
8-
const rule = require('../../tools/eslint-rules/require-buffer');
8+
const rule = require('../../tools/eslint-rules/require-globals');
99
const ruleTester = new RuleTester({
1010
parserOptions: { ecmaVersion: 6 },
1111
env: { node: true }
@@ -18,7 +18,7 @@ const useStrict = '\'use strict\';\n\n';
1818
const bufferModule = 'const { Buffer } = require(\'buffer\');\n';
1919
const mockComment = '// Some Comment\n//\n// Another Comment\n\n';
2020
const useBuffer = 'Buffer;';
21-
ruleTester.run('require-buffer', rule, {
21+
ruleTester.run('require-globals', rule, {
2222
valid: [
2323
'foo',
2424
'const Buffer = require("Buffer"); Buffer;',

tools/eslint-rules/require-buffer.js

-35
This file was deleted.

tools/eslint-rules/require-globals.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
// This rule makes sure that no Globals are going to be used in /lib.
4+
// That could otherwise result in problems with the repl.
5+
6+
module.exports = function(context) {
7+
8+
function flagIt(msg, fix) {
9+
return (reference) => {
10+
context.report({
11+
node: reference.identifier,
12+
message: msg,
13+
fix: (fixer) => {
14+
const sourceCode = context.getSourceCode();
15+
16+
const useStrict = /'use strict';\n\n?/g;
17+
const hasUseStrict = !!useStrict.exec(sourceCode.text);
18+
const firstLOC = sourceCode.ast.range[0];
19+
const rangeNeedle = hasUseStrict ? useStrict.lastIndex : firstLOC;
20+
21+
return fixer.insertTextBeforeRange([rangeNeedle], `${fix}\n`);
22+
}
23+
});
24+
};
25+
}
26+
27+
return {
28+
'Program:exit': function() {
29+
const globalScope = context.getScope();
30+
let variable = globalScope.set.get('Buffer');
31+
if (variable) {
32+
const fix = "const { Buffer } = require('buffer');";
33+
const msg = `Use ${fix} at the beginning of this file`;
34+
variable.references.forEach(flagIt(msg, fix));
35+
}
36+
variable = globalScope.set.get('URL');
37+
if (variable) {
38+
const fix = "const { URL } = require('url');";
39+
const msg = `Use ${fix} at the beginning of this file`;
40+
variable.references.forEach(flagIt(msg, fix));
41+
}
42+
variable = globalScope.set.get('URLSearchParams');
43+
if (variable) {
44+
const fix = "const { URLSearchParams } = require('url');";
45+
const msg = `Use ${fix} at the beginning of this file`;
46+
variable.references.forEach(flagIt(msg, fix));
47+
}
48+
}
49+
};
50+
};

0 commit comments

Comments
 (0)