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,
34
45
StringPrototypeSlice,
46
+ StringPrototypeSplit,
35
47
StringPrototypeStartsWith,
36
48
} = primordials ;
37
49
@@ -54,7 +66,7 @@ const { EOL } = require('internal/constants');
54
66
const { NativeModule } = require ( 'internal/bootstrap/loaders' ) ;
55
67
const { isError } = require ( 'internal/util' ) ;
56
68
57
- const errorCache = new Map ( ) ;
69
+ const errorCache = new SafeMap ( ) ;
58
70
const CallTracker = require ( 'internal/assert/calltracker' ) ;
59
71
60
72
let isDeepEqual ;
@@ -83,7 +95,7 @@ const meta = [
83
95
'\\u001e' , '\\u001f'
84
96
] ;
85
97
86
- const escapeFn = ( str ) => meta [ str . charCodeAt ( 0 ) ] ;
98
+ const escapeFn = ( str ) => meta [ StringPrototypeCharCodeAt ( str , 0 ) ] ;
87
99
88
100
let warned = false ;
89
101
@@ -234,7 +246,7 @@ function parseCode(code, offset) {
234
246
classFields ,
235
247
staticClassFeatures
236
248
) ;
237
- parseExpressionAt = Parser . parseExpressionAt . bind ( Parser ) ;
249
+ parseExpressionAt = FunctionPrototypeBind ( Parser . parseExpressionAt , Parser ) ;
238
250
}
239
251
let node ;
240
252
let start = 0 ;
@@ -259,8 +271,9 @@ function parseCode(code, offset) {
259
271
260
272
return [
261
273
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 )
264
277
] ;
265
278
}
266
279
@@ -324,23 +337,24 @@ function getErrMessage(message, fn) {
324
337
decoder . end ( ) ;
325
338
} else {
326
339
for ( let i = 0 ; i < line ; i ++ ) {
327
- code = code . slice ( code . indexOf ( '\n' ) + 1 ) ;
340
+ code = StringPrototypeSlice ( code ,
341
+ StringPrototypeIndexOf ( code , '\n' ) + 1 ) ;
328
342
}
329
343
[ column , message ] = parseCode ( code , column ) ;
330
344
}
331
345
// Always normalize indentation, otherwise the message could look weird.
332
- if ( message . includes ( '\n' ) ) {
346
+ if ( StringPrototypeIncludes ( message , '\n' ) ) {
333
347
if ( EOL === '\r\n' ) {
334
- message = message . replace ( / \r \n / g, '\n' ) ;
348
+ message = StringPrototypeReplace ( message , / \r \n / g, '\n' ) ;
335
349
}
336
- const frames = message . split ( '\n' ) ;
337
- message = frames . shift ( ) ;
350
+ const frames = StringPrototypeSplit ( message , '\n' ) ;
351
+ message = ArrayPrototypeShift ( frames ) ;
338
352
for ( const frame of frames ) {
339
353
let pos = 0 ;
340
354
while ( pos < column && ( frame [ pos ] === ' ' || frame [ pos ] === '\t' ) ) {
341
355
pos ++ ;
342
356
}
343
- message += `\n ${ frame . slice ( pos ) } ` ;
357
+ message += `\n ${ StringPrototypeSlice ( frame , pos ) } ` ;
344
358
}
345
359
}
346
360
message = `The expression evaluated to a falsy value:\n\n ${ message } \n` ;
@@ -606,7 +620,7 @@ function expectedException(actual, expected, message, fn) {
606
620
// Special handle errors to make sure the name and the message are
607
621
// compared as well.
608
622
if ( expected instanceof Error ) {
609
- keys . push ( 'name' , 'message' ) ;
623
+ ArrayPrototypePush ( keys , 'name' , 'message' ) ;
610
624
} else if ( keys . length === 0 ) {
611
625
throw new ERR_INVALID_ARG_VALUE ( 'error' ,
612
626
expected , 'may not be an empty object' ) ;
@@ -649,7 +663,7 @@ function expectedException(actual, expected, message, fn) {
649
663
throwError = true ;
650
664
} else {
651
665
// Check validation functions return value.
652
- const res = expected . call ( { } , actual ) ;
666
+ const res = ReflectApply ( expected , { } , [ actual ] ) ;
653
667
if ( res !== true ) {
654
668
if ( ! message ) {
655
669
generatedMessage = true ;
@@ -794,7 +808,7 @@ function hasMatchingError(actual, expected) {
794
808
if ( ObjectPrototypeIsPrototypeOf ( Error , expected ) ) {
795
809
return false ;
796
810
}
797
- return expected . call ( { } , actual ) === true ;
811
+ return ReflectApply ( expected , { } , [ actual ] ) === true ;
798
812
}
799
813
800
814
function expectsNoError ( stackStartFn , actual , error , message ) {
@@ -866,20 +880,21 @@ assert.ifError = function ifError(err) {
866
880
// This will remove any duplicated frames from the error frames taken
867
881
// from within `ifError` and add the original error frames to the newly
868
882
// created ones.
869
- const tmp2 = origStack . split ( '\n' ) ;
870
- tmp2 . shift ( ) ;
883
+ const tmp2 = StringPrototypeSplit ( origStack , '\n' ) ;
884
+ ArrayPrototypeShift ( tmp2 ) ;
871
885
// Filter all frames existing in err.stack.
872
- let tmp1 = newErr . stack . split ( '\n' ) ;
886
+ let tmp1 = StringPrototypeSplit ( newErr . stack , '\n' ) ;
873
887
for ( const errFrame of tmp2 ) {
874
888
// Find the first occurrence of the frame.
875
- const pos = tmp1 . indexOf ( errFrame ) ;
889
+ const pos = ArrayPrototypeIndexOf ( tmp1 , errFrame ) ;
876
890
if ( pos !== - 1 ) {
877
891
// Only keep new frames.
878
- tmp1 = tmp1 . slice ( 0 , pos ) ;
892
+ tmp1 = ArrayPrototypeSlice ( tmp1 , 0 , pos ) ;
879
893
break ;
880
894
}
881
895
}
882
- newErr . stack = `${ tmp1 . join ( '\n' ) } \n${ tmp2 . join ( '\n' ) } ` ;
896
+ newErr . stack =
897
+ `${ ArrayPrototypeJoin ( tmp1 , '\n' ) } \n${ ArrayPrototypeJoin ( tmp2 , '\n' ) } ` ;
883
898
}
884
899
885
900
throw newErr ;
0 commit comments