1
1
'use strict' ;
2
2
3
3
const {
4
+ ArrayPrototypeFilter,
5
+ ArrayPrototypeIncludes,
6
+ ArrayPrototypeMap,
7
+ Boolean,
8
+ FunctionPrototypeBind,
4
9
MathMin,
10
+ RegExpPrototypeTest,
11
+ SafeSet,
5
12
SafeStringIterator,
6
- Set,
13
+ StringPrototypeEndsWith,
14
+ StringPrototypeIndexOf,
15
+ StringPrototypeLastIndexOf,
16
+ StringPrototypeReplace,
17
+ StringPrototypeSlice,
18
+ StringPrototypeStartsWith,
19
+ StringPrototypeToLowerCase,
20
+ StringPrototypeTrim,
7
21
Symbol,
8
22
} = primordials ;
9
23
@@ -60,7 +74,9 @@ function isRecoverableError(e, code) {
60
74
// curly brace with parenthesis. Note: only the open parenthesis is added
61
75
// here as the point is to test for potentially valid but incomplete
62
76
// expressions.
63
- if ( / ^ \s * \{ / . test ( code ) && isRecoverableError ( e , `(${ code } ` ) ) return true ;
77
+ if ( RegExpPrototypeTest ( / ^ \s * \{ / , code ) &&
78
+ isRecoverableError ( e , `(${ code } ` ) )
79
+ return true ;
64
80
65
81
let recoverable = false ;
66
82
@@ -100,9 +116,11 @@ function isRecoverableError(e, code) {
100
116
break ;
101
117
102
118
case 'Unterminated string constant' :
103
- const token = this . input . slice ( this . lastTokStart , this . pos ) ;
119
+ const token = StringPrototypeSlice ( this . input ,
120
+ this . lastTokStart , this . pos ) ;
104
121
// See https://www.ecma-international.org/ecma-262/#sec-line-terminators
105
- if ( / \\ (?: \r \n ? | \n | \u2028 | \u2029 ) $ / . test ( token ) ) {
122
+ if ( RegExpPrototypeTest ( / \\ (?: \r \n ? | \n | \u2028 | \u2029 ) $ / ,
123
+ token ) ) {
106
124
recoverable = true ;
107
125
}
108
126
}
@@ -236,15 +254,15 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
236
254
hasCompletions = true ;
237
255
238
256
// If there is a common prefix to all matches, then apply that portion.
239
- const completions = rawCompletions . filter ( ( e ) => e ) ;
257
+ const completions = ArrayPrototypeFilter ( rawCompletions , Boolean ) ;
240
258
const prefix = commonPrefix ( completions ) ;
241
259
242
260
// No common prefix found.
243
261
if ( prefix . length <= completeOn . length ) {
244
262
return ;
245
263
}
246
264
247
- const suffix = prefix . slice ( completeOn . length ) ;
265
+ const suffix = StringPrototypeSlice ( prefix , completeOn . length ) ;
248
266
249
267
if ( insertPreview ) {
250
268
repl . _insertString ( suffix ) ;
@@ -272,16 +290,22 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
272
290
}
273
291
274
292
function isInStrictMode ( repl ) {
275
- return repl . replMode === REPL_MODE_STRICT || process . execArgv
276
- . map ( ( e ) => e . toLowerCase ( ) . replace ( / _ / g, '-' ) )
277
- . includes ( '--use-strict' ) ;
293
+ return repl . replMode === REPL_MODE_STRICT || ArrayPrototypeIncludes (
294
+ ArrayPrototypeMap ( process . execArgv ,
295
+ ( e ) => StringPrototypeReplace (
296
+ StringPrototypeToLowerCase ( e ) ,
297
+ / _ / g,
298
+ '-'
299
+ ) ) ,
300
+ '--use-strict' ) ;
278
301
}
279
302
280
303
// This returns a code preview for arbitrary input code.
281
304
function getInputPreview ( input , callback ) {
282
305
// For similar reasons as `defaultEval`, wrap expressions starting with a
283
306
// curly brace with parenthesis.
284
- if ( input . startsWith ( '{' ) && ! input . endsWith ( ';' ) && ! wrapped ) {
307
+ if ( StringPrototypeStartsWith ( input , '{' ) &&
308
+ ! StringPrototypeEndsWith ( input , ';' ) && ! wrapped ) {
285
309
input = `(${ input } )` ;
286
310
wrapped = true ;
287
311
}
@@ -347,7 +371,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
347
371
return ;
348
372
}
349
373
350
- const line = repl . line . trim ( ) ;
374
+ const line = StringPrototypeTrim ( repl . line ) ;
351
375
352
376
// Do not preview in case the line only contains whitespace.
353
377
if ( line === '' ) {
@@ -413,9 +437,9 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
413
437
414
438
// Line breaks are very rare and probably only occur in case of error
415
439
// messages with line breaks.
416
- const lineBreakPos = inspected . indexOf ( '\n' ) ;
440
+ const lineBreakPos = StringPrototypeIndexOf ( inspected , '\n' ) ;
417
441
if ( lineBreakPos !== - 1 ) {
418
- inspected = `${ inspected . slice ( 0 , lineBreakPos ) } ` ;
442
+ inspected = `${ StringPrototypeSlice ( inspected , 0 , lineBreakPos ) } ` ;
419
443
}
420
444
421
445
const result = repl . useColors ?
@@ -453,7 +477,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
453
477
// Refresh prints the whole screen again and the preview will be removed
454
478
// during that procedure. Print the preview again. This also makes sure
455
479
// the preview is always correct after resizing the terminal window.
456
- const originalRefresh = repl . _refreshLine . bind ( repl ) ;
480
+ const originalRefresh = FunctionPrototypeBind ( repl . _refreshLine , repl ) ;
457
481
repl . _refreshLine = ( ) => {
458
482
inputPreview = null ;
459
483
originalRefresh ( ) ;
@@ -463,7 +487,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
463
487
let insertCompletionPreview = true ;
464
488
// Insert the longest common suffix of the current input in case the user
465
489
// moves to the right while already being at the current input end.
466
- const originalMoveCursor = repl . _moveCursor . bind ( repl ) ;
490
+ const originalMoveCursor = FunctionPrototypeBind ( repl . _moveCursor , repl ) ;
467
491
repl . _moveCursor = ( dx ) => {
468
492
const currentCursor = repl . cursor ;
469
493
originalMoveCursor ( dx ) ;
@@ -477,7 +501,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
477
501
478
502
// This is the only function that interferes with the completion insertion.
479
503
// Monkey patch it to prevent inserting the completion when it shouldn't be.
480
- const originalClearLine = repl . clearLine . bind ( repl ) ;
504
+ const originalClearLine = FunctionPrototypeBind ( repl . clearLine , repl ) ;
481
505
repl . clearLine = ( ) => {
482
506
insertCompletionPreview = false ;
483
507
originalClearLine ( ) ;
@@ -493,7 +517,7 @@ function setupReverseSearch(repl) {
493
517
return { reverseSearch ( ) { return false ; } } ;
494
518
}
495
519
496
- const alreadyMatched = new Set ( ) ;
520
+ const alreadyMatched = new SafeSet ( ) ;
497
521
const labels = {
498
522
r : 'bck-i-search: ' ,
499
523
s : 'fwd-i-search: '
@@ -557,18 +581,18 @@ function setupReverseSearch(repl) {
557
581
if ( cursor === - 1 ) {
558
582
cursor = entry . length ;
559
583
}
560
- cursor = entry . lastIndexOf ( input , cursor - 1 ) ;
584
+ cursor = StringPrototypeLastIndexOf ( entry , input , cursor - 1 ) ;
561
585
} else {
562
- cursor = entry . indexOf ( input , cursor + 1 ) ;
586
+ cursor = StringPrototypeIndexOf ( entry , input , cursor + 1 ) ;
563
587
}
564
588
// Match not found.
565
589
if ( cursor === - 1 ) {
566
590
goToNextHistoryIndex ( ) ;
567
591
// Match found.
568
592
} else {
569
593
if ( repl . useColors ) {
570
- const start = entry . slice ( 0 , cursor ) ;
571
- const end = entry . slice ( cursor + input . length ) ;
594
+ const start = StringPrototypeSlice ( entry , 0 , cursor ) ;
595
+ const end = StringPrototypeSlice ( entry , cursor + input . length ) ;
572
596
entry = `${ start } \x1B[4m${ input } \x1B[24m${ end } ` ;
573
597
}
574
598
print ( entry , `${ labels [ dir ] } ${ input } _` , cursor ) ;
@@ -611,7 +635,7 @@ function setupReverseSearch(repl) {
611
635
// tick end instead of after each operation.
612
636
let rows = 0 ;
613
637
if ( lastMatch !== - 1 ) {
614
- const line = repl . history [ lastMatch ] . slice ( 0 , lastCursor ) ;
638
+ const line = StringPrototypeSlice ( repl . history [ lastMatch ] , 0 , lastCursor ) ;
615
639
rows = repl . _getDisplayPos ( `${ repl . getPrompt ( ) } ${ line } ` ) . rows ;
616
640
cursorTo ( repl . output , promptPos . cols ) ;
617
641
} else if ( isInReverseSearch && repl . line !== '' ) {
@@ -633,7 +657,7 @@ function setupReverseSearch(repl) {
633
657
// To know exactly how many rows we have to move the cursor back we need the
634
658
// cursor rows, the output rows and the input rows.
635
659
const prompt = repl . getPrompt ( ) ;
636
- const cursorLine = ` ${ prompt } ${ outputLine . slice ( 0 , cursor ) } ` ;
660
+ const cursorLine = prompt + StringPrototypeSlice ( outputLine , 0 , cursor ) ;
637
661
const cursorPos = repl . _getDisplayPos ( cursorLine ) ;
638
662
const outputPos = repl . _getDisplayPos ( `${ prompt } ${ outputLine } ` ) ;
639
663
const inputPos = repl . _getDisplayPos ( inputLine ) ;
@@ -691,7 +715,7 @@ function setupReverseSearch(repl) {
691
715
search ( ) ;
692
716
} else if ( key . name === 'backspace' ||
693
717
( key . ctrl && ( key . name === 'h' || key . name === 'w' ) ) ) {
694
- reset ( input . slice ( 0 , input . length - 1 ) ) ;
718
+ reset ( StringPrototypeSlice ( input , 0 , input . length - 1 ) ) ;
695
719
search ( ) ;
696
720
// Special handle <ctrl> + c and escape. Those should only cancel the
697
721
// reverse search. The original line is visible afterwards again.
0 commit comments