Skip to content
Closed
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
123 changes: 54 additions & 69 deletions lib/internal/util/inspect.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -191,7 +191,7 @@ function inspect(value, opts) {
}
if (ctx.colors) ctx.stylize = stylizeWithColor;
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
return formatValue(ctx, value, ctx.depth);
return formatValue(ctx, value, 0);
}
inspect.custom = customInspectSymbol;

Expand All@@ -209,34 +209,34 @@ Object.defineProperty(inspect, 'defaultOptions', {

// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
inspect.colors = Object.assign(Object.create(null), {
'bold': [1, 22],
'italic': [3, 23],
'underline': [4, 24],
'inverse': [7, 27],
'white': [37, 39],
'grey': [90, 39],
'black': [30, 39],
'blue': [34, 39],
'cyan': [36, 39],
'green': [32, 39],
'magenta': [35, 39],
'red': [31, 39],
'yellow': [33, 39]
bold: [1, 22],
italic: [3, 23],
underline: [4, 24],
inverse: [7, 27],
white: [37, 39],
grey: [90, 39],
black: [30, 39],
blue: [34, 39],
cyan: [36, 39],
green: [32, 39],
magenta: [35, 39],
red: [31, 39],
yellow: [33, 39]
});

// Don't use 'blue' not visible on cmd.exe
inspect.styles = Object.assign(Object.create(null), {
'special': 'cyan',
'number': 'yellow',
'bigint': 'yellow',
'boolean': 'yellow',
'undefined': 'grey',
'null': 'bold',
'string': 'green',
'symbol': 'green',
'date': 'magenta',
special: 'cyan',
number: 'yellow',
bigint: 'yellow',
boolean: 'yellow',
undefined: 'grey',
null: 'bold',
string: 'green',
symbol: 'green',
date: 'magenta',
// "name": intentionally not styling
'regexp': 'red'
regexp: 'red'
});

function addQuotes(str, quotes) {
Expand DownExpand Up@@ -356,14 +356,10 @@ function getPrefix(constructor, tag, fallback) {
return `[${fallback}: null prototype] `;
}

if (constructor !== '') {
if (tag !== '' && constructor !== tag) {
return `${constructor} [${tag}] `;
}
return `${constructor} `;
if (tag !== '' && constructor !== tag) {
return `${constructor} [${tag}] `;
}

return '';
return `${constructor} `;
}

const getBoxedValue = formatPrimitive.bind(null, stylizeNoColor);
Expand DownExpand Up@@ -405,11 +401,10 @@ function getCtxStyle(constructor, tag) {
}

function formatProxy(ctx, proxy, recurseTimes) {
if (recurseTimes != null) {
if (recurseTimes < 0)
return ctx.stylize('Proxy [Array]', 'special');
recurseTimes -= 1;
if (recurseTimes > ctx.depth && ctx.depth !== null) {
return ctx.stylize('Proxy [Array]', 'special');
}
recurseTimes += 1;
ctx.indentationLvl += 2;
const res = [
formatValue(ctx, proxy[0], recurseTimes),
Expand DownExpand Up@@ -524,7 +519,10 @@ function formatValue(ctx, value, recurseTimes) {
maybeCustom !== inspect &&
// Also filter out any prototype objects using the circular check.
!(value.constructor && value.constructor.prototype === value)) {
const ret = maybeCustom.call(value, recurseTimes, ctx);
// This makes sure the recurseTimes are reported as before while using
// a counter internally.
const depth = ctx.depth === null ? null : ctx.depth - recurseTimes;
const ret = maybeCustom.call(value, depth, ctx);

// If the custom inspection method returned `this`, don't go into
// infinite recursion.
Expand DownExpand Up@@ -557,7 +555,6 @@ function formatRaw(ctx, value, recurseTimes) {
let braces;
let noIterator = true;
let i = 0;
let skip = false;
const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE;

let extrasType = kObjectType;
Expand DownExpand Up@@ -615,16 +612,12 @@ function formatRaw(ctx, value, recurseTimes) {
braces = ['{', '}'];
if (constructor === 'Object') {
if (isArgumentsObject(value)) {
if (keys.length === 0)
return '[Arguments] {}';
braces[0] = '[Arguments] {';
} else if (tag !== '') {
braces[0] = `${getPrefix(constructor, tag, 'Object')}{`;
if (keys.length === 0) {
return `${braces[0]}}`;
}
} else if (keys.length === 0) {
return '{}';
}
if (keys.length === 0) {
return `${braces[0]}}`;
}
} else if (typeof value === 'function') {
const type = constructor || tag || 'Function';
Expand All@@ -638,7 +631,7 @@ function formatRaw(ctx, value, recurseTimes) {
const prefix = getPrefix(constructor, tag, 'RegExp');
if (prefix !== 'RegExp ')
base = `${prefix}${base}`;
if (keys.length === 0 || recurseTimes < 0)
if (keys.length === 0 || recurseTimes > ctx.depth && ctx.depth !== null)
return ctx.stylize(base, 'regexp');
} else if (isDate(value)) {
// Make dates with properties first say the date
Expand DownExpand Up@@ -696,7 +689,6 @@ function formatRaw(ctx, value, recurseTimes) {
} else if (isModuleNamespaceObject(value)) {
braces[0] = `[${tag}] {`;
formatter = formatNamespaceObject;
skip = true;
} else if (isBoxedPrimitive(value)) {
let type;
if (isNumberObject(value)) {
Expand DownExpand Up@@ -746,22 +738,19 @@ function formatRaw(ctx, value, recurseTimes) {
}
}

if (recurseTimes != null) {
if (recurseTimes < 0)
return ctx.stylize(`[${getCtxStyle(constructor, tag)}]`, 'special');
recurseTimes -= 1;
if (recurseTimes > ctx.depth && ctx.depth !== null) {
return ctx.stylize(`[${getCtxStyle(constructor, tag)}]`, 'special');
}
recurseTimes += 1;

ctx.seen.push(value);
let output;
const indentationLvl = ctx.indentationLvl;
try {
output = formatter(ctx, value, recurseTimes, keys);
if (skip === false) {
for (i = 0; i < keys.length; i++) {
output.push(
formatProperty(ctx, value, recurseTimes, keys[i], extrasType));
}
for (i = 0; i < keys.length; i++) {
output.push(
formatProperty(ctx, value, recurseTimes, keys[i], extrasType));
}
} catch (err) {
return handleMaxCallStackSize(ctx, err, constructor, tag, indentationLvl);
Expand DownExpand Up@@ -810,9 +799,7 @@ function handleMaxCallStackSize(ctx, err, constructor, tag, indentationLvl) {

function formatNumber(fn, value) {
// Format -0 as '-0'. Checking `value === -0` won't distinguish 0 from -0.
if (Object.is(value, -0))
return fn('-0', 'number');
return fn(`${value}`, 'number');
return fn(Object.is(value, -0) ? '-0' : `${value}`, 'number');
}

function formatBigInt(fn, value) {
Expand All@@ -824,12 +811,11 @@ function formatPrimitive(fn, value, ctx) {
if (ctx.compact === false &&
ctx.indentationLvl + value.length > ctx.breakLength &&
value.length > kMinLineLength) {
// eslint-disable-next-line max-len
const minLineLength = Math.max(ctx.breakLength - ctx.indentationLvl, kMinLineLength);
// eslint-disable-next-line max-len
const averageLineLength = Math.ceil(value.length / Math.ceil(value.length / minLineLength));
const rawMaxLineLength = ctx.breakLength - ctx.indentationLvl;
const maxLineLength = Math.max(rawMaxLineLength, kMinLineLength);
const lines = Math.ceil(value.length / maxLineLength);
const averageLineLength = Math.ceil(value.length / lines);
const divisor = Math.max(averageLineLength, kMinLineLength);
let res = '';
if (readableRegExps[divisor] === undefined) {
// Build a new RegExp that naturally breaks text into multiple lines.
//
Expand All@@ -845,7 +831,7 @@ function formatPrimitive(fn, value, ctx) {
const matches = value.match(readableRegExps[divisor]);
if (matches.length > 1) {
const indent = ' '.repeat(ctx.indentationLvl);
res += `${fn(strEscape(matches[0]), 'string')} +\n`;
let res = `${fn(strEscape(matches[0]), 'string')} +\n`;
for (var i = 1; i < matches.length - 1; i++) {
res += `${indent} ${fn(strEscape(matches[i]), 'string')} +\n`;
}
Expand DownExpand Up@@ -873,9 +859,8 @@ function formatError(value) {
}

function formatNamespaceObject(ctx, value, recurseTimes, keys) {
const len = keys.length;
const output = new Array(len);
for (var i = 0; i < len; i++) {
const output = new Array(keys.length);
for (var i = 0; i < keys.length; i++) {
try {
output[i] = formatProperty(ctx, value, recurseTimes, keys[i],
kObjectType);
Expand All@@ -895,6 +880,8 @@ function formatNamespaceObject(ctx, value, recurseTimes, keys) {
ctx.stylize('<uninitialized>', 'special');
}
}
// Reset the keys to an empty array. This prevents duplicated inspection.
keys.length = 0;
return output;
}

Expand DownExpand Up@@ -1104,8 +1091,6 @@ function formatPromise(ctx, value, recurseTimes) {
if (state === kPending) {
output = [ctx.stylize('<pending>', 'special')];
} else {
// Using `formatValue` is correct here without the need to fix the
// indentation level.
ctx.indentationLvl += 2;
const str = formatValue(ctx, result, recurseTimes);
ctx.indentationLvl -= 2;
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-util-inspect.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -1791,7 +1791,6 @@ assert.strictEqual(
util.inspect(new StorageObject()),
'<[Object: null prototype] {}> {}'
);

}

// Check that the fallback always works.
Expand Down