Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

REPL: Fix for multiline input when using custom eval function #25587

Closed
wants to merge 5 commits into from

Conversation

mikeybox
Copy link

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

Michael Edwards added 2 commits June 28, 2015 18:56
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
@@ -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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should read non-standard rather than none standard.

@mikeybox
Copy link
Author

thanks @davidchambers updated the copy.

@thefourtheye
Copy link

Can we see an example to reproduce the problem?

@mikeybox
Copy link
Author

Hey @thefourtheye this fix was submitted because it breaks multi-line support in Babel.js #1741

To reproduce the error you can simply use a custom eval function passed to the REPL server,
you will see that syntax errors are no longer caught in the finish method of the server.
The problem is the event upgrade that happens in the defaultEval handler is local to REPL so overriding this means that no syntax errors created in a custom eval will be promoted to Recoverable.

in the finish method bellow at line 300 it checks for errors to be an instance of Recoverable
this will never happen for errors thrown in a custom eval method.

// If error was SyntaxError and not JSON.parse error
      if (e) {
        if (e instanceof Recoverable) {
          // Start buffering data like that:
          // {
          // ...  x: 1
          // ... }
          self.bufferedCommand += cmd + '\n';
          self.displayPrompt();
          return;
        } else {
          self._domain.emit('error', e);
        }
      }

In the fix I also included a recoverable test option for instances where a custom eval might throw an error that is recoverable but the message is non-standard (this happens in the case of Babel) and will not be matched by isRecoverable

@jasnell
Copy link
Member

jasnell commented Aug 6, 2015

@joyent/node-tsc ... this change proposes adding a new option, which is technically an API change, but it's for a good reason. Thoughts on this one?

@@ -94,6 +94,7 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
ignoreUndefined = options.ignoreUndefined;
prompt = options.prompt;
dom = options.domain;
recoverableCheck = options.recoverable || isRecoverableError;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be better as a ternary based on whether options.recoverable is a function or not.

@cjihrig
Copy link

cjihrig commented Aug 9, 2015

@jasnell I left a few comments. I don't really have an opinion on whether this gets in or not.

@mikeybox
Copy link
Author

@cjihrig I'll add your suggestions to the PR, in terms of testing you're right it isn't really tested but neither was the eval function (unless I'm missing something??) so I just kept it in line with that as it directly affects that.

Michael Edwards added 2 commits August 25, 2015 14:32
@smasher164
Copy link

@mikeybox Here's the relevant issue on nodejs/node#2939

@CestDiego
Copy link

are there any news on this?

@mikeybox
Copy link
Author

mikeybox commented Dec 7, 2015

@CestDiego there is a similar fix in a PR which is further along nodejs/node#3488.

@mikeybox mikeybox closed this Dec 7, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants