Skip to content

Commit

Permalink
errors,console: refactor to use ES2021 syntax
Browse files Browse the repository at this point in the history
PR-URL: #42872
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
xtx1130 authored and targos committed May 2, 2022
1 parent a6e1e7a commit 336242a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
7 changes: 3 additions & 4 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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] = '';
Expand Down
29 changes: 10 additions & 19 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -421,7 +416,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(
Expand Down Expand Up @@ -449,19 +444,15 @@ function getMessage(key, args, self) {
let uvBinding;

function lazyUv() {
if (!uvBinding) {
uvBinding = internalBinding('uv');
}
uvBinding ??= internalBinding('uv');
return uvBinding;
}

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);
}

Expand Down Expand Up @@ -588,7 +579,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}`;
Expand Down Expand Up @@ -622,7 +613,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) {
Expand Down Expand Up @@ -1238,7 +1229,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()
Expand Down Expand Up @@ -1332,7 +1323,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}`;
Expand Down

0 comments on commit 336242a

Please sign in to comment.