@@ -35,16 +35,18 @@ const {
35
35
ObjectKeys,
36
36
ObjectPrototypeIsPrototypeOf,
37
37
ReflectApply,
38
- RegExpPrototypeTest ,
38
+ RegExpPrototypeExec ,
39
39
SafeMap,
40
40
String,
41
41
StringPrototypeCharCodeAt,
42
42
StringPrototypeIncludes,
43
43
StringPrototypeIndexOf,
44
- StringPrototypeReplace,
45
44
StringPrototypeSlice,
46
45
StringPrototypeSplit,
47
46
StringPrototypeStartsWith,
47
+ SafeRegExpPrototypeSymbolReplace,
48
+ SafeRegExpPrototypeTest,
49
+ makeSafeStringObject,
48
50
} = primordials ;
49
51
50
52
const { Buffer } = require ( 'buffer' ) ;
@@ -283,9 +285,11 @@ function parseCode(code, offset) {
283
285
284
286
return [
285
287
node . node . start ,
286
- StringPrototypeReplace ( StringPrototypeSlice ( code ,
287
- node . node . start , node . node . end ) ,
288
- escapeSequencesRegExp , escapeFn ) ,
288
+ SafeRegExpPrototypeSymbolReplace (
289
+ escapeSequencesRegExp ,
290
+ StringPrototypeSlice ( code , node . node . start , node . node . end ) ,
291
+ escapeFn
292
+ ) ,
289
293
] ;
290
294
}
291
295
@@ -357,17 +361,21 @@ function getErrMessage(message, fn) {
357
361
}
358
362
// Always normalize indentation, otherwise the message could look weird.
359
363
if ( StringPrototypeIncludes ( message , '\n' ) ) {
360
- if ( EOL === '\r\n' ) {
361
- message = StringPrototypeReplace ( message , / \r \n / g, '\n' ) ;
362
- }
363
- const frames = StringPrototypeSplit ( message , '\n' ) ;
364
- message = ArrayPrototypeShift ( frames ) ;
365
- for ( const frame of frames ) {
366
- let pos = 0 ;
367
- while ( pos < column && ( frame [ pos ] === ' ' || frame [ pos ] === '\t' ) ) {
364
+ const splitter = EOL === '\r\n' ? / \r \n / g : / \n / g;
365
+ const splitterLength = EOL === '\r\n' ? 2 : 1 ;
366
+ const str = message ;
367
+ let previousIndex = RegExpPrototypeExec ( splitter , str ) . index ;
368
+ message = StringPrototypeSlice ( message , 0 , previousIndex ) ;
369
+ while ( previousIndex != null ) {
370
+ const lineEndIndex = RegExpPrototypeExec ( splitter , str ) ?. index ;
371
+ previousIndex += splitterLength ;
372
+ let pos = previousIndex ;
373
+ while ( pos < previousIndex + column &&
374
+ ( str [ pos ] === ' ' || str [ pos ] === '\t' ) ) {
368
375
pos ++ ;
369
376
}
370
- message += `\n ${ StringPrototypeSlice ( frame , pos ) } ` ;
377
+ message += `\n ${ StringPrototypeSlice ( str , pos , lineEndIndex ) } ` ;
378
+ previousIndex = lineEndIndex ;
371
379
}
372
380
}
373
381
message = `The expression evaluated to a falsy value:\n\n ${ message } \n` ;
@@ -619,7 +627,7 @@ class Comparison {
619
627
if ( actual !== undefined &&
620
628
typeof actual [ key ] === 'string' &&
621
629
isRegExp ( obj [ key ] ) &&
622
- RegExpPrototypeTest ( obj [ key ] , actual [ key ] ) ) {
630
+ SafeRegExpPrototypeTest ( obj [ key ] , actual [ key ] ) ) {
623
631
this [ key ] = actual [ key ] ;
624
632
} else {
625
633
this [ key ] = obj [ key ] ;
@@ -665,7 +673,7 @@ function expectedException(actual, expected, message, fn) {
665
673
// Handle regular expressions.
666
674
if ( isRegExp ( expected ) ) {
667
675
const str = String ( actual ) ;
668
- if ( RegExpPrototypeTest ( expected , str ) )
676
+ if ( SafeRegExpPrototypeTest ( expected , str ) )
669
677
return ;
670
678
671
679
if ( ! message ) {
@@ -700,7 +708,7 @@ function expectedException(actual, expected, message, fn) {
700
708
for ( const key of keys ) {
701
709
if ( typeof actual [ key ] === 'string' &&
702
710
isRegExp ( expected [ key ] ) &&
703
- RegExpPrototypeTest ( expected [ key ] , actual [ key ] ) ) {
711
+ SafeRegExpPrototypeTest ( expected [ key ] , actual [ key ] ) ) {
704
712
continue ;
705
713
}
706
714
compareExceptionKey ( actual , expected , key , message , keys , fn ) ;
@@ -864,7 +872,7 @@ function hasMatchingError(actual, expected) {
864
872
if ( typeof expected !== 'function' ) {
865
873
if ( isRegExp ( expected ) ) {
866
874
const str = String ( actual ) ;
867
- return RegExpPrototypeTest ( expected , str ) ;
875
+ return SafeRegExpPrototypeTest ( expected , str ) ;
868
876
}
869
877
throw new ERR_INVALID_ARG_TYPE (
870
878
'expected' , [ 'Function' , 'RegExp' ] , expected
@@ -978,10 +986,11 @@ assert.ifError = function ifError(err) {
978
986
// This will remove any duplicated frames from the error frames taken
979
987
// from within `ifError` and add the original error frames to the newly
980
988
// created ones.
981
- const tmp2 = StringPrototypeSplit ( origStack , '\n' ) ;
989
+ const LINE_RETURN = makeSafeStringObject ( '\n' ) ;
990
+ const tmp2 = StringPrototypeSplit ( origStack , LINE_RETURN ) ;
982
991
ArrayPrototypeShift ( tmp2 ) ;
983
992
// Filter all frames existing in err.stack.
984
- let tmp1 = StringPrototypeSplit ( newErr . stack , '\n' ) ;
993
+ let tmp1 = StringPrototypeSplit ( newErr . stack , LINE_RETURN ) ;
985
994
for ( const errFrame of tmp2 ) {
986
995
// Find the first occurrence of the frame.
987
996
const pos = ArrayPrototypeIndexOf ( tmp1 , errFrame ) ;
@@ -1007,7 +1016,7 @@ function internalMatch(string, regexp, message, fn) {
1007
1016
}
1008
1017
const match = fn . name === 'match' ;
1009
1018
if ( typeof string !== 'string' ||
1010
- RegExpPrototypeTest ( regexp , string ) !== match ) {
1019
+ SafeRegExpPrototypeTest ( regexp , string ) !== match ) {
1011
1020
if ( message instanceof Error ) {
1012
1021
throw message ;
1013
1022
}
0 commit comments