diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2019-06-02 16:07:08 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2019-06-11 13:50:55 +0200 |
commit | 26de13a15c3d20b484ac830c8f4aa38d69651b2a (patch) | |
tree | a87fb965f0907fd70623de8d966e1e86cef909cc /lib/internal/util | |
parent | 62ac84b5f81a747e8f7693138949d841c1b98851 (diff) | |
download | android-node-v8-26de13a15c3d20b484ac830c8f4aa38d69651b2a.tar.gz android-node-v8-26de13a15c3d20b484ac830c8f4aa38d69651b2a.tar.bz2 android-node-v8-26de13a15c3d20b484ac830c8f4aa38d69651b2a.zip |
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: https://github.com/nodejs/node/pull/28055
Fixes: https://github.com/nodejs/node/issues/27690
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'lib/internal/util')
-rw-r--r-- | lib/internal/util/inspect.js | 39 |
1 files changed, 6 insertions, 33 deletions
diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 1292293cc6..e9dc8d83ea 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -127,8 +127,6 @@ const numberRegExp = /^(0|[1-9][0-9]*)$/; const coreModuleRegExp = /^ at (?:[^/\\(]+ \(|)((?<![/\\]).+)\.js:\d+:\d+\)?$/; const nodeModulesRegExp = /[/\\]node_modules[/\\](.+?)(?=[/\\])/g; -const readableRegExps = {}; - const kMinLineLength = 16; // Constants to map the iterator state. @@ -1080,37 +1078,12 @@ function formatBigInt(fn, value) { function formatPrimitive(fn, value, ctx) { if (typeof value === 'string') { if (ctx.compact !== true && - ctx.indentationLvl + value.length + 4 > ctx.breakLength && - value.length > kMinLineLength) { - // Subtract the potential quotes, the space and the plus as well (4). - const rawMaxLineLength = ctx.breakLength - ctx.indentationLvl - 4; - 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); - if (readableRegExps[divisor] === undefined) { - // Build a new RegExp that naturally breaks text into multiple lines. - // - // Rules - // 1. Greedy match all text up the max line length that ends with a - // whitespace or the end of the string. - // 2. If none matches, non-greedy match any text up to a whitespace or - // the end of the string. - // - // eslint-disable-next-line max-len, node-core/no-unescaped-regexp-dot - readableRegExps[divisor] = new RegExp(`(.|\\n){1,${divisor}}(\\s|$)|(\\n|.)+?(\\s|$)`, 'gm'); - } - const matches = value.match(readableRegExps[divisor]); - if (matches.length > 1) { - const indent = ' '.repeat(ctx.indentationLvl); - let res = `${fn(strEscape(matches[0]), 'string')} +\n`; - const lastIndex = matches.length - 1; - for (let i = 1; i < lastIndex; i++) { - res += `${indent} ${fn(strEscape(matches[i]), 'string')} +\n`; - } - res += `${indent} ${fn(strEscape(matches[lastIndex]), 'string')}`; - return res; - } + value.length > kMinLineLength && + value.length > ctx.breakLength - ctx.indentationLvl - 4) { + return value + .split(/(?<=\n)/) + .map((line) => fn(strEscape(line), 'string')) + .join(` +\n${' '.repeat(ctx.indentationLvl + 2)}`); } return fn(strEscape(value), 'string'); } |