Skip to content

Commit e79bdc3

Browse files
aduh95danielleadams
authored andcommitted
assert: refactor to use more primordials
PR-URL: #36234 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 2c7358e commit e79bdc3

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

lib/assert.js

+37-22
Original file line numberDiff line numberDiff line change
@@ -21,17 +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,
3445
StringPrototypeSlice,
46+
StringPrototypeSplit,
3547
StringPrototypeStartsWith,
3648
} = primordials;
3749

@@ -54,7 +66,7 @@ const { EOL } = require('internal/constants');
5466
const { NativeModule } = require('internal/bootstrap/loaders');
5567
const { isError } = require('internal/util');
5668

57-
const errorCache = new Map();
69+
const errorCache = new SafeMap();
5870
const CallTracker = require('internal/assert/calltracker');
5971

6072
let isDeepEqual;
@@ -83,7 +95,7 @@ const meta = [
8395
'\\u001e', '\\u001f'
8496
];
8597

86-
const escapeFn = (str) => meta[str.charCodeAt(0)];
98+
const escapeFn = (str) => meta[StringPrototypeCharCodeAt(str, 0)];
8799

88100
let warned = false;
89101

@@ -234,7 +246,7 @@ function parseCode(code, offset) {
234246
classFields,
235247
staticClassFeatures
236248
);
237-
parseExpressionAt = Parser.parseExpressionAt.bind(Parser);
249+
parseExpressionAt = FunctionPrototypeBind(Parser.parseExpressionAt, Parser);
238250
}
239251
let node;
240252
let start = 0;
@@ -259,8 +271,9 @@ function parseCode(code, offset) {
259271

260272
return [
261273
node.node.start,
262-
code.slice(node.node.start, node.node.end)
263-
.replace(escapeSequencesRegExp, escapeFn)
274+
StringPrototypeReplace(StringPrototypeSlice(code,
275+
node.node.start, node.node.end),
276+
escapeSequencesRegExp, escapeFn)
264277
];
265278
}
266279

@@ -324,23 +337,24 @@ function getErrMessage(message, fn) {
324337
decoder.end();
325338
} else {
326339
for (let i = 0; i < line; i++) {
327-
code = code.slice(code.indexOf('\n') + 1);
340+
code = StringPrototypeSlice(code,
341+
StringPrototypeIndexOf(code, '\n') + 1);
328342
}
329343
[column, message] = parseCode(code, column);
330344
}
331345
// Always normalize indentation, otherwise the message could look weird.
332-
if (message.includes('\n')) {
346+
if (StringPrototypeIncludes(message, '\n')) {
333347
if (EOL === '\r\n') {
334-
message = message.replace(/\r\n/g, '\n');
348+
message = StringPrototypeReplace(message, /\r\n/g, '\n');
335349
}
336-
const frames = message.split('\n');
337-
message = frames.shift();
350+
const frames = StringPrototypeSplit(message, '\n');
351+
message = ArrayPrototypeShift(frames);
338352
for (const frame of frames) {
339353
let pos = 0;
340354
while (pos < column && (frame[pos] === ' ' || frame[pos] === '\t')) {
341355
pos++;
342356
}
343-
message += `\n ${frame.slice(pos)}`;
357+
message += `\n ${StringPrototypeSlice(frame, pos)}`;
344358
}
345359
}
346360
message = `The expression evaluated to a falsy value:\n\n ${message}\n`;
@@ -606,7 +620,7 @@ function expectedException(actual, expected, message, fn) {
606620
// Special handle errors to make sure the name and the message are
607621
// compared as well.
608622
if (expected instanceof Error) {
609-
keys.push('name', 'message');
623+
ArrayPrototypePush(keys, 'name', 'message');
610624
} else if (keys.length === 0) {
611625
throw new ERR_INVALID_ARG_VALUE('error',
612626
expected, 'may not be an empty object');
@@ -649,7 +663,7 @@ function expectedException(actual, expected, message, fn) {
649663
throwError = true;
650664
} else {
651665
// Check validation functions return value.
652-
const res = expected.call({}, actual);
666+
const res = ReflectApply(expected, {}, [actual]);
653667
if (res !== true) {
654668
if (!message) {
655669
generatedMessage = true;
@@ -794,7 +808,7 @@ function hasMatchingError(actual, expected) {
794808
if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
795809
return false;
796810
}
797-
return expected.call({}, actual) === true;
811+
return ReflectApply(expected, {}, [actual]) === true;
798812
}
799813

800814
function expectsNoError(stackStartFn, actual, error, message) {
@@ -866,20 +880,21 @@ assert.ifError = function ifError(err) {
866880
// This will remove any duplicated frames from the error frames taken
867881
// from within `ifError` and add the original error frames to the newly
868882
// created ones.
869-
const tmp2 = origStack.split('\n');
870-
tmp2.shift();
883+
const tmp2 = StringPrototypeSplit(origStack, '\n');
884+
ArrayPrototypeShift(tmp2);
871885
// Filter all frames existing in err.stack.
872-
let tmp1 = newErr.stack.split('\n');
886+
let tmp1 = StringPrototypeSplit(newErr.stack, '\n');
873887
for (const errFrame of tmp2) {
874888
// Find the first occurrence of the frame.
875-
const pos = tmp1.indexOf(errFrame);
889+
const pos = ArrayPrototypeIndexOf(tmp1, errFrame);
876890
if (pos !== -1) {
877891
// Only keep new frames.
878-
tmp1 = tmp1.slice(0, pos);
892+
tmp1 = ArrayPrototypeSlice(tmp1, 0, pos);
879893
break;
880894
}
881895
}
882-
newErr.stack = `${tmp1.join('\n')}\n${tmp2.join('\n')}`;
896+
newErr.stack =
897+
`${ArrayPrototypeJoin(tmp1, '\n')}\n${ArrayPrototypeJoin(tmp2, '\n')}`;
883898
}
884899

885900
throw newErr;

0 commit comments

Comments
 (0)