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 <eaglexrlnk@gmail.com>
1 parent 792acc1 commit 38ae5c4

19 files changed

Lines changed: 65 additions & 79 deletions

‎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-
constnormal=require('child_process').fork('subprocess.js', ['normal']);
1050-
constspecial=require('child_process').fork('subprocess.js', ['special']);
1049+
const { fork } =require('child_process');
1050+
constnormal=fork('subprocess.js', ['normal']);
1051+
constspecial=fork('subprocess.js', ['special']);
10511052

10521053
// Open up the server and send sockets to child
10531054
constserver=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+
constutil=require('util');
4+
35
var_lazyConstants=null;
46

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

188-
if(err){
189-
consterrnoException=require('util')._errnoException;
190-
throwerrnoException(err,'kill');
191-
}
190+
if(err)
191+
throwutil._errnoException(err,'kill');
192192

193193
returntrue;
194194
};
@@ -220,8 +220,7 @@ function setupSignalHandlers() {
220220
consterr=wrap.start(signum);
221221
if(err){
222222
wrap.close();
223-
consterrnoException=require('util')._errnoException;
224-
throwerrnoException(err,'uv_signal_start');
223+
throwutil._errnoException(err,'uv_signal_start');
225224
}
226225

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

262261

263262
functionsetupRawDebug(){
264-
constformat=require('util').format;
265263
constrawDebug=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
vardir,files,f,name,base,ext,abs,subfiles,s;
871871
group=[];
872-
varpaths=module.paths.concat(require('module').globalPaths);
872+
varpaths=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
constfs=require('fs');
55
constassert=require('assert');
66
constos=require('os');
7-
constchild_process=require('child_process');
7+
const{ exec, execSync, spawn, spawnSync }=require('child_process');
88
conststream=require('stream');
99
constutil=require('util');
1010
constTimer=process.binding('timer_wrap').Timer;
@@ -121,7 +121,7 @@ Object.defineProperty(exports, 'inFreeBSDJail', {
121121
if(inFreeBSDJail!==null)returninFreeBSDJail;
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-
constopensslCmd=child_process.spawnSync(opensslCli,['version']);
171+
constopensslCmd=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-
constchild=child_process.exec(testCmd);
222+
constchild=exec(testCmd);
223223
child.on('exit',functiononExit(exitCode,signal){
224224
consterrMsg='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-
constspawn=require('child_process').spawn;
243-
244242
if(exports.isWindows){
245243
returnspawn('more',[],options);
246244
}else{
@@ -250,8 +248,6 @@ exports.spawnCat = function(options) {
250248

251249

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

262258

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

273267

274268
exports.spawnSyncPwd=function(options){
275-
constspawnSync=require('child_process').spawnSync;
276-
277269
if(exports.isWindows){
278270
returnspawnSync('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
constassert=require('assert');
5-
consta=require('assert');
65

76
functionmakeBlock(f){
87
constargs=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
constassert=require('assert');
4-
consta=require('assert');
4+
consta=assert;
55

66
functionmakeBlock(f){
77
constargs=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
constcommon=require('../common');
33
constassert=require('assert');
4-
constspawn=require('child_process').spawn;
5-
constfork=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
constassert=require('assert');
44
constpath=require('path');
55
constnet=require('net');
6-
constfork=require('child_process').fork;
7-
constspawn=require('child_process').spawn;
6+
const{ fork, spawn }=require('child_process');
87

98
constemptyFile=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
constcommon=require('../common');
43
constassert=require('assert');
54
constdomain=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
constassert=require('assert');
44
constinspect=require('util').inspect;
5-
constcheckIsHttpToken=require('_http_common')._checkIsHttpToken;
6-
constcheckInvalidHeaderChar=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)