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: https://github.com/nodejs/node/pull/27082 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
864860e9f3
commit
de23055536
@ -1,5 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
/* eslint-env node */
|
||||||
|
|
||||||
const Module = require('module');
|
const Module = require('module');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
@ -31,7 +33,6 @@ Module._findPath = (request, paths, isMain) => {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
root: true,
|
root: true,
|
||||||
plugins: ['markdown', 'node-core'],
|
plugins: ['markdown', 'node-core'],
|
||||||
env: { node: true, es6: true },
|
|
||||||
parser: 'babel-eslint',
|
parser: 'babel-eslint',
|
||||||
parserOptions: { sourceType: 'script' },
|
parserOptions: { sourceType: 'script' },
|
||||||
overrides: [
|
overrides: [
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
## Benchmark-specific linter rules
|
## Benchmark-specific linter rules
|
||||||
|
|
||||||
|
env:
|
||||||
|
node: true
|
||||||
|
es6: true
|
||||||
|
|
||||||
rules:
|
rules:
|
||||||
comma-dangle:
|
comma-dangle:
|
||||||
- error
|
- error
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
env:
|
||||||
|
es6: true
|
||||||
|
|
||||||
rules:
|
rules:
|
||||||
prefer-object-spread: error
|
prefer-object-spread: error
|
||||||
no-buffer-constructor: error
|
no-buffer-constructor: error
|
||||||
@ -19,10 +22,11 @@ rules:
|
|||||||
- selector: "CallExpression[callee.object.name='Error'][callee.property.name='captureStackTrace']"
|
- selector: "CallExpression[callee.object.name='Error'][callee.property.name='captureStackTrace']"
|
||||||
message: "Please use `require('internal/errors').hideStackFrames()` instead."
|
message: "Please use `require('internal/errors').hideStackFrames()` instead."
|
||||||
# Custom rules in tools/eslint-rules
|
# Custom rules in tools/eslint-rules
|
||||||
node-core/require-globals: error
|
|
||||||
node-core/lowercase-name-for-primitive: error
|
node-core/lowercase-name-for-primitive: error
|
||||||
node-core/non-ascii-character: error
|
node-core/non-ascii-character: error
|
||||||
globals:
|
globals:
|
||||||
|
Intl: false
|
||||||
|
# Assertions
|
||||||
CHECK: false
|
CHECK: false
|
||||||
CHECK_EQ: false
|
CHECK_EQ: false
|
||||||
CHECK_GE: false
|
CHECK_GE: false
|
||||||
@ -37,5 +41,21 @@ globals:
|
|||||||
DCHECK_LE: false
|
DCHECK_LE: false
|
||||||
DCHECK_LT: false
|
DCHECK_LT: false
|
||||||
DCHECK_NE: false
|
DCHECK_NE: false
|
||||||
|
# Parameters passed to internal modules
|
||||||
|
global: false
|
||||||
|
require: false
|
||||||
|
process: false
|
||||||
|
exports: false
|
||||||
|
module: false
|
||||||
internalBinding: false
|
internalBinding: false
|
||||||
primordials: false
|
primordials: false
|
||||||
|
# Globals
|
||||||
|
# TODO(joyeecheung): if possible, get these in native modules
|
||||||
|
# through `require` instead of grabbing them from the global proxy.
|
||||||
|
clearTimeout: false
|
||||||
|
setTimeout: false
|
||||||
|
clearInterval: false
|
||||||
|
setInterval: false
|
||||||
|
setImmediate: false
|
||||||
|
clearImmediate: false
|
||||||
|
console: false
|
||||||
|
@ -654,9 +654,10 @@ Buffer.prototype.equals = function equals(otherBuffer) {
|
|||||||
return _compare(this, otherBuffer) === 0;
|
return _compare(this, otherBuffer) === 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let INSPECT_MAX_BYTES = 50;
|
||||||
// Override how buffers are presented by util.inspect().
|
// Override how buffers are presented by util.inspect().
|
||||||
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
|
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
|
||||||
const max = exports.INSPECT_MAX_BYTES;
|
const max = INSPECT_MAX_BYTES;
|
||||||
const actualMax = Math.min(max, this.length);
|
const actualMax = Math.min(max, this.length);
|
||||||
const remaining = this.length - max;
|
const remaining = this.length - max;
|
||||||
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
|
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
|
||||||
@ -1089,19 +1090,25 @@ if (internalBinding('config').hasIntl) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = exports = {
|
module.exports = {
|
||||||
Buffer,
|
Buffer,
|
||||||
SlowBuffer,
|
SlowBuffer,
|
||||||
transcode,
|
transcode,
|
||||||
INSPECT_MAX_BYTES: 50,
|
|
||||||
|
|
||||||
// Legacy
|
// Legacy
|
||||||
kMaxLength,
|
kMaxLength,
|
||||||
kStringMaxLength
|
kStringMaxLength
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.defineProperty(exports, 'constants', {
|
Object.defineProperties(module.exports, {
|
||||||
|
constants: {
|
||||||
configurable: false,
|
configurable: false,
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
value: constants
|
value: constants
|
||||||
|
},
|
||||||
|
INSPECT_MAX_BYTES: {
|
||||||
|
configurable: true,
|
||||||
|
enumerable: true,
|
||||||
|
get() { return INSPECT_MAX_BYTES; },
|
||||||
|
set(val) { INSPECT_MAX_BYTES = val; }
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
@ -145,7 +145,7 @@ function createVerify(algorithm, options) {
|
|||||||
return new Verify(algorithm, options);
|
return new Verify(algorithm, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = exports = {
|
module.exports = {
|
||||||
// Methods
|
// Methods
|
||||||
createCipheriv,
|
createCipheriv,
|
||||||
createDecipheriv,
|
createDecipheriv,
|
||||||
@ -218,7 +218,7 @@ function getFipsForced() {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperties(exports, {
|
Object.defineProperties(module.exports, {
|
||||||
createCipher: {
|
createCipher: {
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
value: deprecate(createCipher,
|
value: deprecate(createCipher,
|
||||||
|
@ -124,6 +124,7 @@ const internalBindingWhitelist = new SafeSet([
|
|||||||
let internalBinding;
|
let internalBinding;
|
||||||
{
|
{
|
||||||
const bindingObj = Object.create(null);
|
const bindingObj = Object.create(null);
|
||||||
|
// eslint-disable-next-line no-global-assign
|
||||||
internalBinding = function internalBinding(module) {
|
internalBinding = function internalBinding(module) {
|
||||||
let mod = bindingObj[module];
|
let mod = bindingObj[module];
|
||||||
if (typeof mod !== 'object') {
|
if (typeof mod !== 'object') {
|
||||||
|
@ -129,7 +129,7 @@ function normalizeReferrerURL(referrer) {
|
|||||||
return new URL(referrer).href;
|
return new URL(referrer).href;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = exports = {
|
module.exports = {
|
||||||
addBuiltinLibsToObject,
|
addBuiltinLibsToObject,
|
||||||
builtinLibs,
|
builtinLibs,
|
||||||
makeRequireFunction,
|
makeRequireFunction,
|
||||||
|
@ -113,8 +113,8 @@ function timestamp() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Log is just a thin wrapper to console.log that prepends a timestamp
|
// Log is just a thin wrapper to console.log that prepends a timestamp
|
||||||
function log() {
|
function log(...args) {
|
||||||
console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
|
console.log('%s - %s', timestamp(), format(...args));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -218,7 +218,7 @@ function getSystemErrorName(err) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Keep the `exports =` so that various functions can still be monkeypatched
|
// Keep the `exports =` so that various functions can still be monkeypatched
|
||||||
module.exports = exports = {
|
module.exports = {
|
||||||
_errnoException: errnoException,
|
_errnoException: errnoException,
|
||||||
_exceptionWithHostPort: exceptionWithHostPort,
|
_exceptionWithHostPort: exceptionWithHostPort,
|
||||||
_extend,
|
_extend,
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
## Test-specific linter rules
|
## Test-specific linter rules
|
||||||
|
|
||||||
|
env:
|
||||||
|
node: true
|
||||||
|
es6: true
|
||||||
|
|
||||||
rules:
|
rules:
|
||||||
# ECMAScript 6
|
# ECMAScript 6
|
||||||
# http://eslint.org/docs/rules/#ecmascript-6
|
# http://eslint.org/docs/rules/#ecmascript-6
|
||||||
|
@ -1,51 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
const common = require('../common');
|
|
||||||
if (!common.hasCrypto)
|
|
||||||
common.skip('missing crypto');
|
|
||||||
|
|
||||||
common.skipIfEslintMissing();
|
|
||||||
|
|
||||||
const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
|
|
||||||
const rule = require('../../tools/eslint-rules/require-globals');
|
|
||||||
const ruleTester = new RuleTester({
|
|
||||||
parserOptions: { ecmaVersion: 6 },
|
|
||||||
env: { node: true }
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = "Use const { Buffer } = require('buffer'); " +
|
|
||||||
'at the beginning of this file';
|
|
||||||
|
|
||||||
const useStrict = '\'use strict\';\n\n';
|
|
||||||
const bufferModule = 'const { Buffer } = require(\'buffer\');\n';
|
|
||||||
const mockComment = '// Some Comment\n//\n// Another Comment\n\n';
|
|
||||||
const useBuffer = 'Buffer;';
|
|
||||||
ruleTester.run('require-globals', rule, {
|
|
||||||
valid: [
|
|
||||||
'foo',
|
|
||||||
'const Buffer = require("Buffer"); Buffer;',
|
|
||||||
'const { Buffer } = require(\'buffer\'); Buffer;',
|
|
||||||
],
|
|
||||||
invalid: [
|
|
||||||
{
|
|
||||||
code: useBuffer,
|
|
||||||
errors: [{ message }],
|
|
||||||
output: bufferModule + useBuffer,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: useStrict + useBuffer,
|
|
||||||
errors: [{ message }],
|
|
||||||
output: useStrict + bufferModule + useBuffer,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: mockComment + useBuffer,
|
|
||||||
errors: [{ message }],
|
|
||||||
output: mockComment + bufferModule + useBuffer,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: mockComment + useStrict + useBuffer,
|
|
||||||
errors: [{ message }],
|
|
||||||
output: mockComment + useStrict + bufferModule + useBuffer,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
});
|
|
@ -1,3 +1,7 @@
|
|||||||
|
env:
|
||||||
|
node: true
|
||||||
|
es6: true
|
||||||
|
|
||||||
rules:
|
rules:
|
||||||
comma-dangle:
|
comma-dangle:
|
||||||
- error
|
- error
|
||||||
|
@ -1,50 +0,0 @@
|
|||||||
'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