Skip to content

Commit 9ced334

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 56b150b commit 9ced334

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.
@@ -1055,37 +1053,12 @@ function formatBigInt(fn, value) {
10551053
function formatPrimitive(fn, value, ctx) {
10561054
if (typeof value === 'string') {
10571055
if (ctx.compact !== true &&
1058-
ctx.indentationLvl + value.length + 4 > ctx.breakLength &&
1059-
value.length > kMinLineLength) {
1060-
// Subtract the potential quotes, the space and the plus as well (4).
1061-
const rawMaxLineLength = ctx.breakLength - ctx.indentationLvl - 4;
1062-
const maxLineLength = Math.max(rawMaxLineLength, kMinLineLength);
1063-
const lines = Math.ceil(value.length / maxLineLength);
1064-
const averageLineLength = Math.ceil(value.length / lines);
1065-
const divisor = Math.max(averageLineLength, kMinLineLength);
1066-
if (readableRegExps[divisor] === undefined) {
1067-
// Build a new RegExp that naturally breaks text into multiple lines.
1068-
//
1069-
// Rules
1070-
// 1. Greedy match all text up the max line length that ends with a
1071-
// whitespace or the end of the string.
1072-
// 2. If none matches, non-greedy match any text up to a whitespace or
1073-
// the end of the string.
1074-
//
1075-
// eslint-disable-next-line max-len, node-core/no-unescaped-regexp-dot
1076-
readableRegExps[divisor] = new RegExp(`(.|\\n){1,${divisor}}(\\s|$)|(\\n|.)+?(\\s|$)`, 'gm');
1077-
}
1078-
const matches = value.match(readableRegExps[divisor]);
1079-
if (matches.length > 1) {
1080-
const indent = ' '.repeat(ctx.indentationLvl);
1081-
let res = `${fn(strEscape(matches[0]), 'string')} +\n`;
1082-
const lastIndex = matches.length - 1;
1083-
for (let i = 1; i < lastIndex; i++) {
1084-
res += `${indent} ${fn(strEscape(matches[i]), 'string')} +\n`;
1085-
}
1086-
res += `${indent} ${fn(strEscape(matches[lastIndex]), 'string')}`;
1087-
return res;
1088-
}
1056+
value.length > kMinLineLength &&
1057+
value.length > ctx.breakLength - ctx.indentationLvl - 4) {
1058+
return value
1059+
.split(/(?<=\n)/)
1060+
.map((line) => fn(strEscape(line), 'string'))
1061+
.join(` +\n${' '.repeat(ctx.indentationLvl + 2)}`);
10891062
}
10901063
return fn(strEscape(value), 'string');
10911064
}

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 {}');
@@ -1487,10 +1487,9 @@ util.inspect(process);
14871487
' 2,',
14881488
' [',
14891489
' [',
1490-
" 'Lorem ipsum dolor\\nsit amet,\\tconsectetur ' +",
1491-
" 'adipiscing elit, sed do eiusmod tempor ' +",
1492-
" 'incididunt ut labore et dolore magna ' +",
1493-
" 'aliqua.',",
1490+
" 'Lorem ipsum dolor\\n' +",
1491+
" 'sit amet,\\tconsectetur adipiscing elit, sed do eiusmod " +
1492+
"tempor incididunt ut labore et dolore magna aliqua.',",
14941493
" 'test',",
14951494
" 'foo'",
14961495
' ]',
@@ -1507,12 +1506,9 @@ util.inspect(process);
15071506

15081507
out = util.inspect(o.a[2][0][0], { compact: false, breakLength: 30 });
15091508
expect = [
1510-
"'Lorem ipsum dolor\\nsit ' +",
1511-
" 'amet,\\tconsectetur ' +",
1512-
" 'adipiscing elit, sed do ' +",
1513-
" 'eiusmod tempor incididunt ' +",
1514-
" 'ut labore et dolore magna ' +",
1515-
" 'aliqua.'"
1509+
"'Lorem ipsum dolor\\n' +",
1510+
" 'sit amet,\\tconsectetur adipiscing elit, sed do eiusmod tempor " +
1511+
"incididunt ut labore et dolore magna aliqua.'"
15161512
].join('\n');
15171513
assert.strictEqual(out, expect);
15181514

@@ -1526,30 +1522,7 @@ util.inspect(process);
15261522
'12 45 78 01 34 67 90 23 56 89 123456789012345678901234567890',
15271523
{ compact: false, breakLength: 3 });
15281524
expect = [
1529-
"'12 45 78 01 34 ' +",
1530-
" '67 90 23 56 89 ' +",
1531-
" '123456789012345678901234567890'"
1532-
].join('\n');
1533-
assert.strictEqual(out, expect);
1534-
1535-
out = util.inspect(
1536-
'12 45 78 01 34 67 90 23 56 89 1234567890123 0',
1537-
{ compact: false, breakLength: 3 });
1538-
expect = [
1539-
"'12 45 78 01 34 ' +",
1540-
" '67 90 23 56 89 ' +",
1541-
" '1234567890123 0'"
1542-
].join('\n');
1543-
assert.strictEqual(out, expect);
1544-
1545-
out = util.inspect(
1546-
'12 45 78 01 34 67 90 23 56 89 12345678901234567 0',
1547-
{ compact: false, breakLength: 3 });
1548-
expect = [
1549-
"'12 45 78 01 34 ' +",
1550-
" '67 90 23 56 89 ' +",
1551-
" '12345678901234567 ' +",
1552-
" '0'"
1525+
"'12 45 78 01 34 67 90 23 56 89 123456789012345678901234567890'"
15531526
].join('\n');
15541527
assert.strictEqual(out, expect);
15551528

@@ -1588,7 +1561,7 @@ util.inspect(process);
15881561

15891562
o[util.inspect.custom] = () => ({ a: '12 45 78 01 34 67 90 23' });
15901563
out = util.inspect(o, { compact: false, breakLength: 3 });
1591-
expect = "{\n a: '12 45 78 01 34 ' +\n '67 90 23'\n}";
1564+
expect = "{\n a: '12 45 78 01 34 67 90 23'\n}";
15921565
assert.strictEqual(out, expect);
15931566
}
15941567

@@ -2192,16 +2165,13 @@ assert.strictEqual(
21922165
' b: {',
21932166
' x: 5,',
21942167
' c: {',
2195-
" x: '10000000000000000 00000000000000000 ' +",
2196-
" '10000000000000000 00000000000000000 ' +",
2197-
" '10000000000000000 00000000000000000 ' +",
2198-
" '10000000000000000 00000000000000000 ' +",
2199-
" '10000000000000000 00000000000000000 ' +",
2200-
" '10000000000000000 00000000000000000 ' +",
2201-
" '10000000000000000 00000000000000000 ' +",
2202-
" '10000000000000000 00000000000000000 ' +",
2203-
" '10000000000000000 00000000000000000 ' +",
2204-
" '10000000000000000 00000000000000000 ',",
2168+
" x: '10000000000000000 00000000000000000 10000000000000000 " +
2169+
'00000000000000000 10000000000000000 00000000000000000 ' +
2170+
'10000000000000000 00000000000000000 10000000000000000 ' +
2171+
'00000000000000000 10000000000000000 00000000000000000 ' +
2172+
'10000000000000000 00000000000000000 10000000000000000 ' +
2173+
'00000000000000000 10000000000000000 00000000000000000 ' +
2174+
"10000000000000000 00000000000000000 ',",
22052175
' d: 2,',
22062176
' e: 3',
22072177
' }',

0 commit comments

Comments
 (0)