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 PR-URL: https://github.com/nodejs/node/pull/20617 Fixes: https://github.com/nodejs/node/issues/19021 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
9deca876bb
commit
9aa4ec43fc
18
lib/repl.js
18
lib/repl.js
@ -215,9 +215,15 @@ function REPLServer(prompt,
|
|||||||
|
|
||||||
function defaultEval(code, context, file, cb) {
|
function defaultEval(code, context, file, cb) {
|
||||||
var err, result, script, wrappedErr;
|
var err, result, script, wrappedErr;
|
||||||
|
var isExitCommand = false;
|
||||||
var wrappedCmd = false;
|
var wrappedCmd = false;
|
||||||
var awaitPromise = false;
|
var awaitPromise = false;
|
||||||
var input = code;
|
var input = code;
|
||||||
|
var trimmedCommand = code.trim();
|
||||||
|
|
||||||
|
if (trimmedCommand === 'exit' || trimmedCommand === 'quit') {
|
||||||
|
isExitCommand = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (/^\s*\{/.test(code) && /\}\s*$/.test(code)) {
|
if (/^\s*\{/.test(code) && /\}\s*$/.test(code)) {
|
||||||
// It's confusing for `{ a : 1 }` to be interpreted as a block
|
// It's confusing for `{ a : 1 }` to be interpreted as a block
|
||||||
@ -313,10 +319,16 @@ function REPLServer(prompt,
|
|||||||
breakOnSigint: self.breakEvalOnSigint
|
breakOnSigint: self.breakEvalOnSigint
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const localContext = self.useGlobal ? global : self.context;
|
||||||
|
if (isExitCommand && !localContext.hasOwnProperty(trimmedCommand)) {
|
||||||
|
self.outputStream.write('(To exit, press ^D or type .exit)\n');
|
||||||
|
return self.displayPrompt();
|
||||||
|
}
|
||||||
|
|
||||||
if (self.useGlobal) {
|
if (self.useGlobal) {
|
||||||
result = script.runInThisContext(scriptOptions);
|
result = script.runInThisContext(scriptOptions);
|
||||||
} else {
|
} else {
|
||||||
result = script.runInContext(context, scriptOptions);
|
result = script.runInContext(localContext, scriptOptions);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (self.breakEvalOnSigint) {
|
if (self.breakEvalOnSigint) {
|
||||||
@ -332,12 +344,10 @@ function REPLServer(prompt,
|
|||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
err = e;
|
err = e;
|
||||||
|
|
||||||
if (err && err.code === 'ERR_SCRIPT_EXECUTION_INTERRUPTED') {
|
if (err && err.code === 'ERR_SCRIPT_EXECUTION_INTERRUPTED') {
|
||||||
// The stack trace for this case is not very useful anyway.
|
// The stack trace for this case is not very useful anyway.
|
||||||
Object.defineProperty(err, 'stack', { value: '' });
|
Object.defineProperty(err, 'stack', { value: '' });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.domain) {
|
if (process.domain) {
|
||||||
debug('not recoverable, send to domain');
|
debug('not recoverable, send to domain');
|
||||||
process.domain.emit('error', err);
|
process.domain.emit('error', err);
|
||||||
|
@ -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 = [
|
const errorTests = [
|
||||||
// Uncaught error throws and prints out
|
// Uncaught error throws and prints out
|
||||||
{
|
{
|
||||||
@ -742,6 +765,7 @@ const tcpTests = [
|
|||||||
const [ socket, replServer ] = await startUnixRepl();
|
const [ socket, replServer ] = await startUnixRepl();
|
||||||
|
|
||||||
await runReplTests(socket, prompt_unix, unixTests);
|
await runReplTests(socket, prompt_unix, unixTests);
|
||||||
|
await runReplTests(socket, prompt_unix, friendlyExitTests);
|
||||||
await runReplTests(socket, prompt_unix, errorTests);
|
await runReplTests(socket, prompt_unix, errorTests);
|
||||||
replServer.replMode = repl.REPL_MODE_STRICT;
|
replServer.replMode = repl.REPL_MODE_STRICT;
|
||||||
await runReplTests(socket, prompt_unix, strictModeTests);
|
await runReplTests(socket, prompt_unix, strictModeTests);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user