Skip to content

Commit 4f87522

Browse files
committed
doc, lib, test: do not re-require needlessly
PR-URL: #14244 Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
1 parent 43bd47c commit 4f87522

23 files changed

+72
-85
lines changed

doc/api/child_process.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,9 @@ socket to the child process. The example below spawns two children that each
11361136
handle connections with "normal" or "special" priority:
11371137

11381138
```js
1139-
const normal = require('child_process').fork('child.js', ['normal']);
1140-
const special = require('child_process').fork('child.js', ['special']);
1139+
const { fork } = require('child_process');
1140+
const normal = fork('child.js', ['normal']);
1141+
const special = fork('child.js', ['special']);
11411142

11421143
// Open up the server and send sockets to child. Use pauseOnConnect to prevent
11431144
// the sockets from being read before they are sent to the child process.

lib/_tls_legacy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121

2222
'use strict';
2323

24-
require('internal/util').assertCrypto();
24+
const internalUtil = require('internal/util');
25+
internalUtil.assertCrypto();
2526

2627
const assert = require('assert');
2728
const Buffer = require('buffer').Buffer;
2829
const common = require('_tls_common');
2930
const Connection = process.binding('crypto').Connection;
3031
const EventEmitter = require('events');
31-
const internalUtil = require('internal/util');
3232
const stream = require('stream');
3333
const Timer = process.binding('timer_wrap').Timer;
3434
const tls = require('tls');

lib/internal/process.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const errors = require('internal/errors');
4+
const util = require('util');
45
const constants = process.binding('constants').os.signals;
56

67
const assert = process.assert = function(x, msg) {
@@ -178,10 +179,8 @@ function setupKillAndExit() {
178179
}
179180
}
180181

181-
if (err) {
182-
const errnoException = require('util')._errnoException;
183-
throw errnoException(err, 'kill');
184-
}
182+
if (err)
183+
throw util._errnoException(err, 'kill');
185184

186185
return true;
187186
};
@@ -212,8 +211,7 @@ function setupSignalHandlers() {
212211
const err = wrap.start(signum);
213212
if (err) {
214213
wrap.close();
215-
const errnoException = require('util')._errnoException;
216-
throw errnoException(err, 'uv_signal_start');
214+
throw util._errnoException(err, 'uv_signal_start');
217215
}
218216

219217
signalWraps[type] = wrap;
@@ -253,10 +251,9 @@ function setupChannel() {
253251

254252

255253
function setupRawDebug() {
256-
const format = require('util').format;
257254
const rawDebug = process._rawDebug;
258255
process._rawDebug = function() {
259-
rawDebug(format.apply(null, arguments));
256+
rawDebug(util.format.apply(null, arguments));
260257
};
261258
}
262259

lib/repl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ try {
8686
}
8787

8888
// hack for repl require to work properly with node_modules folders
89-
module.paths = require('module')._nodeModulePaths(module.filename);
89+
module.paths = Module._nodeModulePaths(module.filename);
9090

9191
// If obj.hasOwnProperty has been overridden, then calling
9292
// obj.hasOwnProperty(prop) will break.
@@ -794,7 +794,7 @@ function complete(line, callback) {
794794
filter = match[1];
795795
var dir, files, f, name, base, ext, abs, subfiles, s;
796796
group = [];
797-
var paths = module.paths.concat(require('module').globalPaths);
797+
var paths = module.paths.concat(Module.globalPaths);
798798
for (i = 0; i < paths.length; i++) {
799799
dir = path.resolve(paths[i], subdir);
800800
try {

test/common/index.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ const path = require('path');
2525
const fs = require('fs');
2626
const assert = require('assert');
2727
const os = require('os');
28-
const child_process = require('child_process');
28+
const { exec, execSync, spawn, spawnSync } = require('child_process');
2929
const stream = require('stream');
3030
const util = require('util');
3131
const Timer = process.binding('timer_wrap').Timer;
32-
const execSync = require('child_process').execSync;
3332

3433
const testRoot = process.env.NODE_TEST_DIR ?
3534
fs.realpathSync(process.env.NODE_TEST_DIR) : path.resolve(__dirname, '..');
@@ -186,7 +185,7 @@ Object.defineProperty(exports, 'inFreeBSDJail', {
186185
if (inFreeBSDJail !== null) return inFreeBSDJail;
187186

188187
if (exports.isFreeBSD &&
189-
child_process.execSync('sysctl -n security.jail.jailed').toString() ===
188+
execSync('sysctl -n security.jail.jailed').toString() ===
190189
'1\n') {
191190
inFreeBSDJail = true;
192191
} else {
@@ -233,7 +232,7 @@ Object.defineProperty(exports, 'opensslCli', {get: function() {
233232

234233
if (exports.isWindows) opensslCli += '.exe';
235234

236-
const opensslCmd = child_process.spawnSync(opensslCli, ['version']);
235+
const opensslCmd = spawnSync(opensslCli, ['version']);
237236
if (opensslCmd.status !== 0 || opensslCmd.error !== undefined) {
238237
// openssl command cannot be executed
239238
opensslCli = false;
@@ -283,7 +282,7 @@ exports.childShouldThrowAndAbort = function() {
283282
}
284283
testCmd += `"${process.argv[0]}" --abort-on-uncaught-exception `;
285284
testCmd += `"${process.argv[1]}" child`;
286-
const child = child_process.exec(testCmd);
285+
const child = exec(testCmd);
287286
child.on('exit', function onExit(exitCode, signal) {
288287
const errMsg = 'Test should have aborted ' +
289288
`but instead exited with exit code ${exitCode}` +
@@ -303,8 +302,6 @@ exports.ddCommand = function(filename, kilobytes) {
303302

304303

305304
exports.spawnPwd = function(options) {
306-
const spawn = require('child_process').spawn;
307-
308305
if (exports.isWindows) {
309306
return spawn('cmd.exe', ['/d', '/c', 'cd'], options);
310307
} else {
@@ -314,8 +311,6 @@ exports.spawnPwd = function(options) {
314311

315312

316313
exports.spawnSyncPwd = function(options) {
317-
const spawnSync = require('child_process').spawnSync;
318-
319314
if (exports.isWindows) {
320315
return spawnSync('cmd.exe', ['/d', '/c', 'cd'], options);
321316
} else {
@@ -789,7 +784,7 @@ exports.getTTYfd = function getTTYfd() {
789784
else if (!tty.isatty(tty_fd)) tty_fd++;
790785
else {
791786
try {
792-
tty_fd = require('fs').openSync('/dev/tty');
787+
tty_fd = fs.openSync('/dev/tty');
793788
} catch (e) {
794789
// There aren't any tty fd's available to use.
795790
return -1;

test/parallel/test-assert-typedarray-deepequal.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
require('../common');
44
const assert = require('assert');
5-
const a = require('assert');
65

76
function makeBlock(f) {
87
const args = Array.prototype.slice.call(arguments, 1);
@@ -51,7 +50,8 @@ equalArrayPairs.forEach((arrayPair) => {
5150

5251
notEqualArrayPairs.forEach((arrayPair) => {
5352
assert.throws(
54-
makeBlock(a.deepEqual, arrayPair[0], arrayPair[1]),
55-
a.AssertionError
53+
// eslint-disable-next-line no-restricted-properties
54+
makeBlock(assert.deepEqual, arrayPair[0], arrayPair[1]),
55+
assert.AssertionError
5656
);
5757
});

test/parallel/test-assert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'use strict';
2323
const common = require('../common');
2424
const assert = require('assert');
25-
const a = require('assert');
25+
const a = assert;
2626

2727
function makeBlock(f) {
2828
const args = Array.prototype.slice.call(arguments, 1);

test/parallel/test-child-process-fork-and-spawn.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
'use strict';
2323
const common = require('../common');
2424
const assert = require('assert');
25-
const spawn = require('child_process').spawn;
26-
const fork = require('child_process').fork;
25+
const { fork, spawn } = require('child_process');
2726

2827
// Fork, then spawn. The spawned process should not hang.
2928
switch (process.argv[2] || '') {

test/parallel/test-child-process-send-returns-boolean.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const common = require('../common');
33
const assert = require('assert');
44
const path = require('path');
55
const net = require('net');
6-
const fork = require('child_process').fork;
7-
const spawn = require('child_process').spawn;
6+
const { fork, spawn } = require('child_process');
87

98
const emptyFile = path.join(common.fixturesDir, 'empty.js');
109

test/parallel/test-crypto-random.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
148148
bufferTooSmall: /^RangeError: buffer too small$/,
149149
};
150150

151+
const max = require('buffer').kMaxLength + 1;
152+
151153
for (const buf of bufs) {
152154
const len = Buffer.byteLength(buf);
153155
assert.strictEqual(len, 10, `Expected byteLength of 10, got ${len}`);
@@ -168,8 +170,6 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
168170
crypto.randomFill(buf, NaN, common.mustNotCall());
169171
}, errMessages.offsetNotNumber);
170172

171-
const max = require('buffer').kMaxLength + 1;
172-
173173
assert.throws(() => {
174174
crypto.randomFillSync(buf, 11);
175175
}, errMessages.offsetOutOfRange);

0 commit comments

Comments
 (0)