Skip to content
Closed
Changes from 1 commit
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
Next Next commit
assert: refactor to avoid unsafe array iteration
  • Loading branch information
aduh95 committed Jan 3, 2021
commit 92517c145ef84632232baac77aaf613f8f8ec98e
44 changes: 26 additions & 18 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@

const {
ArrayPrototypeIndexOf,
ArrayPrototypeForEach,
ArrayPrototypeJoin,
ArrayPrototypePush,
ArrayPrototypeShift,
ArrayPrototypeSlice,
ArrayPrototypeSome,
ArrayPrototypeUnshift,
Error,
ErrorCaptureStackTrace,
FunctionPrototypeBind,
Expand Down Expand Up @@ -403,7 +406,8 @@ function innerOk(fn, argLen, value, message) {
// Pure assertion tests whether a value is truthy, as determined
// by !!value.
function ok(...args) {
innerOk(ok, args.length, ...args);
ArrayPrototypeUnshift(args, ok, args.length);
ReflectApply(innerOk, null, args);
}
assert.ok = ok;

Expand Down Expand Up @@ -543,7 +547,7 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) {

class Comparison {
constructor(obj, keys, actual) {
for (const key of keys) {
ArrayPrototypeForEach(keys, (key) => {
if (key in obj) {
if (actual !== undefined &&
typeof actual[key] === 'string' &&
Expand All @@ -554,7 +558,7 @@ class Comparison {
this[key] = obj[key];
}
}
}
});
}
}

Expand Down Expand Up @@ -626,14 +630,13 @@ function expectedException(actual, expected, message, fn) {
expected, 'may not be an empty object');
}
if (isDeepEqual === undefined) lazyLoadComparison();
for (const key of keys) {
if (typeof actual[key] === 'string' &&
isRegExp(expected[key]) &&
RegExpPrototypeTest(expected[key], actual[key])) {
continue;
ArrayPrototypeForEach(keys, (key) => {
if (typeof actual[key] !== 'string' ||
!isRegExp(expected[key]) ||
!RegExpPrototypeTest(expected[key], actual[key])) {
compareExceptionKey(actual, expected, key, message, keys, fn);
}
compareExceptionKey(actual, expected, key, message, keys, fn);
}
});
return;
}
// Guard instanceof against arrow functions as they don't have a prototype.
Expand Down Expand Up @@ -837,19 +840,23 @@ function expectsNoError(stackStartFn, actual, error, message) {
}

assert.throws = function throws(promiseFn, ...args) {
expectsError(throws, getActual(promiseFn), ...args);
ArrayPrototypeUnshift(args, throws, getActual(promiseFn));
ReflectApply(expectsError, null, args);
};

assert.rejects = async function rejects(promiseFn, ...args) {
expectsError(rejects, await waitForActual(promiseFn), ...args);
ArrayPrototypeUnshift(args, rejects, await waitForActual(promiseFn));
ReflectApply(expectsError, null, args);
};

assert.doesNotThrow = function doesNotThrow(fn, ...args) {
expectsNoError(doesNotThrow, getActual(fn), ...args);
ArrayPrototypeUnshift(args, doesNotThrow, getActual(fn));
ReflectApply(expectsNoError, null, args);
};

assert.doesNotReject = async function doesNotReject(fn, ...args) {
expectsNoError(doesNotReject, await waitForActual(fn), ...args);
ArrayPrototypeUnshift(args, doesNotReject, await waitForActual(fn));
ReflectApply(expectsNoError, null, args);
};

assert.ifError = function ifError(err) {
Expand Down Expand Up @@ -884,15 +891,15 @@ assert.ifError = function ifError(err) {
ArrayPrototypeShift(tmp2);
// Filter all frames existing in err.stack.
let tmp1 = StringPrototypeSplit(newErr.stack, '\n');
for (const errFrame of tmp2) {
ArrayPrototypeSome(tmp2, (errFrame) => {
// Find the first occurrence of the frame.
const pos = ArrayPrototypeIndexOf(tmp1, errFrame);
if (pos !== -1) {
// Only keep new frames.
tmp1 = ArrayPrototypeSlice(tmp1, 0, pos);
break;
return true;
}
}
});
newErr.stack =
`${ArrayPrototypeJoin(tmp1, '\n')}\n${ArrayPrototypeJoin(tmp2, '\n')}`;
}
Expand Down Expand Up @@ -948,7 +955,8 @@ assert.CallTracker = CallTracker;

// Expose a strict only variant of assert
function strict(...args) {
innerOk(strict, args.length, ...args);
ArrayPrototypeUnshift(args, strict, args.length);
ReflectApply(innerOk, null, args);
}
assert.strict = ObjectAssign(strict, assert, {
equal: assert.strictEqual,
Expand Down