lib: remove usage of require('util')
Remove usage of public `require('util').inspect` and `require('util').formatWithOptions`. PR-URL: https://github.com/nodejs/node/pull/26777 Refs: https://github.com/nodejs/node/issues/26546 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
a1330af6a3
commit
cf1b9d395d
@ -15,10 +15,13 @@ const {
|
|||||||
} = require('internal/errors');
|
} = require('internal/errors');
|
||||||
const { previewEntries } = internalBinding('util');
|
const { previewEntries } = internalBinding('util');
|
||||||
const { Buffer: { isBuffer } } = require('buffer');
|
const { Buffer: { isBuffer } } = require('buffer');
|
||||||
const util = require('util');
|
const {
|
||||||
|
inspect,
|
||||||
|
formatWithOptions
|
||||||
|
} = require('internal/util/inspect');
|
||||||
const {
|
const {
|
||||||
isTypedArray, isSet, isMap, isSetIterator, isMapIterator,
|
isTypedArray, isSet, isMap, isSetIterator, isMapIterator,
|
||||||
} = util.types;
|
} = require('internal/util/types');
|
||||||
const kCounts = Symbol('counts');
|
const kCounts = Symbol('counts');
|
||||||
|
|
||||||
const kTraceConsoleCategory = 'node,node.console';
|
const kTraceConsoleCategory = 'node,node.console';
|
||||||
@ -271,12 +274,12 @@ Console.prototype[kGetInspectOptions] = function(stream) {
|
|||||||
|
|
||||||
Console.prototype[kFormatForStdout] = function(args) {
|
Console.prototype[kFormatForStdout] = function(args) {
|
||||||
const opts = this[kGetInspectOptions](this._stdout);
|
const opts = this[kGetInspectOptions](this._stdout);
|
||||||
return util.formatWithOptions(opts, ...args);
|
return formatWithOptions(opts, ...args);
|
||||||
};
|
};
|
||||||
|
|
||||||
Console.prototype[kFormatForStderr] = function(args) {
|
Console.prototype[kFormatForStderr] = function(args) {
|
||||||
const opts = this[kGetInspectOptions](this._stderr);
|
const opts = this[kGetInspectOptions](this._stderr);
|
||||||
return util.formatWithOptions(opts, ...args);
|
return formatWithOptions(opts, ...args);
|
||||||
};
|
};
|
||||||
|
|
||||||
const consoleMethods = {
|
const consoleMethods = {
|
||||||
@ -291,7 +294,7 @@ const consoleMethods = {
|
|||||||
|
|
||||||
|
|
||||||
dir(object, options) {
|
dir(object, options) {
|
||||||
this[kWriteToConsole](kUseStdout, util.inspect(object, {
|
this[kWriteToConsole](kUseStdout, inspect(object, {
|
||||||
customInspect: false,
|
customInspect: false,
|
||||||
...this[kGetInspectOptions](this._stdout),
|
...this[kGetInspectOptions](this._stdout),
|
||||||
...options
|
...options
|
||||||
@ -406,7 +409,7 @@ const consoleMethods = {
|
|||||||
if (cliTable === undefined) cliTable = require('internal/cli_table');
|
if (cliTable === undefined) cliTable = require('internal/cli_table');
|
||||||
const final = (k, v) => this.log(cliTable(k, v));
|
const final = (k, v) => this.log(cliTable(k, v));
|
||||||
|
|
||||||
const inspect = (v) => {
|
const _inspect = (v) => {
|
||||||
const depth = v !== null &&
|
const depth = v !== null &&
|
||||||
typeof v === 'object' &&
|
typeof v === 'object' &&
|
||||||
!isArray(v) &&
|
!isArray(v) &&
|
||||||
@ -416,10 +419,10 @@ const consoleMethods = {
|
|||||||
maxArrayLength: 3,
|
maxArrayLength: 3,
|
||||||
...this[kGetInspectOptions](this._stdout)
|
...this[kGetInspectOptions](this._stdout)
|
||||||
};
|
};
|
||||||
return util.inspect(v, opt);
|
return inspect(v, opt);
|
||||||
};
|
};
|
||||||
const getIndexArray = (length) => ArrayFrom(
|
const getIndexArray = (length) => ArrayFrom(
|
||||||
{ length }, (_, i) => inspect(i));
|
{ length }, (_, i) => _inspect(i));
|
||||||
|
|
||||||
const mapIter = isMapIterator(tabularData);
|
const mapIter = isMapIterator(tabularData);
|
||||||
let isKeyValue = false;
|
let isKeyValue = false;
|
||||||
@ -436,14 +439,14 @@ const consoleMethods = {
|
|||||||
let length = 0;
|
let length = 0;
|
||||||
if (mapIter) {
|
if (mapIter) {
|
||||||
for (; i < tabularData.length / 2; ++i) {
|
for (; i < tabularData.length / 2; ++i) {
|
||||||
keys.push(inspect(tabularData[i * 2]));
|
keys.push(_inspect(tabularData[i * 2]));
|
||||||
values.push(inspect(tabularData[i * 2 + 1]));
|
values.push(_inspect(tabularData[i * 2 + 1]));
|
||||||
length++;
|
length++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (const [k, v] of tabularData) {
|
for (const [k, v] of tabularData) {
|
||||||
keys.push(inspect(k));
|
keys.push(_inspect(k));
|
||||||
values.push(inspect(v));
|
values.push(_inspect(v));
|
||||||
length++;
|
length++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -465,7 +468,7 @@ const consoleMethods = {
|
|||||||
const values = [];
|
const values = [];
|
||||||
let length = 0;
|
let length = 0;
|
||||||
for (const v of tabularData) {
|
for (const v of tabularData) {
|
||||||
values.push(inspect(v));
|
values.push(_inspect(v));
|
||||||
length++;
|
length++;
|
||||||
}
|
}
|
||||||
return final([iterKey, valuesKey], [getIndexArray(length), values]);
|
return final([iterKey, valuesKey], [getIndexArray(length), values]);
|
||||||
@ -482,7 +485,7 @@ const consoleMethods = {
|
|||||||
(typeof item !== 'function' && typeof item !== 'object');
|
(typeof item !== 'function' && typeof item !== 'object');
|
||||||
if (properties === undefined && primitive) {
|
if (properties === undefined && primitive) {
|
||||||
hasPrimitives = true;
|
hasPrimitives = true;
|
||||||
valuesKeyArray[i] = inspect(item);
|
valuesKeyArray[i] = _inspect(item);
|
||||||
} else {
|
} else {
|
||||||
const keys = properties || ObjectKeys(item);
|
const keys = properties || ObjectKeys(item);
|
||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
@ -491,7 +494,7 @@ const consoleMethods = {
|
|||||||
if ((primitive && properties) || !hasOwnProperty(item, key))
|
if ((primitive && properties) || !hasOwnProperty(item, key))
|
||||||
map[key][i] = '';
|
map[key][i] = '';
|
||||||
else
|
else
|
||||||
map[key][i] = item == null ? item : inspect(item[key]);
|
map[key][i] = item == null ? item : _inspect(item[key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user