21
21
'use strict' ;
22
22
23
23
const {
24
+ ArrayPrototypeIndexOf,
25
+ ArrayPrototypeJoin,
26
+ ArrayPrototypePush,
27
+ ArrayPrototypeShift,
28
+ ArrayPrototypeSlice,
24
29
Error,
25
30
ErrorCaptureStackTrace,
31
+ FunctionPrototypeBind,
32
+ NumberIsNaN,
26
33
ObjectAssign,
27
34
ObjectIs,
28
35
ObjectKeys,
29
36
ObjectPrototypeIsPrototypeOf,
30
- Map,
31
- NumberIsNaN,
37
+ ReflectApply,
32
38
RegExpPrototypeTest,
39
+ SafeMap,
33
40
String,
41
+ StringPrototypeCharCodeAt,
42
+ StringPrototypeIncludes,
43
+ StringPrototypeIndexOf,
44
+ StringPrototypeReplace,
45
+ StringPrototypeSlice,
46
+ StringPrototypeSplit,
34
47
} = primordials ;
35
48
36
49
const { Buffer } = require ( 'buffer' ) ;
@@ -52,7 +65,7 @@ const { EOL } = require('internal/constants');
52
65
const { NativeModule } = require ( 'internal/bootstrap/loaders' ) ;
53
66
const { isError } = require ( 'internal/util' ) ;
54
67
55
- const errorCache = new Map ( ) ;
68
+ const errorCache = new SafeMap ( ) ;
56
69
const CallTracker = require ( 'internal/assert/calltracker' ) ;
57
70
58
71
let isDeepEqual ;
@@ -81,7 +94,7 @@ const meta = [
81
94
'\\u001e' , '\\u001f'
82
95
] ;
83
96
84
- const escapeFn = ( str ) => meta [ str . charCodeAt ( 0 ) ] ;
97
+ const escapeFn = ( str ) => meta [ StringPrototypeCharCodeAt ( str , 0 ) ] ;
85
98
86
99
let warned = false ;
87
100
@@ -240,7 +253,7 @@ function parseCode(code, offset) {
240
253
classFields ,
241
254
staticClassFeatures
242
255
) ;
243
- parseExpressionAt = Parser . parseExpressionAt . bind ( Parser ) ;
256
+ parseExpressionAt = FunctionPrototypeBind ( Parser . parseExpressionAt , Parser ) ;
244
257
}
245
258
let node ;
246
259
let start = 0 ;
@@ -265,8 +278,9 @@ function parseCode(code, offset) {
265
278
266
279
return [
267
280
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 )
270
284
] ;
271
285
}
272
286
@@ -330,23 +344,24 @@ function getErrMessage(message, fn) {
330
344
decoder . end ( ) ;
331
345
} else {
332
346
for ( let i = 0 ; i < line ; i ++ ) {
333
- code = code . slice ( code . indexOf ( '\n' ) + 1 ) ;
347
+ code = StringPrototypeSlice ( code ,
348
+ StringPrototypeIndexOf ( code , '\n' ) + 1 ) ;
334
349
}
335
350
[ column , message ] = parseCode ( code , column ) ;
336
351
}
337
352
// Always normalize indentation, otherwise the message could look weird.
338
- if ( message . includes ( '\n' ) ) {
353
+ if ( StringPrototypeIncludes ( message , '\n' ) ) {
339
354
if ( EOL === '\r\n' ) {
340
- message = message . replace ( / \r \n / g, '\n' ) ;
355
+ message = StringPrototypeReplace ( message , / \r \n / g, '\n' ) ;
341
356
}
342
- const frames = message . split ( '\n' ) ;
343
- message = frames . shift ( ) ;
357
+ const frames = StringPrototypeSplit ( message , '\n' ) ;
358
+ message = ArrayPrototypeShift ( frames ) ;
344
359
for ( const frame of frames ) {
345
360
let pos = 0 ;
346
361
while ( pos < column && ( frame [ pos ] === ' ' || frame [ pos ] === '\t' ) ) {
347
362
pos ++ ;
348
363
}
349
- message += `\n ${ frame . slice ( pos ) } ` ;
364
+ message += `\n ${ StringPrototypeSlice ( frame , pos ) } ` ;
350
365
}
351
366
}
352
367
message = `The expression evaluated to a falsy value:\n\n ${ message } \n` ;
@@ -670,7 +685,7 @@ function expectedException(actual, expected, message, fn) {
670
685
// Special handle errors to make sure the name and the message are
671
686
// compared as well.
672
687
if ( expected instanceof Error ) {
673
- keys . push ( 'name' , 'message' ) ;
688
+ ArrayPrototypePush ( keys , 'name' , 'message' ) ;
674
689
} else if ( keys . length === 0 ) {
675
690
throw new ERR_INVALID_ARG_VALUE ( 'error' ,
676
691
expected , 'may not be an empty object' ) ;
@@ -713,7 +728,7 @@ function expectedException(actual, expected, message, fn) {
713
728
throwError = true ;
714
729
} else {
715
730
// Check validation functions return value.
716
- const res = expected . call ( { } , actual ) ;
731
+ const res = ReflectApply ( expected , { } , [ actual ] ) ;
717
732
if ( res !== true ) {
718
733
if ( ! message ) {
719
734
generatedMessage = true ;
@@ -858,7 +873,7 @@ function hasMatchingError(actual, expected) {
858
873
if ( ObjectPrototypeIsPrototypeOf ( Error , expected ) ) {
859
874
return false ;
860
875
}
861
- return expected . call ( { } , actual ) === true ;
876
+ return ReflectApply ( expected , { } , [ actual ] ) === true ;
862
877
}
863
878
864
879
function expectsNoError ( stackStartFn , actual , error , message ) {
@@ -959,20 +974,21 @@ assert.ifError = function ifError(err) {
959
974
// This will remove any duplicated frames from the error frames taken
960
975
// from within `ifError` and add the original error frames to the newly
961
976
// created ones.
962
- const tmp2 = origStack . split ( '\n' ) ;
963
- tmp2 . shift ( ) ;
977
+ const tmp2 = StringPrototypeSplit ( origStack , '\n' ) ;
978
+ ArrayPrototypeShift ( tmp2 ) ;
964
979
// Filter all frames existing in err.stack.
965
- let tmp1 = newErr . stack . split ( '\n' ) ;
980
+ let tmp1 = StringPrototypeSplit ( newErr . stack , '\n' ) ;
966
981
for ( const errFrame of tmp2 ) {
967
982
// Find the first occurrence of the frame.
968
- const pos = tmp1 . indexOf ( errFrame ) ;
983
+ const pos = ArrayPrototypeIndexOf ( tmp1 , errFrame ) ;
969
984
if ( pos !== - 1 ) {
970
985
// Only keep new frames.
971
- tmp1 = tmp1 . slice ( 0 , pos ) ;
986
+ tmp1 = ArrayPrototypeSlice ( tmp1 , 0 , pos ) ;
972
987
break ;
973
988
}
974
989
}
975
- newErr . stack = `${ tmp1 . join ( '\n' ) } \n${ tmp2 . join ( '\n' ) } ` ;
990
+ newErr . stack =
991
+ `${ ArrayPrototypeJoin ( tmp1 , '\n' ) } \n${ ArrayPrototypeJoin ( tmp2 , '\n' ) } ` ;
976
992
}
977
993
978
994
throw newErr ;
0 commit comments