Skip to content

Commit eb023ef

Browse files
vsemozhetbytaddaleax
authored andcommitted
doc, lib, test: do not re-require needlessly
Backport-PR-URL: #14524 Backport-Reviewed-By: Anna Henningsen <[email protected]> PR-URL: #14244 Reviewed-By: Alexey Orlenko <[email protected]>
1 parent c704c02 commit eb023ef

22 files changed

+67
-74
lines changed

doc/api/child_process.md

+3-2
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

+2-2
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

+6-8
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() {
@@ -178,10 +180,8 @@ function setupKillAndExit() {
178180
}
179181
}
180182

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

186186
return true;
187187
};
@@ -212,8 +212,7 @@ function setupSignalHandlers() {
212212
const err = wrap.start(signum);
213213
if (err) {
214214
wrap.close();
215-
const errnoException = require('util')._errnoException;
216-
throw errnoException(err, 'uv_signal_start');
215+
throw util._errnoException(err, 'uv_signal_start');
217216
}
218217

219218
signalWraps[type] = wrap;
@@ -253,10 +252,9 @@ function setupChannel() {
253252

254253

255254
function setupRawDebug() {
256-
const format = require('util').format;
257255
const rawDebug = process._rawDebug;
258256
process._rawDebug = function() {
259-
rawDebug(format.apply(null, arguments));
257+
rawDebug(util.format.apply(null, arguments));
260258
};
261259
}
262260

lib/repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ try {
8585
}
8686

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

9090
// If obj.hasOwnProperty has been overridden, then calling
9191
// obj.hasOwnProperty(prop) will break.

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

+3-3
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

+1-1
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

+1-2
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

+1-2
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

+2-2
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);

test/parallel/test-domain-exit-dispose.js

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
2423
const common = require('../common');
2524
const assert = require('assert');
2625
const domain = require('domain');

test/parallel/test-handle-wrap-isrefed.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ const strictEqual = require('assert').strictEqual;
2424
}
2525

2626

27+
const dgram = require('dgram');
28+
2729
// dgram ipv4
2830
{
29-
const dgram = require('dgram');
3031
const sock4 = dgram.createSocket('udp4');
3132
strictEqual(Object.getPrototypeOf(sock4._handle).hasOwnProperty('hasRef'),
3233
true, 'udp_wrap: ipv4: hasRef() missing');
@@ -46,7 +47,6 @@ const strictEqual = require('assert').strictEqual;
4647

4748
// dgram ipv6
4849
{
49-
const dgram = require('dgram');
5050
const sock6 = dgram.createSocket('udp6');
5151
strictEqual(Object.getPrototypeOf(sock6._handle).hasOwnProperty('hasRef'),
5252
true, 'udp_wrap: ipv6: hasRef() missing');

test/parallel/test-http-invalidheaderfield2.js

+9-10
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
});

test/parallel/test-listen-fd-detached.js

