Skip to content

Commit 336242a

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 a6e1e7a commit 336242a

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

lib/internal/console/constructor.js

+3-4
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
return ReflectConstruct(Console, arguments);
105105
}
106106

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

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

492492
const _inspect = (v) => {
@@ -570,8 +570,7 @@ const consoleMethods = {
570570
} else {
571571
const keys = properties || ObjectKeys(item);
572572
for (const key of keys) {
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

+10-19
Original file line numberDiff line numberDiff line change
@@ -175,24 +175,19 @@ let assert;
175175

176176
let internalUtil = null;
177177
function lazyInternalUtil() {
178-
if (!internalUtil) {
179-
internalUtil = require('internal/util');
180-
}
178+
internalUtil ??= require('internal/util');
181179
return internalUtil;
182180
}
183181

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

192188
let buffer;
193189
function lazyBuffer() {
194-
if (buffer === undefined)
195-
buffer = require('buffer').Buffer;
190+
buffer ??= require('buffer').Buffer;
196191
return buffer;
197192
}
198193

@@ -421,7 +416,7 @@ function E(sym, val, def, ...otherClasses) {
421416
function getMessage(key, args, self) {
422417
const msg = messages.get(key);
423418

424-
if (assert === undefined) assert = require('internal/assert');
419+
assert ??= require('internal/assert');
425420

426421
if (typeof msg === 'function') {
427422
assert(
@@ -449,19 +444,15 @@ function getMessage(key, args, self) {
449444
let uvBinding;
450445

451446
function lazyUv() {
452-
if (!uvBinding) {
453-
uvBinding = internalBinding('uv');
454-
}
447+
uvBinding ??= internalBinding('uv');
455448
return uvBinding;
456449
}
457450

458451
const uvUnmappedError = ['UNKNOWN', 'unknown error'];
459452

460453
function uvErrmapGet(name) {
461454
uvBinding = lazyUv();
462-
if (!uvBinding.errmap) {
463-
uvBinding.errmap = uvBinding.getErrorMap();
464-
}
455+
uvBinding.errmap ??= uvBinding.getErrorMap();
465456
return MapPrototypeGet(uvBinding.errmap, name);
466457
}
467458

@@ -588,7 +579,7 @@ const errnoException = hideStackFrames(
588579
// getSystemErrorName(err) to guard against invalid arguments from users.
589580
// This can be replaced with [ code ] = errmap.get(err) when this method
590581
// is no longer exposed to user land.
591-
if (util === undefined) util = require('util');
582+
util ??= require('util');
592583
const code = util.getSystemErrorName(err);
593584
const message = original ?
594585
`${syscall} ${code} ${original}` : `${syscall} ${code}`;
@@ -622,7 +613,7 @@ const exceptionWithHostPort = hideStackFrames(
622613
// getSystemErrorName(err) to guard against invalid arguments from users.
623614
// This can be replaced with [ code ] = errmap.get(err) when this method
624615
// is no longer exposed to user land.
625-
if (util === undefined) util = require('util');
616+
util ??= require('util');
626617
const code = util.getSystemErrorName(err);
627618
let details = '';
628619
if (port && port > 0) {
@@ -1238,7 +1229,7 @@ E('ERR_INVALID_ARG_TYPE',
12381229
} else if (typeof actual === 'function' && actual.name) {
12391230
msg += `. Received function ${actual.name}`;
12401231
} else if (typeof actual === 'object') {
1241-
if (actual.constructor && actual.constructor.name) {
1232+
if (actual.constructor?.name) {
12421233
msg += `. Received an instance of ${actual.constructor.name}`;
12431234
} else {
12441235
const inspected = lazyInternalUtilInspect()
@@ -1332,7 +1323,7 @@ E('ERR_INVALID_RETURN_PROPERTY_VALUE', (input, name, prop, value) => {
13321323
}, TypeError);
13331324
E('ERR_INVALID_RETURN_VALUE', (input, name, value) => {
13341325
let type;
1335-
if (value && value.constructor && value.constructor.name) {
1326+
if (value?.constructor?.name) {
13361327
type = `instance of ${value.constructor.name}`;
13371328
} else {
13381329
type = `type ${typeof value}`;

0 commit comments

Comments
 (0)