Skip to content

Commit a1b9103

Browse files
committed
lib: fix repl.js
1 parent 139e960 commit a1b9103

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

lib/repl.js

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ function REPLServer(prompt,
370370
this.displayPrompt();
371371
});
372372

373+
var self = this;
373374
this.on('line', (cmd) => {
374375
debug('line %j', cmd);
375376
sawSIGINT = false;
@@ -396,73 +397,73 @@ function REPLServer(prompt,
396397
}
397398
}
398399

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) {
400422
debug('finish', e, ret);
401-
this.memory(cmd);
423+
self.memory(cmd);
402424

403-
if (e && !this.bufferedCommand && cmd.trim().match(/^npm /)) {
404-
this.outputStream.write('npm should be run outside of the ' +
425+
if (e && !self.bufferedCommand && cmd.trim().match(/^npm /)) {
426+
self.outputStream.write('npm should be run outside of the ' +
405427
'node repl, in your normal shell.\n' +
406428
'(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();
410432
return;
411433
}
412434

413435
// If error was SyntaxError and not JSON.parse error
414436
if (e) {
415-
if (e instanceof Recoverable && !this.lineParser.shouldFail) {
437+
if (e instanceof Recoverable && !self.lineParser.shouldFail) {
416438
// Start buffering data like that:
417439
// {
418440
// ... x: 1
419441
// ... }
420-
this.bufferedCommand += cmd + '\n';
421-
this.displayPrompt();
442+
self.bufferedCommand += cmd + '\n';
443+
self.displayPrompt();
422444
return;
423445
} else {
424-
this._domain.emit('error', e.err || e);
446+
self._domain.emit('error', e.err || e);
425447
}
426448
}
427449

428450
// Clear buffer if no SyntaxErrors
429-
this.lineParser.reset();
430-
this.bufferedCommand = '';
451+
self.lineParser.reset();
452+
self.bufferedCommand = '';
431453

432454
// If we got any output - print it (if no error)
433455
if (!e &&
434456
// When an invalid REPL command is used, error message is printed
435457
// immediately. We don't have to print anything else. So, only when
436458
// the second argument to this function is there, print it.
437459
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');
441463
}
442464

443465
// 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();
466467
}
467468
});
468469

@@ -1109,10 +1110,10 @@ REPLServer.prototype.convertToContext = function(cmd) {
11091110
const scopeFunc = /^\s*function\s*([_\w\$]+)/;
11101111
var matches;
11111112

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;
11131115
matches = scopeVar.exec(cmd);
11141116
if (matches && matches.length === 3) {
1115-
// DOUBLECHECK(tristian): should "self" be "this"?
11161117
return 'self.context.' + matches[1] + matches[2];
11171118
}
11181119

0 commit comments

Comments
 (0)