From 92cf3684919e843c8dc41b21d41327e2eadbe2e4 Mon Sep 17 00:00:00 2001 From: Michael Edwards Date: Sun, 28 Jun 2015 18:56:05 +0100 Subject: [PATCH 1/5] 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 #8640 --- doc/api/repl.markdown | 2 ++ lib/repl.js | 12 ++++-------- test/simple/test-repl-options.js | 2 ++ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/doc/api/repl.markdown b/doc/api/repl.markdown index e5081c3c8db4..fcb6a3146aad 100644 --- a/doc/api/repl.markdown +++ b/doc/api/repl.markdown @@ -49,6 +49,8 @@ the following values: - `eval` - function that will be used to eval each given line. Defaults to an async wrapper for `eval()`. See below for an example of a custom `eval`. + + - `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. - `useColors` - a boolean which specifies whether or not the `writer` function should output colors. If a different `writer` function is set then this does diff --git a/lib/repl.js b/lib/repl.js index 3bb145bb44f7..ee446bd144bc 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -82,7 +82,7 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) { return new REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined); } - var options, input, output, dom; + var options, input, output, dom, recoverableCheck; if (util.isObject(prompt)) { // an options object was given options = prompt; @@ -94,6 +94,7 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) { ignoreUndefined = options.ignoreUndefined; prompt = options.prompt; dom = options.domain; + recoverableCheck = options.recoverable || isRecoverableError; } else if (!util.isString(prompt)) { throw new Error('An options Object, or a prompt String are required'); } else { @@ -298,7 +299,7 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) { // If error was SyntaxError and not JSON.parse error if (e) { - if (e instanceof Recoverable) { + if (recoverableCheck(e)) { // Start buffering data like that: // { // ... x: 1 @@ -952,9 +953,4 @@ function isRecoverableError(e) { return e && e.name === 'SyntaxError' && /^(Unexpected end of input|Unexpected token)/.test(e.message); -} - -function Recoverable(err) { - this.err = err; -} -inherits(Recoverable, SyntaxError); +} \ No newline at end of file diff --git a/test/simple/test-repl-options.js b/test/simple/test-repl-options.js index 94a622da2609..78fd520d4c04 100644 --- a/test/simple/test-repl-options.js +++ b/test/simple/test-repl-options.js @@ -58,6 +58,7 @@ assert.equal(r1.useColors, r1.rli.terminal); // 2 function writer() {} function evaler() {} +function recoverTester() {} var r2 = repl.start({ input: stream, output: stream, @@ -66,6 +67,7 @@ var r2 = repl.start({ useGlobal: true, ignoreUndefined: true, eval: evaler, + recover: recoverTester, writer: writer }); assert.equal(r2.input, stream); From 13afaec37e67e084628fe10d6e26aa0251661d5f Mon Sep 17 00:00:00 2001 From: Michael Edwards Date: Sun, 28 Jun 2015 19:01:43 +0100 Subject: [PATCH 2/5] typo on recoverable. --- test/simple/test-repl-options.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/simple/test-repl-options.js b/test/simple/test-repl-options.js index 78fd520d4c04..9652d69a2dd1 100644 --- a/test/simple/test-repl-options.js +++ b/test/simple/test-repl-options.js @@ -67,7 +67,7 @@ var r2 = repl.start({ useGlobal: true, ignoreUndefined: true, eval: evaler, - recover: recoverTester, + recoverable: recoverTester, writer: writer }); assert.equal(r2.input, stream); From 197f5347d30734b068a5d7e397d78b1c8f00cd76 Mon Sep 17 00:00:00 2001 From: Michael Edwards Date: Wed, 29 Jul 2015 20:44:57 +0100 Subject: [PATCH 3/5] Corrected copy to read non-standard errors. --- doc/api/repl.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/api/repl.markdown b/doc/api/repl.markdown index fcb6a3146aad..0ae8a9a93202 100644 --- a/doc/api/repl.markdown +++ b/doc/api/repl.markdown @@ -32,8 +32,8 @@ For example, you could add this to your bashrc file: ## repl.start(options) -Returns and starts a `REPLServer` instance, that inherits from -[Readline Interface][]. Accepts an "options" Object that takes +Returns and starts a `REPLServer` instance, that inherits from +[Readline Interface][]. Accepts an "options" Object that takes the following values: - `prompt` - the prompt and `stream` for all I/O. Defaults to `> `. @@ -49,8 +49,8 @@ the following values: - `eval` - function that will be used to eval each given line. Defaults to an async wrapper for `eval()`. See below for an example of a custom `eval`. - - - `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. + + - `recoverable` - function that will return a `bool` when passed an error and report if it is recoverable. Use if your `eval` returns non-standard errors but you still want the benefits of multiline input. - `useColors` - a boolean which specifies whether or not the `writer` function should output colors. If a different `writer` function is set then this does From e76ef5022941a82e062d12064d7d5725256adcf7 Mon Sep 17 00:00:00 2001 From: Michael Edwards Date: Tue, 25 Aug 2015 14:32:03 +0100 Subject: [PATCH 4/5] Removed defunct `Recoverable` function. --- lib/repl.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index ee446bd144bc..6b09adefdb3e 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -123,10 +123,7 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) { }); } catch (e) { debug('parse error %j', code, e); - if (isRecoverableError(e)) - err = new Recoverable(e); - else - err = e; + err = e; } if (!err) { From 6a20d0517273eb5da9a712f22e6f6117803603e7 Mon Sep 17 00:00:00 2001 From: Michael Edwards Date: Tue, 25 Aug 2015 14:52:27 +0100 Subject: [PATCH 5/5] fixed listing error No newline at end of file --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 6b09adefdb3e..7a5bec4f0496 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -950,4 +950,4 @@ function isRecoverableError(e) { return e && e.name === 'SyntaxError' && /^(Unexpected end of input|Unexpected token)/.test(e.message); -} \ No newline at end of file +}