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: add hint as to how to exit #20617

Closed
wants to merge 5 commits into from

Conversation

monkingxue
Copy link
Contributor

add friendly tips about how to exit repl.

Fixes: #19021

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the repl Issues and PRs related to the REPL subsystem. label May 9, 2018
@mscdex
Copy link
Contributor

mscdex commented May 9, 2018

I think it would be better to at least check that exit and/or quit (whichever was entered) are not already defined before making the suggestion.

@mscdex
Copy link
Contributor

mscdex commented May 9, 2018

Also, can you remove the merge commit?

@monkingxue
Copy link
Contributor Author

@mscdex Ok, I will remove the commit. And this is my first commit, so sorry.

@monkingxue
Copy link
Contributor Author

This is the effect changed now.

$ ./out/Release/node
> exit
(To exit, press ^D or type .exit)
> quit
(To exit, press ^D or type .exit)
> exit = 1
1
> exit
1
> quit = 2
2
> quit
2

@monkingxue
Copy link
Contributor Author

monkingxue commented May 9, 2018

When exit and/or quit have been defined, the runInThisContext function will not throw an error, but our prompt just be output in the catch block.

@Trott
Copy link
Member

Trott commented May 9, 2018

@nodejs/repl

@Trott
Copy link
Member

Trott commented May 9, 2018

Also pinging @princejwesley (who should totally let us know if they want to be added to @nodejs/repl).

@apapirovski apapirovski changed the title @monkingxue repl: add hint as to how to exit May 9, 2018
@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented May 9, 2018

Is this semver-major? Can there be cases when changed output is breaking?

@vsemozhetbyt
Copy link
Contributor

@princejwesley
Copy link
Contributor

Its good to be generic for all defined commands. Its like hint message property while defining commands and emit the message to help the user only when the command is not defined in the context (global or context) and there is no buffered command.

const context = this.useGlobal ? global : this.context;
if (context[cmd] === undefined && this.commands[cmd] && this[kBufferedCommandSymbol] === '') {
    // emit this.commands[cmd].hint
}

lib/repl.js Outdated
var wrappedCmd = false;
var awaitPromise = false;
var input = code;

if ((trimCmd = code.trim()) && (trimCmd === 'exit' || trimCmd === 'quit')) {
Copy link
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
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.

lib/repl.js Outdated
if (isExitCmd) {
self.outputStream.write('(To exit, press ^D or type .exit)\n');
return self.displayPrompt();
} else {
Copy link
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.

@monkingxue
Copy link
Contributor Author

@princejwesley The command must start with ., like .help, .exit and so on. So we cannot define a variable named same as command.

@monkingxue
Copy link
Contributor Author

Hi, this is my first time offering pr to nodejs, so what should I do after commit the changes? Thank you so much~

@jasnell
Copy link
Member

jasnell commented May 10, 2018

@monkingxue ... the process is: a number of us will review the commits and offer feedback as necessary. Once we're relatively sure it's ready, someone with commit access to kick off a CI test run in jenkins at ci.nodejs.org, and if it looks good we'll get it landed. So far you've done everything you need to do :-)

@monkingxue
Copy link
Contributor Author

@jasnell Thank u sooo much~ It is an honor to pr for nodejs !

@princejwesley
Copy link
Contributor

princejwesley commented May 10, 2018

@monkingxue Internally commands are not prefixed with . 😄 .

I don't think, its a good idea to check in the catch block after executing the code. because Its easy to define exit in such a way that it will throw error.

e.g.

$ node
> Object.defineProperty(global, 'exit', { get: function() { throw new Error('catch me') } });
Object [global] {
...
}
> exit
Error: catch me
    at get (repl:1:65)
>

I suggest to try something like this.

   const context = this.useGlobal ? global : this.context;
   if (isExitCommand  && !context.hasOwnProperty(trimCommand)) {
      // emit hint
   } 

@nodejs/repl what do you say?

@monkingxue
Copy link
Contributor Author

@princejwesley oh I see! You're right, i will fix the code, thank u suggestion!

lib/repl.js Outdated
@@ -338,7 +338,8 @@ function REPLServer(prompt,
}
}
} catch (e) {
if (isExitCommand) {
const context = this.useGlobal ? global : this.context;
if (isExitCommand && !context.hasOwnProperty(trimCommand)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this can be done before executing the code, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean define the context out of the catch block ?

Copy link
Contributor

Choose a reason for hiding this comment

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

No. the whole test condition. check this before executing vm.runInContext

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok I got it and I have modified the code, thank u~

Copy link
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

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

LGTM with my comments addressed.

lib/repl.js Outdated
var wrappedCmd = false;
var awaitPromise = false;
var input = code;
var trimCommand = code.trim();
Copy link
Member

Choose a reason for hiding this comment

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

Would you be so kind and rename the variable to trimmedCommand? Otherwise I would confuse it with a function :-)

if (err && err.code === 'ERR_SCRIPT_EXECUTION_INTERRUPTED') {
// The stack trace for this case is not very useful anyway.
Copy link
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.

lib/repl.js Outdated
var wrappedCmd = false;
var awaitPromise = false;
var input = code;
var trimCommand = code.trim();

if ((trimCommand === 'exit' || trimCommand === 'quit')) {
Copy link
Member

Choose a reason for hiding this comment

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

Please remove the extra brackets.

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: nodejs#19021
Before output the hint, check if defined
the command-named variable as an error
@BridgeAR
Copy link
Member

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label May 18, 2018
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request May 18, 2018
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: nodejs#20617
Fixes: nodejs#19021
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
@BridgeAR
Copy link
Member

Landed in 9aa4ec4

@monkingxue congratulations on your first commit to Node.js! 🎉

@BridgeAR BridgeAR closed this May 18, 2018
MylesBorins pushed a commit that referenced this pull request May 22, 2018
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: #20617
Fixes: #19021
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
@addaleax addaleax mentioned this pull request May 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support typing "exit" or "quit" means exit from REPL.
9 participants