-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ function parent() {
8585
}).listen(0, function() {
8686
console.error('server listening on %d', this.address().port);
8787

88-
const spawn = require('child_process').spawn;
8988
const child = spawn(process.execPath, [__filename, 'child'], {
9089
stdio: [ 'ignore', 'ignore', 'ignore', server._handle ],
9190
detached: true

test/parallel/test-net-pause-resume-connecting.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,35 @@ const server = net.createServer(function(conn) {
4040

4141
server.listen(0, function() {
4242
// Client 1
43-
conn = require('net').createConnection(this.address().port, 'localhost');
43+
conn = net.createConnection(this.address().port, 'localhost');
4444
conn.resume();
4545
conn.on('data', onDataOk);
4646

4747

4848
// Client 2
49-
conn = require('net').createConnection(this.address().port, 'localhost');
49+
conn = net.createConnection(this.address().port, 'localhost');
5050
conn.pause();
5151
conn.resume();
5252
conn.on('data', onDataOk);
5353

5454

5555
// Client 3
56-
conn = require('net').createConnection(this.address().port, 'localhost');
56+
conn = net.createConnection(this.address().port, 'localhost');
5757
conn.pause();
5858
conn.on('data', common.mustNotCall());
5959
scheduleTearDown(conn);
6060

6161

6262
// Client 4
63-
conn = require('net').createConnection(this.address().port, 'localhost');
63+
conn = net.createConnection(this.address().port, 'localhost');
6464
conn.resume();
6565
conn.pause();
6666
conn.resume();
6767
conn.on('data', onDataOk);
6868

6969

7070
// Client 5
71-
conn = require('net').createConnection(this.address().port, 'localhost');
71+
conn = net.createConnection(this.address().port, 'localhost');
7272
conn.resume();
7373
conn.resume();
7474
conn.pause();

test/parallel/test-process-exit-code.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,23 @@ function child5() {
8383
}
8484

8585
function parent() {
86+
const { spawn } = require('child_process');
87+
const node = process.execPath;
88+
const f = __filename;
89+
const option = { stdio: [ 0, 1, 'ignore' ] };
90+
91+
const test = (arg, exit) => {
92+
spawn(node, [f, arg], option).on('exit', (code) => {
93+
assert.strictEqual(
94+
code, exit,
95+
`wrong exit for ${arg}\nexpected:${exit} but got:${code}`);
96+
console.log('ok - %s exited with %d', arg, exit);
97+
});
98+
};
99+
86100
test('child1', 42);
87101
test('child2', 42);
88102
test('child3', 0);
89103
test('child4', 1);
90104
test('child5', 99);
91105
}
92-
93-
function test(arg, exit) {
94-
const spawn = require('child_process').spawn;
95-
const node = process.execPath;
96-
const f = __filename;
97-
const option = { stdio: [ 0, 1, 'ignore' ] };
98-
spawn(node, [f, arg], option).on('exit', function(code) {
99-
assert.strictEqual(
100-
code, exit,
101-
`wrong exit for ${arg}\nexpected:${exit} but got:${code}`);
102-
console.log('ok - %s exited with %d', arg, exit);
103-
});
104-
}

test/parallel/test-readline-interface.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ const readline = require('readline');
2727
const internalReadline = require('internal/readline');
2828
const EventEmitter = require('events').EventEmitter;
2929
const inherits = require('util').inherits;
30-
const Writable = require('stream').Writable;
31-
const Readable = require('stream').Readable;
30+
const { Writable, Readable } = require('stream');
3231

3332
function FakeInput() {
3433
EventEmitter.call(this);

test/parallel/test-require-symlink.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ const common = require('../common');
44
const assert = require('assert');
55
const path = require('path');
66
const fs = require('fs');
7-
const exec = require('child_process').exec;
8-
const spawn = require('child_process').spawn;
7+
const { exec, spawn } = require('child_process');
98
const util = require('util');
109

1110
common.refreshTmpDir();

test/parallel/test-stream2-push.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121

2222
'use strict';
2323
require('../common');
24-
const Readable = require('stream').Readable;
25-
const Writable = require('stream').Writable;
2624
const assert = require('assert');
25+
const { Readable, Writable } = require('stream');
2726

2827
const EE = require('events').EventEmitter;
2928

test/parallel/test-tls-session-cache.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ if (!common.opensslCli)
2828
if (!common.hasCrypto)
2929
common.skip('missing crypto');
3030

31+
const assert = require('assert');
32+
const tls = require('tls');
33+
const fs = require('fs');
34+
const { join } = require('path');
35+
const { spawn } = require('child_process');
36+
37+
const keyFile = join(common.fixturesDir, 'agent.key');
38+
const certFile = join(common.fixturesDir, 'agent.crt');
39+
3140
doTest({ tickets: false }, function() {
3241
doTest({ tickets: true }, function() {
3342
doTest({ tickets: false, invalidSession: true }, function() {
@@ -37,14 +46,6 @@ doTest({ tickets: false }, function() {
3746
});
3847

3948
function doTest(testOptions, callback) {
40-
const assert = require('assert');
41-
const tls = require('tls');
42-
const fs = require('fs');
43-
const join = require('path').join;
44-
const spawn = require('child_process').spawn;
45-
46-
const keyFile = join(common.fixturesDir, 'agent.key');
47-
const certFile = join(common.fixturesDir, 'agent.crt');
4849
const key = fs.readFileSync(keyFile);
4950
const cert = fs.readFileSync(certFile);
5051
const options = {

0 commit comments

Comments
 (0)