Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors,console: refactor to use ES2020 & ES2021 syntax #42872

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
errors,console: refactor to use ES2020 syntax
  • Loading branch information
xtx1130 committed Apr 25, 2022
commit 461a0bb2f7715bbb31f573f2ca270aa1aa193a58
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