Skip to content

Commit 92cf368

Browse files
author
Michael Edwards
committed
REPL: Fix for multiline input when using custom eval function
Multiline input doesn't work if user overrides "eval" in REPL.start. Overriding default eval will stop multiline inputs on syntax errors because of the current error checking in the finish callback. This fixes that issue and allows for a custom recoverableError check function to be passed the the REPL server for more customisable use. fixes nodejs#8640
1 parent b436e59 commit 92cf368

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

doc/api/repl.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ the following values:
4949

5050
- `eval` - function that will be used to eval each given line. Defaults to
5151
an async wrapper for `eval()`. See below for an example of a custom `eval`.
52+
53+
- `recoverable` - function that will return a `bool` when passed an error and report if it is recoverable. Use if your `eval` returns none standard errors but you still want the benefits of multiline input.
5254

5355
- `useColors` - a boolean which specifies whether or not the `writer` function
5456
should output colors. If a different `writer` function is set then this does

lib/repl.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
8282
return new REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined);
8383
}
8484

85-
var options, input, output, dom;
85+
var options, input, output, dom, recoverableCheck;
8686
if (util.isObject(prompt)) {
8787
// an options object was given
8888
options = prompt;
@@ -94,6 +94,7 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
9494
ignoreUndefined = options.ignoreUndefined;
9595
prompt = options.prompt;
9696
dom = options.domain;
97+
recoverableCheck = options.recoverable || isRecoverableError;
9798
} else if (!util.isString(prompt)) {
9899
throw new Error('An options Object, or a prompt String are required');
99100
} else {
@@ -298,7 +299,7 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
298299

299300
// If error was SyntaxError and not JSON.parse error
300301
if (e) {
301-
if (e instanceof Recoverable) {
302+
if (recoverableCheck(e)) {
302303
// Start buffering data like that:
303304
// {
304305
// ... x: 1
@@ -952,9 +953,4 @@ function isRecoverableError(e) {
952953
return e &&
953954
e.name === 'SyntaxError' &&
954955
/^(Unexpected end of input|Unexpected token)/.test(e.message);
955-
}
956-
957-
function Recoverable(err) {
958-
this.err = err;
959-
}
960-
inherits(Recoverable, SyntaxError);
956+
}

test/simple/test-repl-options.js

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ assert.equal(r1.useColors, r1.rli.terminal);
5858
// 2
5959
function writer() {}
6060
function evaler() {}
61+
function recoverTester() {}
6162
var r2 = repl.start({
6263
input: stream,
6364
output: stream,
@@ -66,6 +67,7 @@ var r2 = repl.start({
6667
useGlobal: true,
6768
ignoreUndefined: true,
6869
eval: evaler,
70+
recover: recoverTester,
6971
writer: writer
7072
});
7173
assert.equal(r2.input, stream);

0 commit comments

Comments
 (0)