Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
repl: add friendly tips about how to exit repl
Imitate python repl, when the user enters 'exit' or 'quit',
no longer prompt 'Reference Error', but
prompts 'To exit, press ^D or type .exit'.
If the user defines variables named 'exit' or 'quit' ,
only the value of the variables are output

Fixes: #19021
  • Loading branch information
monkingxue committed May 18, 2018
commit 7b88a82e33a6bdab0027bbb2707f14c93652bac6
32 changes: 20 additions & 12 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,16 @@ function REPLServer(prompt,
}

function defaultEval(code, context, file, cb) {
var err, result, script, wrappedErr;
var err, result, script, wrappedErr, trimCmd;
var isExitCmd = false;
var wrappedCmd = false;
var awaitPromise = false;
var input = code;

if ((trimCmd = code.trim()) && (trimCmd === 'exit' || trimCmd === 'quit')) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just a tiny style thing, but could you maybe turn trimCmd = code.trim() into its own line? Also, don’t be shy to use a verbose name like trimCommand/isExitCommand.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you soooo much about your advice! I will modify the code as soon.

isExitCmd = true;
}

if (/^\s*\{/.test(code) && /\}\s*$/.test(code)) {
// It's confusing for `{ a : 1 }` to be interpreted as a block
// statement rather than an object literal. So, we first try
Expand Down Expand Up @@ -331,18 +336,21 @@ function REPLServer(prompt,
}
}
} catch (e) {
err = e;

if (err && err.code === 'ERR_SCRIPT_EXECUTION_INTERRUPTED') {
if (isExitCmd) {
self.outputStream.write('(To exit, press ^D or type .exit)\n');
return self.displayPrompt();
} else {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you don’t really need to add the else { … } here since you return early in the if() {} block anyway.

err = e;
if (err && err.code === 'ERR_SCRIPT_EXECUTION_INTERRUPTED') {
// The stack trace for this case is not very useful anyway.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: this is a unrelated change and I think the comment is better the way it was before.

Object.defineProperty(err, 'stack', { value: '' });
}

if (process.domain) {
debug('not recoverable, send to domain');
process.domain.emit('error', err);
process.domain.exit();
return;
Object.defineProperty(err, 'stack', { value: '' });
}
if (process.domain) {
debug('not recoverable, send to domain');
process.domain.emit('error', err);
process.domain.exit();
return;
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,29 @@ const strictModeTests = [
}
];

const friendlyExitTests = [
{
send: 'exit',
expect: '(To exit, press ^D or type .exit)'
},
{
send: 'quit',
expect: '(To exit, press ^D or type .exit)'
},
{
send: 'quit = 1',
expect: '1'
},
{
send: 'quit',
expect: '1'
},
{
send: 'exit',
expect: '(To exit, press ^D or type .exit)'
},
];

const errorTests = [
// Uncaught error throws and prints out
{
Expand Down Expand Up @@ -742,6 +765,7 @@ const tcpTests = [
const [ socket, replServer ] = await startUnixRepl();

await runReplTests(socket, prompt_unix, unixTests);
await runReplTests(socket, prompt_unix, friendlyExitTests);
await runReplTests(socket, prompt_unix, errorTests);
replServer.replMode = repl.REPL_MODE_STRICT;
await runReplTests(socket, prompt_unix, strictModeTests);
Expand Down