Skip to content

Commit f5115b4

Browse files
xtx1130targos
authored andcommitted
errors,console: refactor to use ES2021 syntax
PR-URL: #42872 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent e45861f commit f5115b4

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

‎lib/internal/console/constructor.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */){
100100
// We have to test new.target here to see if this function is called
101101
// with new, because we need to define a custom instanceof to accommodate
102102
// the global console.
103-
if(!new.target){
103+
if(new.target===undefined){
104104
returnReflectConstruct(Console,arguments);
105105
}
106106

@@ -486,7 +486,7 @@ const consoleMethods ={
486486
if(tabularData===null||typeoftabularData!=='object')
487487
returnthis.log(tabularData);
488488

489-
if(cliTable===undefined)cliTable=require('internal/cli_table');
489+
cliTable??=require('internal/cli_table');
490490
constfinal=(k,v)=>this.log(cliTable(k,v));
491491

492492
const_inspect=(v)=>{
@@ -570,8 +570,7 @@ const consoleMethods ={
570570
}else{
571571
constkeys=properties||ObjectKeys(item);
572572
for(constkeyofkeys){
573-
if(map[key]===undefined)
574-
map[key]=[];
573+
map[key]??=[];
575574
if((primitive&&properties)||
576575
!ObjectPrototypeHasOwnProperty(item,key))
577576
map[key][i]='';

‎lib/internal/errors.js‎

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,24 +175,19 @@ let assert;
175175

176176
letinternalUtil=null;
177177
functionlazyInternalUtil(){
178-
if(!internalUtil){
179-
internalUtil=require('internal/util');
180-
}
178+
internalUtil??=require('internal/util');
181179
returninternalUtil;
182180
}
183181

184182
letinternalUtilInspect=null;
185183
functionlazyInternalUtilInspect(){
186-
if(!internalUtilInspect){
187-
internalUtilInspect=require('internal/util/inspect');
188-
}
184+
internalUtilInspect??=require('internal/util/inspect');
189185
returninternalUtilInspect;
190186
}
191187

192188
letbuffer;
193189
functionlazyBuffer(){
194-
if(buffer===undefined)
195-
buffer=require('buffer').Buffer;
190+
buffer??=require('buffer').Buffer;
196191
returnbuffer;
197192
}
198193

@@ -411,7 +406,7 @@ function E(sym, val, def, ...otherClasses){
411406
functiongetMessage(key,args,self){
412407
constmsg=messages.get(key);
413408

414-
if(assert===undefined)assert=require('internal/assert');
409+
assert??=require('internal/assert');
415410

416411
if(typeofmsg==='function'){
417412
assert(
@@ -439,19 +434,15 @@ function getMessage(key, args, self){
439434
letuvBinding;
440435

441436
functionlazyUv(){
442-
if(!uvBinding){
443-
uvBinding=internalBinding('uv');
444-
}
437+
uvBinding??=internalBinding('uv');
445438
returnuvBinding;
446439
}
447440

448441
constuvUnmappedError=['UNKNOWN','unknown error'];
449442

450443
functionuvErrmapGet(name){
451444
uvBinding=lazyUv();
452-
if(!uvBinding.errmap){
453-
uvBinding.errmap=uvBinding.getErrorMap();
454-
}
445+
uvBinding.errmap??=uvBinding.getErrorMap();
455446
returnMapPrototypeGet(uvBinding.errmap,name);
456447
}
457448

@@ -578,7 +569,7 @@ const errnoException = hideStackFrames(
578569
// getSystemErrorName(err) to guard against invalid arguments from users.
579570
// This can be replaced with [ code ] = errmap.get(err) when this method
580571
// is no longer exposed to user land.
581-
if(util===undefined)util=require('util');
572+
util??=require('util');
582573
constcode=util.getSystemErrorName(err);
583574
constmessage=original ?
584575
`${syscall}${code}${original}` : `${syscall}${code}`;
@@ -612,7 +603,7 @@ const exceptionWithHostPort = hideStackFrames(
612603
// getSystemErrorName(err) to guard against invalid arguments from users.
613604
// This can be replaced with [ code ] = errmap.get(err) when this method
614605
// is no longer exposed to user land.
615-
if(util===undefined)util=require('util');
606+
util??=require('util');
616607
constcode=util.getSystemErrorName(err);
617608
letdetails='';
618609
if(port&&port>0){
@@ -1224,7 +1215,7 @@ E('ERR_INVALID_ARG_TYPE',
12241215
}elseif(typeofactual==='function'&&actual.name){
12251216
msg+=`. Received function ${actual.name}`;
12261217
}elseif(typeofactual==='object'){
1227-
if(actual.constructor&&actual.constructor.name){
1218+
if(actual.constructor?.name){
12281219
msg+=`. Received an instance of ${actual.constructor.name}`;
12291220
}else{
12301221
constinspected=lazyInternalUtilInspect()
@@ -1320,7 +1311,7 @@ E('ERR_INVALID_RETURN_PROPERTY_VALUE', (input, name, prop, value) =>{
13201311
},TypeError);
13211312
E('ERR_INVALID_RETURN_VALUE',(input,name,value)=>{
13221313
lettype;
1323-
if(value&&value.constructor&&value.constructor.name){
1314+
if(value?.constructor?.name){
13241315
type=`instance of ${value.constructor.name}`;
13251316
}else{
13261317
type=`type ${typeofvalue}`;

0 commit comments

Comments
(0)