Skip to content

Commit 56ffc07

Browse files
xtx1130juanarbol
authored andcommitted
errors,console: refactor to use ES2021 syntax
PR-URL: #42872 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent 20acb48 commit 56ffc07

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

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

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

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

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

448441
const uvUnmappedError = ['UNKNOWN', 'unknown error'];
449442

450443
function uvErrmapGet(name) {
451444
uvBinding = lazyUv();
452-
if (!uvBinding.errmap) {
453-
uvBinding.errmap = uvBinding.getErrorMap();
454-
}
445+
uvBinding.errmap ??= uvBinding.getErrorMap();
455446
return MapPrototypeGet(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
const code = util.getSystemErrorName(err);
583574
const message = 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
const code = util.getSystemErrorName(err);
617608
let details = '';
618609
if (port && port > 0) {
@@ -1224,7 +1215,7 @@ E('ERR_INVALID_ARG_TYPE',
12241215
} else if (typeof actual === 'function' && actual.name) {
12251216
msg += `. Received function ${actual.name}`;
12261217
} else if (typeof actual === '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
const inspected = 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
let type;
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 ${typeof value}`;

0 commit comments

Comments
 (0)