Skip to content

Commit 87fa636

Browse files
aduh95targos
authored andcommitted
assert: refactor to use more primordials
PR-URL: #36234 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 9f67c08 commit 87fa636

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

lib/assert.js

+38-22
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,29 @@
2121
'use strict';
2222

2323
const {
24+
ArrayPrototypeIndexOf,
25+
ArrayPrototypeJoin,
26+
ArrayPrototypePush,
27+
ArrayPrototypeShift,
28+
ArrayPrototypeSlice,
2429
Error,
2530
ErrorCaptureStackTrace,
31+
FunctionPrototypeBind,
32+
NumberIsNaN,
2633
ObjectAssign,
2734
ObjectIs,
2835
ObjectKeys,
2936
ObjectPrototypeIsPrototypeOf,
30-
Map,
31-
NumberIsNaN,
37+
ReflectApply,
3238
RegExpPrototypeTest,
39+
SafeMap,
3340
String,
41+
StringPrototypeCharCodeAt,
42+
StringPrototypeIncludes,
43+
StringPrototypeIndexOf,
44+
StringPrototypeReplace,
45+
StringPrototypeSlice,
46+
StringPrototypeSplit,
3447
} = primordials;
3548

3649
const { Buffer } = require('buffer');
@@ -52,7 +65,7 @@ const { EOL } = require('internal/constants');
5265
const { NativeModule } = require('internal/bootstrap/loaders');
5366
const { isError } = require('internal/util');
5467

55-
const errorCache = new Map();
68+
const errorCache = new SafeMap();
5669
const CallTracker = require('internal/assert/calltracker');
5770

5871
let isDeepEqual;
@@ -81,7 +94,7 @@ const meta = [
8194
'\\u001e', '\\u001f'
8295
];
8396

84-
const escapeFn = (str) => meta[str.charCodeAt(0)];
97+
const escapeFn = (str) => meta[StringPrototypeCharCodeAt(str, 0)];
8598

8699
let warned = false;
87100

@@ -240,7 +253,7 @@ function parseCode(code, offset) {
240253
classFields,
241254
staticClassFeatures
242255
);
243-
parseExpressionAt = Parser.parseExpressionAt.bind(Parser);
256+
parseExpressionAt = FunctionPrototypeBind(Parser.parseExpressionAt, Parser);
244257
}
245258
let node;
246259
let start = 0;
@@ -265,8 +278,9 @@ function parseCode(code, offset) {
265278

266279
return [
267280
node.node.start,
268-
code.slice(node.node.start, node.node.end)
269-
.replace(escapeSequencesRegExp, escapeFn)
281+
StringPrototypeReplace(StringPrototypeSlice(code,
282+
node.node.start, node.node.end),
283+
escapeSequencesRegExp, escapeFn)
270284
];
271285
}
272286

@@ -330,23 +344,24 @@ function getErrMessage(message, fn) {
330344
decoder.end();
331345
} else {
332346
for (let i = 0; i < line; i++) {
333-
code = code.slice(code.indexOf('\n') + 1);
347+
code = StringPrototypeSlice(code,
348+
StringPrototypeIndexOf(code, '\n') + 1);
334349
}
335350
[column, message] = parseCode(code, column);
336351
}
337352
// Always normalize indentation, otherwise the message could look weird.
338-
if (message.includes('\n')) {
353+
if (StringPrototypeIncludes(message, '\n')) {
339354
if (EOL === '\r\n') {
340-
message = message.replace(/\r\n/g, '\n');
355+
message = StringPrototypeReplace(message, /\r\n/g, '\n');
341356
}
342-
const frames = message.split('\n');
343-
message = frames.shift();
357+
const frames = StringPrototypeSplit(message, '\n');
358+
message = ArrayPrototypeShift(frames);
344359
for (const frame of frames) {
345360
let pos = 0;
346361
while (pos < column && (frame[pos] === ' ' || frame[pos] === '\t')) {
347362
pos++;
348363
}
349-
message += `\n ${frame.slice(pos)}`;
364+
message += `\n ${StringPrototypeSlice(frame, pos)}`;
350365
}
351366
}
352367
message = `The expression evaluated to a falsy value:\n\n ${message}\n`;
@@ -670,7 +685,7 @@ function expectedException(actual, expected, message, fn) {
670685
// Special handle errors to make sure the name and the message are
671686
// compared as well.
672687
if (expected instanceof Error) {
673-
keys.push('name', 'message');
688+
ArrayPrototypePush(keys, 'name', 'message');
674689
} else if (keys.length === 0) {
675690
throw new ERR_INVALID_ARG_VALUE('error',
676691
expected, 'may not be an empty object');
@@ -713,7 +728,7 @@ function expectedException(actual, expected, message, fn) {
713728
throwError = true;
714729
} else {
715730
// Check validation functions return value.
716-
const res = expected.call({}, actual);
731+
const res = ReflectApply(expected, {}, [actual]);
717732
if (res !== true) {
718733
if (!message) {
719734
generatedMessage = true;
@@ -858,7 +873,7 @@ function hasMatchingError(actual, expected) {
858873
if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
859874
return false;
860875
}
861-
return expected.call({}, actual) === true;
876+
return ReflectApply(expected, {}, [actual]) === true;
862877
}
863878

864879
function expectsNoError(stackStartFn, actual, error, message) {
@@ -959,20 +974,21 @@ assert.ifError = function ifError(err) {
959974
// This will remove any duplicated frames from the error frames taken
960975
// from within `ifError` and add the original error frames to the newly
961976
// created ones.
962-
const tmp2 = origStack.split('\n');
963-
tmp2.shift();
977+
const tmp2 = StringPrototypeSplit(origStack, '\n');
978+
ArrayPrototypeShift(tmp2);
964979
// Filter all frames existing in err.stack.
965-
let tmp1 = newErr.stack.split('\n');
980+
let tmp1 = StringPrototypeSplit(newErr.stack, '\n');
966981
for (const errFrame of tmp2) {
967982
// Find the first occurrence of the frame.
968-
const pos = tmp1.indexOf(errFrame);
983+
const pos = ArrayPrototypeIndexOf(tmp1, errFrame);
969984
if (pos !== -1) {
970985
// Only keep new frames.
971-
tmp1 = tmp1.slice(0, pos);
986+
tmp1 = ArrayPrototypeSlice(tmp1, 0, pos);
972987
break;
973988
}
974989
}
975-
newErr.stack = `${tmp1.join('\n')}\n${tmp2.join('\n')}`;
990+
newErr.stack =
991+
`${ArrayPrototypeJoin(tmp1, '\n')}\n${ArrayPrototypeJoin(tmp2, '\n')}`;
976992
}
977993

978994
throw newErr;

0 commit comments

Comments
 (0)