@@ -370,6 +370,7 @@ function REPLServer(prompt,
370
370
this . displayPrompt ( ) ;
371
371
} ) ;
372
372
373
+ var self = this ;
373
374
this . on ( 'line' , ( cmd ) => {
374
375
debug ( 'line %j' , cmd ) ;
375
376
sawSIGINT = false ;
@@ -396,73 +397,73 @@ function REPLServer(prompt,
396
397
}
397
398
}
398
399
399
- const finish = ( e , ret ) => {
400
+ if ( ! skipCatchall && ( cmd || ( ! cmd && this . bufferedCommand ) ) ) {
401
+ var evalCmd = this . bufferedCommand + cmd ;
402
+ if ( / ^ \s * \{ / . test ( evalCmd ) && / \} \s * $ / . test ( evalCmd ) ) {
403
+ // It's confusing for `{ a : 1 }` to be interpreted as a block
404
+ // statement rather than an object literal. So, we first try
405
+ // to wrap it in parentheses, so that it will be interpreted as
406
+ // an expression.
407
+ evalCmd = '(' + evalCmd + ')\n' ;
408
+ } else {
409
+ // otherwise we just append a \n so that it will be either
410
+ // terminated, or continued onto the next expression if it's an
411
+ // unexpected end of input.
412
+ evalCmd = evalCmd + '\n' ;
413
+ }
414
+
415
+ debug ( 'eval %j' , evalCmd ) ;
416
+ this . eval ( evalCmd , this . context , 'repl' , finish ) ;
417
+ } else {
418
+ finish ( null ) ;
419
+ }
420
+
421
+ function finish ( e , ret ) {
400
422
debug ( 'finish' , e , ret ) ;
401
- this . memory ( cmd ) ;
423
+ self . memory ( cmd ) ;
402
424
403
- if ( e && ! this . bufferedCommand && cmd . trim ( ) . match ( / ^ n p m / ) ) {
404
- this . outputStream . write ( 'npm should be run outside of the ' +
425
+ if ( e && ! self . bufferedCommand && cmd . trim ( ) . match ( / ^ n p m / ) ) {
426
+ self . outputStream . write ( 'npm should be run outside of the ' +
405
427
'node repl, in your normal shell.\n' +
406
428
'(Press Control-D to exit.)\n' ) ;
407
- this . lineParser . reset ( ) ;
408
- this . bufferedCommand = '' ;
409
- this . displayPrompt ( ) ;
429
+ self . lineParser . reset ( ) ;
430
+ self . bufferedCommand = '' ;
431
+ self . displayPrompt ( ) ;
410
432
return ;
411
433
}
412
434
413
435
// If error was SyntaxError and not JSON.parse error
414
436
if ( e ) {
415
- if ( e instanceof Recoverable && ! this . lineParser . shouldFail ) {
437
+ if ( e instanceof Recoverable && ! self . lineParser . shouldFail ) {
416
438
// Start buffering data like that:
417
439
// {
418
440
// ... x: 1
419
441
// ... }
420
- this . bufferedCommand += cmd + '\n' ;
421
- this . displayPrompt ( ) ;
442
+ self . bufferedCommand += cmd + '\n' ;
443
+ self . displayPrompt ( ) ;
422
444
return ;
423
445
} else {
424
- this . _domain . emit ( 'error' , e . err || e ) ;
446
+ self . _domain . emit ( 'error' , e . err || e ) ;
425
447
}
426
448
}
427
449
428
450
// Clear buffer if no SyntaxErrors
429
- this . lineParser . reset ( ) ;
430
- this . bufferedCommand = '' ;
451
+ self . lineParser . reset ( ) ;
452
+ self . bufferedCommand = '' ;
431
453
432
454
// If we got any output - print it (if no error)
433
455
if ( ! e &&
434
456
// When an invalid REPL command is used, error message is printed
435
457
// immediately. We don't have to print anything else. So, only when
436
458
// the second argument to this function is there, print it.
437
459
arguments . length === 2 &&
438
- ( ! this . ignoreUndefined || ret !== undefined ) ) {
439
- this . context . _ = ret ;
440
- this . outputStream . write ( this . writer ( ret ) + '\n' ) ;
460
+ ( ! self . ignoreUndefined || ret !== undefined ) ) {
461
+ self . context . _ = ret ;
462
+ self . outputStream . write ( self . writer ( ret ) + '\n' ) ;
441
463
}
442
464
443
465
// Display prompt again
444
- this . displayPrompt ( ) ;
445
- } ;
446
-
447
- if ( ! skipCatchall && ( cmd || ( ! cmd && this . bufferedCommand ) ) ) {
448
- var evalCmd = this . bufferedCommand + cmd ;
449
- if ( / ^ \s * \{ / . test ( evalCmd ) && / \} \s * $ / . test ( evalCmd ) ) {
450
- // It's confusing for `{ a : 1 }` to be interpreted as a block
451
- // statement rather than an object literal. So, we first try
452
- // to wrap it in parentheses, so that it will be interpreted as
453
- // an expression.
454
- evalCmd = '(' + evalCmd + ')\n' ;
455
- } else {
456
- // otherwise we just append a \n so that it will be either
457
- // terminated, or continued onto the next expression if it's an
458
- // unexpected end of input.
459
- evalCmd = evalCmd + '\n' ;
460
- }
461
-
462
- debug ( 'eval %j' , evalCmd ) ;
463
- this . eval ( evalCmd , this . context , 'repl' , finish ) ;
464
- } else {
465
- finish ( null ) ;
466
+ self . displayPrompt ( ) ;
466
467
}
467
468
} ) ;
468
469
@@ -1109,10 +1110,10 @@ REPLServer.prototype.convertToContext = function(cmd) {
1109
1110
const scopeFunc = / ^ \s * f u n c t i o n \s * ( [ _ \w \$ ] + ) / ;
1110
1111
var matches ;
1111
1112
1112
- // Replaces: var foo = "bar"; with: this.context.foo = bar;
1113
+ // DOUBLECHECK(tflanagan) should "self" be replaced with "this"?
1114
+ // Replaces: var foo = "bar"; with: self.context.foo = bar;
1113
1115
matches = scopeVar . exec ( cmd ) ;
1114
1116
if ( matches && matches . length === 3 ) {
1115
- // DOUBLECHECK(tristian): should "self" be "this"?
1116
1117
return 'self.context.' + matches [ 1 ] + matches [ 2 ] ;
1117
1118
}
1118
1119
0 commit comments