deno: Exiting the REPL should be `.exit` instead of `exit`

Currently you exit the REPL via exit. This is inconsistent with Node.js (which is .exit) and also collides with what is a valid variable name in JavaScript.

> const exit = "";
error: Uncaught SyntaxError: Identifier 'exit' has already been declared
► <unknown>:1:1
    at evaluate ($deno$/repl.ts:64:34)
    at Object.replLoop ($deno$/repl.ts:153:13)
> exit = "bar"
bar
> 

and even funnier/dangerous, this will cause the Deno REPL to exit:

> console.log(exit)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 28 (24 by maintainers)

Most upvoted comments

No, _, _error, and help/_help work because they are just values, though automatically set. Exit is not the same, we should choose between .exit and removal (i.e. just using Deno.exit()).

Either way, if we support more REPL commands then we need a pattern for it, and .<cmd> is the only one that would generally make sense. Consider if we added a break command for example. These are meta REPL commands, they shouldn’t be valid JS. (So what @Caesar2011 said.)

Only point, forcing CTRL-D only is an accessibility issue. Being able to type something out without needing to hold another key down is always preferable from that aspect.

I think arbitrarily appropriating valid, top-level/global variables in order to use them as REPL environment commands will be an unwelcome surprise to some people.

How about just using the _ prefix? We already have _ and _error, we can simply have _exit - as terse as .exit and is valid JS, and unlikely something people would name in REPL

@Caesar2011 It does, type help.

The problem I see, and that’s why I asked about the help command. If we want to implement other commands like help or save like node. With the same problem like now with exit. And in this case we should be consistent. Deno.help() and Deno.save() do not make any sense, but .save and .help does. As special REPL commands they do not have to be compliant with JS code. And .exit is in some way also a special command - or at least - expected if we have .help, .load and .save.

Either way, if we support more REPL commands then we need a pattern for it, and .<cmd> is the only one that would generally make sense. Consider if we added a break command for example. These are meta REPL commands, they shouldn’t be valid JS. (So what @Caesar2011 said.)

I guess I am not fully on board that we must use non-JS syntax to do meta commands that also makes sense in JS like _exit (since it make sense to be a JS thing as it could be implemented as a getter that have side effects), but I do agree for other ones beyond JS where there is room for debate. V8 uses % prefix for some operations so that would also be an interesting candidate.

Why not process the line REPL before pass it to V8 as JavaScript code, if the line matches /^\s*exit\s*$/ then exit. So we can both keep the feature and prevent to add something to JS.

Preprocessing is an option, though the regex you propose might be too broad.

@Soremwar Doing everything before is not interactive. That’s the whole point of a REPL. You don’t start one with an exact plan. By the way you didn’t address .break and .editor. So yeah, there’s no way to avoid non-JS commands if we want those features.

@Caesar2011 It doesn’t

@kitsonk Well, what do you think? In the end it’s all a matter of preference

exit could be patched to execute only when alone, so no const exit or console.log(exit) oddities. But as I said, more code to maintain

In its syntax position it is never valid JavaScript.

That’s the problem, you have to handle this cases in a different way you would handle JS code. That means more code to maintain

Why bother? Better to use already existing code Functions make it clear that you will execute an action (in this case well, exit XD) so that’s bonus for readability

Just my opinion though

I just checked and Deno is available in REPL. And Deno.exit() is already implemented in 0.40.0 and works like a charm to exit REPL.

Let’s make it a function then, like Python

Deno.exit() Could work well

Not sure if Deno is available in the REPL though, but including it inside (event partially) is a way better option than read stdin to provide .command functionality, or just dump variables into the namespace that may conflict with user programs if not well known

@kitsonk Is right, typed commands alongside keyboard commands provide way better accessibility, and are very desirable in a REPL

I use Ctrl+D all the time, much quicker and cooler. You can send the signal issued by Ctrl+d when using pipes, too.

Maybe we could also show the version number within the informational text on start.

agree.

Welcome to Node.js v12.14.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file

Press ^C to abort current expression, ^D to exit the repl

It’s just that it isn’t javascript. Can we just remove it instead?

I use ctrl+d to exit. Maybe we can just print a reminder for people when the repl starts?