@@ -238,7 +238,33 @@ function REPLServer(prompt,
238
238
239
239
eval_ = eval_ || defaultEval ;
240
240
241
+ function preprocess ( code ) {
242
+ let cmd = code ;
243
+ if ( / ^ \s * \{ / . test ( cmd ) && / \} \s * $ / . test ( cmd ) ) {
244
+ // It's confusing for `{ a : 1 }` to be interpreted as a block
245
+ // statement rather than an object literal. So, we first try
246
+ // to wrap it in parentheses, so that it will be interpreted as
247
+ // an expression.
248
+ cmd = `(${ cmd } )` ;
249
+ self . wrappedCmd = true ;
250
+ } else {
251
+ // Mitigate https://github.com/nodejs/node/issues/548
252
+ cmd = cmd . replace (
253
+ / ^ \s * f u n c t i o n (?: \s * ( \* ) \s * | \s + ) ( [ ^ ( ] + ) / ,
254
+ ( _ , genStar , name ) => `var ${ name } = function ${ genStar || '' } ${ name } `
255
+ ) ;
256
+ }
257
+ // Append a \n so that it will be either
258
+ // terminated, or continued onto the next expression if it's an
259
+ // unexpected end of input.
260
+ return `${ cmd } \n` ;
261
+ }
262
+
241
263
function defaultEval ( code , context , file , cb ) {
264
+ // Remove trailing new line
265
+ code = code . replace ( / \n $ / , '' ) ;
266
+ code = preprocess ( code ) ;
267
+
242
268
var err , result , retry = false , input = code , wrappedErr ;
243
269
// first, create the Script object to check the syntax
244
270
@@ -506,8 +532,7 @@ function REPLServer(prompt,
506
532
}
507
533
}
508
534
509
- var evalCmd = self . bufferedCommand + cmd ;
510
- evalCmd = preprocess ( evalCmd ) ;
535
+ const evalCmd = self . bufferedCommand + cmd + '\n' ;
511
536
512
537
debug ( 'eval %j' , evalCmd ) ;
513
538
self . eval ( evalCmd , self . context , 'repl' , finish ) ;
@@ -564,28 +589,6 @@ function REPLServer(prompt,
564
589
// Display prompt again
565
590
self . displayPrompt ( ) ;
566
591
}
567
-
568
- function preprocess ( code ) {
569
- let cmd = code ;
570
- if ( / ^ \s * \{ / . test ( cmd ) && / \} \s * $ / . test ( cmd ) ) {
571
- // It's confusing for `{ a : 1 }` to be interpreted as a block
572
- // statement rather than an object literal. So, we first try
573
- // to wrap it in parentheses, so that it will be interpreted as
574
- // an expression.
575
- cmd = `(${ cmd } )` ;
576
- self . wrappedCmd = true ;
577
- } else {
578
- // Mitigate https://github.com/nodejs/node/issues/548
579
- cmd = cmd . replace (
580
- / ^ \s * f u n c t i o n (?: \s * ( \* ) \s * | \s + ) ( [ ^ ( ] + ) / ,
581
- ( _ , genStar , name ) => `var ${ name } = function ${ genStar || '' } ${ name } `
582
- ) ;
583
- }
584
- // Append a \n so that it will be either
585
- // terminated, or continued onto the next expression if it's an
586
- // unexpected end of input.
587
- return `${ cmd } \n` ;
588
- }
589
592
} ) ;
590
593
591
594
self . on ( 'SIGCONT' , function onSigCont ( ) {
0 commit comments