Skip to content

Commit de23055

Browse files
committed
lib: remove env: node in eslint config for lib files
This patch removes the redundant `require-globals` custom eslint rule by removing `env: node` in the eslint config and whitelist the globals that can be accessed in native modules instead of black listing them. This makes sense for our `lib/` files because here we are creating the Node.js environment instead of running in a normal user land Node.js environment. PR-URL: #27082 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 864860e commit de23055

File tree

12 files changed

+57
-117
lines changed

12 files changed

+57
-117
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
/* eslint-env node */
4+
35
const Module = require('module');
46
const path = require('path');
57

@@ -31,7 +33,6 @@ Module._findPath = (request, paths, isMain) => {
3133
module.exports = {
3234
root: true,
3335
plugins: ['markdown', 'node-core'],
34-
env: { node: true, es6: true },
3536
parser: 'babel-eslint',
3637
parserOptions: { sourceType: 'script' },
3738
overrides: [

benchmark/.eslintrc.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Benchmark-specific linter rules
22

3+
env:
4+
node: true
5+
es6: true
6+
37
rules:
48
comma-dangle:
59
- error

lib/.eslintrc.yaml

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
env:
2+
es6: true
3+
14
rules:
25
prefer-object-spread: error
36
no-buffer-constructor: error
@@ -19,10 +22,11 @@ rules:
1922
- selector: "CallExpression[callee.object.name='Error'][callee.property.name='captureStackTrace']"
2023
message: "Please use `require('internal/errors').hideStackFrames()` instead."
2124
# Custom rules in tools/eslint-rules
22-
node-core/require-globals: error
2325
node-core/lowercase-name-for-primitive: error
2426
node-core/non-ascii-character: error
2527
globals:
28+
Intl: false
29+
# Assertions
2630
CHECK: false
2731
CHECK_EQ: false
2832
CHECK_GE: false
@@ -37,5 +41,21 @@ globals:
3741
DCHECK_LE: false
3842
DCHECK_LT: false
3943
DCHECK_NE: false
44+
# Parameters passed to internal modules
45+
global: false
46+
require: false
47+
process: false
48+
exports: false
49+
module: false
4050
internalBinding: false
4151
primordials: false
52+
# Globals
53+
# TODO(joyeecheung): if possible, get these in native modules
54+
# through `require` instead of grabbing them from the global proxy.
55+
clearTimeout: false
56+
setTimeout: false
57+
clearInterval: false
58+
setInterval: false
59+
setImmediate: false
60+
clearImmediate: false
61+
console: false

lib/buffer.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -654,9 +654,10 @@ Buffer.prototype.equals = function equals(otherBuffer) {
654654
return _compare(this, otherBuffer) === 0;
655655
};
656656

657+
let INSPECT_MAX_BYTES = 50;
657658
// Override how buffers are presented by util.inspect().
658659
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
659-
const max = exports.INSPECT_MAX_BYTES;
660+
const max = INSPECT_MAX_BYTES;
660661
const actualMax = Math.min(max, this.length);
661662
const remaining = this.length - max;
662663
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
@@ -1089,19 +1090,25 @@ if (internalBinding('config').hasIntl) {
10891090
};
10901091
}
10911092

1092-
module.exports = exports = {
1093+
module.exports = {
10931094
Buffer,
10941095
SlowBuffer,
10951096
transcode,
1096-
INSPECT_MAX_BYTES: 50,
1097-
10981097
// Legacy
10991098
kMaxLength,
11001099
kStringMaxLength
11011100
};
11021101

1103-
Object.defineProperty(exports, 'constants', {
1104-
configurable: false,
1105-
enumerable: true,
1106-
value: constants
1102+
Object.defineProperties(module.exports, {
1103+
constants: {
1104+
configurable: false,
1105+
enumerable: true,
1106+
value: constants
1107+
},
1108+
INSPECT_MAX_BYTES: {
1109+
configurable: true,
1110+
enumerable: true,
1111+
get() { return INSPECT_MAX_BYTES; },
1112+
set(val) { INSPECT_MAX_BYTES = val; }
1113+
}
11071114
});

lib/crypto.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function createVerify(algorithm, options) {
145145
return new Verify(algorithm, options);
146146
}
147147

148-
module.exports = exports = {
148+
module.exports = {
149149
// Methods
150150
createCipheriv,
151151
createDecipheriv,
@@ -218,7 +218,7 @@ function getFipsForced() {
218218
return 1;
219219
}
220220

221-
Object.defineProperties(exports, {
221+
Object.defineProperties(module.exports, {
222222
createCipher: {
223223
enumerable: false,
224224
value: deprecate(createCipher,

lib/internal/bootstrap/loaders.js

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ const internalBindingWhitelist = new SafeSet([
124124
let internalBinding;
125125
{
126126
const bindingObj = Object.create(null);
127+
// eslint-disable-next-line no-global-assign
127128
internalBinding = function internalBinding(module) {
128129
let mod = bindingObj[module];
129130
if (typeof mod !== 'object') {

lib/internal/modules/cjs/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function normalizeReferrerURL(referrer) {
129129
return new URL(referrer).href;
130130
}
131131

132-
module.exports = exports = {
132+
module.exports = {
133133
addBuiltinLibsToObject,
134134
builtinLibs,
135135
makeRequireFunction,

lib/util.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ function timestamp() {
113113
}
114114

115115
// Log is just a thin wrapper to console.log that prepends a timestamp
116-
function log() {
117-
console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
116+
function log(...args) {
117+
console.log('%s - %s', timestamp(), format(...args));
118118
}
119119

120120
/**
@@ -218,7 +218,7 @@ function getSystemErrorName(err) {
218218
}
219219

220220
// Keep the `exports =` so that various functions can still be monkeypatched
221-
module.exports = exports = {
221+
module.exports = {
222222
_errnoException: errnoException,
223223
_exceptionWithHostPort: exceptionWithHostPort,
224224
_extend,

test/.eslintrc.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Test-specific linter rules
22

3+
env:
4+
node: true
5+
es6: true
6+
37
rules:
48
# ECMAScript 6
59
# http://eslint.org/docs/rules/#ecmascript-6

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

-51
This file was deleted.

tools/.eslintrc.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
env:
2+
node: true
3+
es6: true
4+
15
rules:
26
comma-dangle:
37
- error

tools/eslint-rules/require-globals.js

-50
This file was deleted.

0 commit comments

Comments
 (0)