Skip to content

Commit 26de13a

Browse files
committed
util: refactor inspecting long lines
Using the `util.inspect` `compact` mode set to something else than `true` resulted in breaking long lines in case the line would exceed the `breakLength` option and if it contained whitespace and or new lines. It turned out that this behavior was less useful than originally expected and it is now changed to only break on line breaks if the `breakLength` option is exceeded for the inspected string. This should be align better with the user expectation than the former behavior. PR-URL: #28055 Fixes: #27690 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 62ac84b commit 26de13a

File tree

2 files changed

+22
-79
lines changed

2 files changed

+22
-79
lines changed

lib/internal/util/inspect.js

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ const numberRegExp = /^(0|[1-9][0-9]*)$/;
127127
const coreModuleRegExp = /^ at (?:[^/\\(]+ \(|)((?<![/\\]).+)\.js:\d+:\d+\)?$/;
128128
const nodeModulesRegExp = /[/\\]node_modules[/\\](.+?)(?=[/\\])/g;
129129

130-
const readableRegExps = {};
131-
132130
const kMinLineLength = 16;
133131

134132
// Constants to map the iterator state.
@@ -1080,37 +1078,12 @@ function formatBigInt(fn, value) {
10801078
function formatPrimitive(fn, value, ctx) {
10811079
if (typeof value === 'string') {
10821080
if (ctx.compact !== true &&
1083-
ctx.indentationLvl + value.length + 4 > ctx.breakLength &&
1084-
value.length > kMinLineLength) {
1085-
// Subtract the potential quotes, the space and the plus as well (4).
1086-
const rawMaxLineLength = ctx.breakLength - ctx.indentationLvl - 4;
1087-
const maxLineLength = Math.max(rawMaxLineLength, kMinLineLength);
1088-
const lines = Math.ceil(value.length / maxLineLength);
1089-
const averageLineLength = Math.ceil(value.length / lines);
1090-
const divisor = Math.max(averageLineLength, kMinLineLength);
1091-
if (readableRegExps[divisor] === undefined) {
1092-
// Build a new RegExp that naturally breaks text into multiple lines.
1093-
//
1094-
// Rules
1095-
// 1. Greedy match all text up the max line length that ends with a
1096-
// whitespace or the end of the string.
1097-
// 2. If none matches, non-greedy match any text up to a whitespace or
1098-
// the end of the string.
1099-
//
1100-
// eslint-disable-next-line max-len, node-core/no-unescaped-regexp-dot
1101-
readableRegExps[divisor] = new RegExp(`(.|\\n){1,${divisor}}(\\s|$)|(\\n|.)+?(\\s|$)`, 'gm');
1102-
}
1103-
const matches = value.match(readableRegExps[divisor]);
1104-
if (matches.length > 1) {
1105-
const indent = ' '.repeat(ctx.indentationLvl);
1106-
let res = `${fn(strEscape(matches[0]), 'string')} +\n`;
1107-
const lastIndex = matches.length - 1;
1108-
for (let i = 1; i < lastIndex; i++) {
1109-
res += `${indent} ${fn(strEscape(matches[i]), 'string')} +\n`;
1110-
}
1111-
res += `${indent} ${fn(strEscape(matches[lastIndex]), 'string')}`;
1112-
return res;
1113-
}
1081+
value.length > kMinLineLength &&
1082+
value.length > ctx.breakLength - ctx.indentationLvl - 4) {
1083+
return value
1084+
.split(/(?<=\n)/)
1085+
.map((line) => fn(strEscape(line), 'string'))
1086+
.join(` +\n${' '.repeat(ctx.indentationLvl + 2)}`);
11141087
}
11151088
return fn(strEscape(value), 'string');
11161089
}

test/parallel/test-util-inspect.js

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ assert.strictEqual(util.inspect(new Date('')), (new Date('')).toString());
9393
assert.strictEqual(util.inspect('\n\u0001'), "'\\n\\u0001'");
9494
assert.strictEqual(
9595
util.inspect(`${Array(75).fill(1)}'\n\u001d\n\u0003`),
96-
`"${Array(75).fill(1)}'\\n" +\n '\\u001d\\n\\u0003'`
96+
`"${Array(75).fill(1)}'\\n" +\n '\\u001d\\n' +\n '\\u0003'`
9797
);
9898
assert.strictEqual(util.inspect([]), '[]');
9999
assert.strictEqual(util.inspect(Object.create([])), 'Array {}');
@@ -1509,10 +1509,9 @@ util.inspect(process);
15091509
' 2,',
15101510
' [',
15111511
' [',
1512-
" 'Lorem ipsum dolor\\nsit amet,\\tconsectetur ' +",
1513-
" 'adipiscing elit, sed do eiusmod tempor ' +",
1514-
" 'incididunt ut labore et dolore magna ' +",
1515-
" 'aliqua.',",
1512+
" 'Lorem ipsum dolor\\n' +",
1513+
" 'sit amet,\\tconsectetur adipiscing elit, sed do eiusmod " +
1514+
"tempor incididunt ut labore et dolore magna aliqua.',",
15161515
" 'test',",
15171516
" 'foo'",
15181517
' ]',
@@ -1529,12 +1528,9 @@ util.inspect(process);
15291528

15301529
out = util.inspect(o.a[2][0][0], { compact: false, breakLength: 30 });
15311530
expect = [
1532-
"'Lorem ipsum dolor\\nsit ' +",
1533-
" 'amet,\\tconsectetur ' +",
1534-
" 'adipiscing elit, sed do ' +",
1535-
" 'eiusmod tempor incididunt ' +",
1536-
" 'ut labore et dolore magna ' +",
1537-
" 'aliqua.'"
1531+
"'Lorem ipsum dolor\\n' +",
1532+
" 'sit amet,\\tconsectetur adipiscing elit, sed do eiusmod tempor " +
1533+
"incididunt ut labore et dolore magna aliqua.'"
15381534
].join('\n');
15391535
assert.strictEqual(out, expect);
15401536

@@ -1548,30 +1544,7 @@ util.inspect(process);
15481544
'12 45 78 01 34 67 90 23 56 89 123456789012345678901234567890',
15491545
{ compact: false, breakLength: 3 });
15501546
expect = [
1551-
"'12 45 78 01 34 ' +",
1552-
" '67 90 23 56 89 ' +",
1553-
" '123456789012345678901234567890'"
1554-
].join('\n');
1555-
assert.strictEqual(out, expect);
1556-
1557-
out = util.inspect(
1558-
'12 45 78 01 34 67 90 23 56 89 1234567890123 0',
1559-
{ compact: false, breakLength: 3 });
1560-
expect = [
1561-
"'12 45 78 01 34 ' +",
1562-
" '67 90 23 56 89 ' +",
1563-
" '1234567890123 0'"
1564-
].join('\n');
1565-
assert.strictEqual(out, expect);
1566-
1567-
out = util.inspect(
1568-
'12 45 78 01 34 67 90 23 56 89 12345678901234567 0',
1569-
{ compact: false, breakLength: 3 });
1570-
expect = [
1571-
"'12 45 78 01 34 ' +",
1572-
" '67 90 23 56 89 ' +",
1573-
" '12345678901234567 ' +",
1574-
" '0'"
1547+
"'12 45 78 01 34 67 90 23 56 89 123456789012345678901234567890'"
15751548
].join('\n');
15761549
assert.strictEqual(out, expect);
15771550

@@ -1610,7 +1583,7 @@ util.inspect(process);
16101583

16111584
o[util.inspect.custom] = () => ({ a: '12 45 78 01 34 67 90 23' });
16121585
out = util.inspect(o, { compact: false, breakLength: 3 });
1613-
expect = "{\n a: '12 45 78 01 34 ' +\n '67 90 23'\n}";
1586+
expect = "{\n a: '12 45 78 01 34 67 90 23'\n}";
16141587
assert.strictEqual(out, expect);
16151588
}
16161589

@@ -2214,16 +2187,13 @@ assert.strictEqual(
22142187
' b: {',
22152188
' x: 5,',
22162189
' c: {',
2217-
" x: '10000000000000000 00000000000000000 ' +",
2218-
" '10000000000000000 00000000000000000 ' +",
2219-
" '10000000000000000 00000000000000000 ' +",
2220-
" '10000000000000000 00000000000000000 ' +",
2221-
" '10000000000000000 00000000000000000 ' +",
2222-
" '10000000000000000 00000000000000000 ' +",
2223-
" '10000000000000000 00000000000000000 ' +",
2224-
" '10000000000000000 00000000000000000 ' +",
2225-
" '10000000000000000 00000000000000000 ' +",
2226-
" '10000000000000000 00000000000000000 ',",
2190+
" x: '10000000000000000 00000000000000000 10000000000000000 " +
2191+
'00000000000000000 10000000000000000 00000000000000000 ' +
2192+
'10000000000000000 00000000000000000 10000000000000000 ' +
2193+
'00000000000000000 10000000000000000 00000000000000000 ' +
2194+
'10000000000000000 00000000000000000 10000000000000000 ' +
2195+
'00000000000000000 10000000000000000 00000000000000000 ' +
2196+
"10000000000000000 00000000000000000 ',",
22272197
' d: 2,',
22282198
' e: 3',
22292199
' }',

0 commit comments

Comments
 (0)