Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: support standalone blocks #5581

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function REPLServer(prompt,
eval_ = eval_ || defaultEval;

function defaultEval(code, context, file, cb) {
var err, result, retry = false;
var err, result, retry = false, input = code, wrappedErr;
// first, create the Script object to check the syntax
while (true) {
try {
Expand All @@ -238,14 +238,23 @@ function REPLServer(prompt,
debug('parse error %j', code, e);
if (self.replMode === exports.REPL_MODE_MAGIC &&
e.message === BLOCK_SCOPED_ERROR &&
!retry) {
retry = true;
!retry || self.wrappedCmd) {
if (self.wrappedCmd) {
self.wrappedCmd = false;
// unwrap and try again
code = `${input.substring(1, input.length - 2)}\n`;
wrappedErr = e;
} else {
retry = true;
}
continue;
}
if (isRecoverableError(e, self))
err = new Recoverable(e);
// preserve original error for wrapped command
const error = wrappedErr || e;
if (isRecoverableError(error, self))
err = new Recoverable(error);
else
err = e;
err = error;
}
break;
}
Expand Down Expand Up @@ -418,6 +427,7 @@ function REPLServer(prompt,
// to wrap it in parentheses, so that it will be interpreted as
// an expression.
evalCmd = '(' + evalCmd + ')\n';
self.wrappedCmd = true;
} else {
// otherwise we just append a \n so that it will be either
// terminated, or continued onto the next expression if it's an
Expand All @@ -435,6 +445,7 @@ function REPLServer(prompt,
debug('finish', e, ret);
self.memory(cmd);

self.wrappedCmd = false;
if (e && !self.bufferedCommand && cmd.trim().match(/^npm /)) {
self.outputStream.write('npm should be run outside of the ' +
'node repl, in your normal shell.\n' +
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ function error_test() {
{ client: client_unix, send: 'function x(s) {\nreturn s.replace(/.*/,"");\n}',
expect: prompt_multiline + prompt_multiline +
'undefined\n' + prompt_unix },
{ client: client_unix, send: '{ var x = 4; }',
expect: 'undefined\n' + prompt_unix },
]);
}

Expand Down