assert: refactor to avoid unsafe array iteration

PR-URL: https://github.com/nodejs/node/pull/37344
Reviewed-By: Darshan Sen <raisinten@gmail.com>
This commit is contained in:
Antoine du Hamel authored and Michaël Zasso committed 2021-06-11 09:30:00 +02:00
1 parent f5541ddea3
commit cfff3b4462
1 file changed
+16 -10
+16 -10
View File
@@ -313,6 +313,17 @@ function createErrDiff(actual, expected, operator) {
return `${msg}${skipped ? skippedMsg : ''}\n${res}${other}${end}${indicator}`;
}
function addEllipsis(string) {
const lines = StringPrototypeSplit(string, '\n', 11);
if (lines.length > 10) {
lines.length = 10;
return `${ArrayPrototypeJoin(lines, '\n')}\n...`;
} else if (string.length > 512) {
return `${StringPrototypeSlice(string, 512)}...`;
}
return string;
}
class AssertionError extends Error {
constructor(options) {
if (typeof options !== 'object' || options === null) {
@@ -469,16 +480,11 @@ class AssertionError extends Error {
const tmpActual = this.actual;
const tmpExpected = this.expected;
for (const name of ['actual', 'expected']) {
if (typeof this[name] === 'string') {
const lines = StringPrototypeSplit(this[name], '\n');
if (lines.length > 10) {
lines.length = 10;
this[name] = `${ArrayPrototypeJoin(lines, '\n')}\n...`;
} else if (this[name].length > 512) {
this[name] = `${StringPrototypeSlice(this[name], 512)}...`;
}
}
if (typeof this.actual === 'string') {
this.actual = addEllipsis(this.actual);
}
if (typeof this.expected === 'string') {
this.expected = addEllipsis(this.expected);
}
// This limits the `actual` and `expected` property default inspection to