Skip to content

Commit 38ae5c4

Browse files
vsemozhetbytMylesBorins
authored andcommitted
doc, lib, test: do not re-require needlessly
PR-URL: #14244 Reviewed-By: Alexey Orlenko <[email protected]>
1 parent 792acc1 commit 38ae5c4

19 files changed

+65
-79
lines changed

doc/api/child_process.md

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

10481048
```js
1049-
const normal = require('child_process').fork('subprocess.js', ['normal']);
1050-
const special = require('child_process').fork('subprocess.js', ['special']);
1049+
const { fork } = require('child_process');
1050+
const normal = fork('subprocess.js', ['normal']);
1051+
const special = fork('subprocess.js', ['special']);
10511052

10521053
// Open up the server and send sockets to child
10531054
const server = require('net').createServer();

lib/internal/process.js

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

3+
const util = require('util');
4+
35
var _lazyConstants = null;
46

57
function lazyConstants() {
@@ -185,10 +187,8 @@ function setupKillAndExit() {
185187
}
186188
}
187189

188-
if (err) {
189-
const errnoException = require('util')._errnoException;
190-
throw errnoException(err, 'kill');
191-
}
190+
if (err)
191+
throw util._errnoException(err, 'kill');
192192

193193
return true;
194194
};
@@ -220,8 +220,7 @@ function setupSignalHandlers() {
220220
const err = wrap.start(signum);
221221
if (err) {
222222
wrap.close();
223-
const errnoException = require('util')._errnoException;
224-
throw errnoException(err, 'uv_signal_start');
223+
throw util._errnoException(err, 'uv_signal_start');
225224
}
226225

227226
signalWraps[type] = wrap;
@@ -261,9 +260,8 @@ function setupChannel() {
261260

262261

263262
function setupRawDebug() {
264-
const format = require('util').format;
265263
const rawDebug = process._rawDebug;
266264
process._rawDebug = function() {
267-
rawDebug(format.apply(null, arguments));
265+
rawDebug(util.format.apply(null, arguments));
268266
};
269267
}

lib/repl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ try {
6161
}
6262

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

6666
// If obj.hasOwnProperty has been overridden, then calling
6767
// obj.hasOwnProperty(prop) will break.
@@ -869,7 +869,7 @@ function complete(line, callback) {
869869
filter = match[1];
870870
var dir, files, f, name, base, ext, abs, subfiles, s;
871871
group = [];
872-
var paths = module.paths.concat(require('module').globalPaths);
872+
var paths = module.paths.concat(Module.globalPaths);
873873
for (i = 0; i < paths.length; i++) {
874874
dir = path.resolve(paths[i], subdir);
875875
try {

test/common/index.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const path = require('path');
44
const fs = require('fs');
55
const assert = require('assert');
66
const os = require('os');
7-
const child_process = require('child_process');
7+
const { exec, execSync, spawn, spawnSync } = require('child_process');
88
const stream = require('stream');
99
const util = require('util');
1010
const Timer = process.binding('timer_wrap').Timer;
@@ -121,7 +121,7 @@ Object.defineProperty(exports, 'inFreeBSDJail', {
121121
if (inFreeBSDJail !== null) return inFreeBSDJail;
122122

123123
if (exports.isFreeBSD &&
124-
child_process.execSync('sysctl -n security.jail.jailed').toString() ===
124+
execSync('sysctl -n security.jail.jailed').toString() ===
125125
'1\n') {
126126
inFreeBSDJail = true;
127127
} else {
@@ -168,7 +168,7 @@ Object.defineProperty(exports, 'opensslCli', {get: function() {
168168

169169
if (exports.isWindows) opensslCli += '.exe';
170170

171-
const opensslCmd = child_process.spawnSync(opensslCli, ['version']);
171+
const opensslCmd = spawnSync(opensslCli, ['version']);
172172
if (opensslCmd.status !== 0 || opensslCmd.error !== undefined) {
173173
// openssl command cannot be executed
174174
opensslCli = false;
@@ -219,7 +219,7 @@ exports.childShouldThrowAndAbort = function() {
219219
}
220220
testCmd += `"${process.argv[0]}" --abort-on-uncaught-exception `;
221221
testCmd += `"${process.argv[1]}" child`;
222-
const child = child_process.exec(testCmd);
222+
const child = exec(testCmd);
223223
child.on('exit', function onExit(exitCode, signal) {
224224
const errMsg = 'Test should have aborted ' +
225225
`but instead exited with exit code ${exitCode}` +
@@ -239,8 +239,6 @@ exports.ddCommand = function(filename, kilobytes) {
239239

240240

241241
exports.spawnCat = function(options) {
242-
const spawn = require('child_process').spawn;
243-
244242
if (exports.isWindows) {
245243
return spawn('more', [], options);
246244
} else {
@@ -250,8 +248,6 @@ exports.spawnCat = function(options) {
250248

251249

252250
exports.spawnSyncCat = function(options) {
253-
const spawnSync = require('child_process').spawnSync;
254-
255251
if (exports.isWindows) {
256252
return spawnSync('more', [], options);
257253
} else {
@@ -261,8 +257,6 @@ exports.spawnSyncCat = function(options) {
261257

262258

263259
exports.spawnPwd = function(options) {
264-
const spawn = require('child_process').spawn;
265-
266260
if (exports.isWindows) {
267261
return spawn('cmd.exe', ['/c', 'cd'], options);
268262
} else {
@@ -272,8 +266,6 @@ exports.spawnPwd = function(options) {
272266

273267

274268
exports.spawnSyncPwd = function(options) {
275-
const spawnSync = require('child_process').spawnSync;
276-
277269
if (exports.isWindows) {
278270
return spawnSync('cmd.exe', ['/c', 'cd'], options);
279271
} else {

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
@@ -1,7 +1,7 @@
11
'use strict';
22
require('../common');
33
const assert = require('assert');
4-
const a = require('assert');
4+
const a = assert;
55

66
function makeBlock(f) {
77
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
@@ -1,8 +1,7 @@
11
'use strict';
22
const common = require('../common');
33
const assert = require('assert');
4-
const spawn = require('child_process').spawn;
5-
const fork = require('child_process').fork;
4+
const { fork, spawn } = require('child_process');
65

76
// Fork, then spawn. The spawned process should not hang.
87
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-domain-exit-dispose.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
require('../common');
32
const common = require('../common');
43
const assert = require('assert');
54
const domain = require('domain');

test/parallel/test-http-invalidheaderfield2.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
require('../common');
33
const assert = require('assert');
44
const inspect = require('util').inspect;
5-
const checkIsHttpToken = require('_http_common')._checkIsHttpToken;
6-
const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar;
5+
const { _checkIsHttpToken, _checkInvalidHeaderChar } = require('_http_common');
76

87
// Good header field names
98
[
@@ -29,8 +28,8 @@ const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar;
2928
'3.14159265359'
3029
].forEach(function(str) {
3130
assert.strictEqual(
32-
checkIsHttpToken(str), true,
33-
`checkIsHttpToken(${inspect(str)}) unexpectedly failed`);
31+
_checkIsHttpToken(str), true,
32+
`_checkIsHttpToken(${inspect(str)}) unexpectedly failed`);
3433
});
3534
// Bad header field names
3635
[
@@ -55,8 +54,8 @@ const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar;
5554
'This,That'
5655
].forEach(function(str) {
5756
assert.strictEqual(
58-
checkIsHttpToken(str), false,
59-
`checkIsHttpToken(${inspect(str)}) unexpectedly succeeded`);
57+
_checkIsHttpToken(str), false,
58+
`_checkIsHttpToken(${inspect(str)}) unexpectedly succeeded`);
6059
});
6160

6261

@@ -68,8 +67,8 @@ const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar;
6867
'!@#$%^&*()-_=+\\;\':"[]{}<>,./?|~`'
6968
].forEach(function(str) {
7069
assert.strictEqual(
71-
checkInvalidHeaderChar(str), false,
72-
`checkInvalidHeaderChar(${inspect(str)}) unexpectedly failed`);
70+
_checkInvalidHeaderChar(str), false,
71+
`_checkInvalidHeaderChar(${inspect(str)}) unexpectedly failed`);
7372
});
7473

7574
// Bad header field values
@@ -84,6 +83,6 @@ const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar;
8483
'Ding!\x07'
8584
].forEach(function(str) {
8685
assert.strictEqual(
87-
checkInvalidHeaderChar(str), true,
88-
`checkInvalidHeaderChar(${inspect(str)}) unexpectedly succeeded`);
86+
_checkInvalidHeaderChar(str), true,
87+
`_checkInvalidHeaderChar(${inspect(str)}) unexpectedly succeeded`);
8988
});

0 commit comments

Comments
 (0)