From 56ffc07ffbd14c8f454ed34b9ac870680cba69e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=8F=9C?= Date: Thu, 28 Apr 2022 18:35:04 +0800 Subject: [PATCH] errors,console: refactor to use ES2021 syntax PR-URL: https://github.com/nodejs/node/pull/42872 Reviewed-By: Antoine du Hamel Reviewed-By: Darshan Sen --- lib/internal/console/constructor.js | 7 +++---- lib/internal/errors.js | 29 ++++++++++------------------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index 5ad57be3bed6a8..18e66a7afee0ee 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -100,7 +100,7 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) { // We have to test new.target here to see if this function is called // with new, because we need to define a custom instanceof to accommodate // the global console. - if (!new.target) { + if (new.target === undefined) { return ReflectConstruct(Console, arguments); } @@ -486,7 +486,7 @@ const consoleMethods = { if (tabularData === null || typeof tabularData !== 'object') return this.log(tabularData); - if (cliTable === undefined) cliTable = require('internal/cli_table'); + cliTable ??= require('internal/cli_table'); const final = (k, v) => this.log(cliTable(k, v)); const _inspect = (v) => { @@ -570,8 +570,7 @@ const consoleMethods = { } else { const keys = properties || ObjectKeys(item); for (const key of keys) { - if (map[key] === undefined) - map[key] = []; + map[key] ??= []; if ((primitive && properties) || !ObjectPrototypeHasOwnProperty(item, key)) map[key][i] = ''; diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 4a55e84eeb1d02..ddef255d1c3988 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -175,24 +175,19 @@ let assert; let internalUtil = null; function lazyInternalUtil() { - if (!internalUtil) { - internalUtil = require('internal/util'); - } + internalUtil ??= require('internal/util'); return internalUtil; } let internalUtilInspect = null; function lazyInternalUtilInspect() { - if (!internalUtilInspect) { - internalUtilInspect = require('internal/util/inspect'); - } + internalUtilInspect ??= require('internal/util/inspect'); return internalUtilInspect; } let buffer; function lazyBuffer() { - if (buffer === undefined) - buffer = require('buffer').Buffer; + buffer ??= require('buffer').Buffer; return buffer; } @@ -411,7 +406,7 @@ function E(sym, val, def, ...otherClasses) { function getMessage(key, args, self) { const msg = messages.get(key); - if (assert === undefined) assert = require('internal/assert'); + assert ??= require('internal/assert'); if (typeof msg === 'function') { assert( @@ -439,9 +434,7 @@ function getMessage(key, args, self) { let uvBinding; function lazyUv() { - if (!uvBinding) { - uvBinding = internalBinding('uv'); - } + uvBinding ??= internalBinding('uv'); return uvBinding; } @@ -449,9 +442,7 @@ const uvUnmappedError = ['UNKNOWN', 'unknown error']; function uvErrmapGet(name) { uvBinding = lazyUv(); - if (!uvBinding.errmap) { - uvBinding.errmap = uvBinding.getErrorMap(); - } + uvBinding.errmap ??= uvBinding.getErrorMap(); return MapPrototypeGet(uvBinding.errmap, name); } @@ -578,7 +569,7 @@ const errnoException = hideStackFrames( // getSystemErrorName(err) to guard against invalid arguments from users. // This can be replaced with [ code ] = errmap.get(err) when this method // is no longer exposed to user land. - if (util === undefined) util = require('util'); + util ??= require('util'); const code = util.getSystemErrorName(err); const message = original ? `${syscall} ${code} ${original}` : `${syscall} ${code}`; @@ -612,7 +603,7 @@ const exceptionWithHostPort = hideStackFrames( // getSystemErrorName(err) to guard against invalid arguments from users. // This can be replaced with [ code ] = errmap.get(err) when this method // is no longer exposed to user land. - if (util === undefined) util = require('util'); + util ??= require('util'); const code = util.getSystemErrorName(err); let details = ''; if (port && port > 0) { @@ -1224,7 +1215,7 @@ E('ERR_INVALID_ARG_TYPE', } else if (typeof actual === 'function' && actual.name) { msg += `. Received function ${actual.name}`; } else if (typeof actual === 'object') { - if (actual.constructor && actual.constructor.name) { + if (actual.constructor?.name) { msg += `. Received an instance of ${actual.constructor.name}`; } else { const inspected = lazyInternalUtilInspect() @@ -1320,7 +1311,7 @@ E('ERR_INVALID_RETURN_PROPERTY_VALUE', (input, name, prop, value) => { }, TypeError); E('ERR_INVALID_RETURN_VALUE', (input, name, value) => { let type; - if (value && value.constructor && value.constructor.name) { + if (value?.constructor?.name) { type = `instance of ${value.constructor.name}`; } else { type = `type ${typeof value}`;