Skip to content

Commit 87fa636

Browse files
aduh95targos
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 9f67c08 commit 87fa636

1 file changed

Lines changed: 38 additions & 22 deletions

File tree

‎lib/assert.js‎

Lines changed: 38 additions & 22 deletions
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-
consterrorCache=newMap();
68+
consterrorCache=newSafeMap();
5669
constCallTracker=require('internal/assert/calltracker');
5770

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

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

8699
letwarned=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
letnode;
246259
letstart=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(leti=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-
constframes=message.split('\n');
343-
message=frames.shift();
357+
constframes=StringPrototypeSplit(message,'\n');
358+
message=ArrayPrototypeShift(frames);
344359
for(constframeofframes){
345360
letpos=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(expectedinstanceofError){
673-
keys.push('name','message');
688+
ArrayPrototypePush(keys,'name','message');
674689
}elseif(keys.length===0){
675690
thrownewERR_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-
constres=expected.call({},actual);
731+
constres=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
returnfalse;
860875
}
861-
returnexpected.call({},actual)===true;
876+
returnReflectApply(expected,{},[actual])===true;
862877
}
863878

864879
functionexpectsNoError(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-
consttmp2=origStack.split('\n');
963-
tmp2.shift();
977+
consttmp2=StringPrototypeSplit(origStack,'\n');
978+
ArrayPrototypeShift(tmp2);
964979
// Filter all frames existing in err.stack.
965-
lettmp1=newErr.stack.split('\n');
980+
lettmp1=StringPrototypeSplit(newErr.stack,'\n');
966981
for(consterrFrameoftmp2){
967982
// Find the first occurrence of the frame.
968-
constpos=tmp1.indexOf(errFrame);
983+
constpos=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
thrownewErr;

0 commit comments

Comments
 (0)