Skip to content

Commit

Permalink
errors & console: refactor to use ES2020 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
xtx1130 committed Feb 23, 2022
1 parent b39dabe commit f578723
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
7 changes: 3 additions & 4 deletions lib/internal/console/constructor.js
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
31 changes: 11 additions & 20 deletions lib/internal/errors.js
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 @@ -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(
Expand Down Expand Up @@ -439,19 +434,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 @@ -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}`;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1227,7 +1218,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 @@ -1311,7 +1302,7 @@ E('ERR_INVALID_RETURN_PROPERTY', (input, name, prop, value) => {
}, TypeError);
E('ERR_INVALID_RETURN_PROPERTY_VALUE', (input, name, prop, 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 All @@ -1321,7 +1312,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 f578723

Please sign in to comment.