[SERVER-4036] Suggestion: insert semicolons into recalled JavaScript in the shell Created: 07/Oct/11  Updated: 06/Dec/22  Resolved: 19/Nov/21

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: features we're not sure of

Type: Improvement Priority: Trivial - P5
Reporter: Tad Marshall Assignee: Backlog - Server Tooling and Methods (STM) (Inactive)
Resolution: Won't Fix Votes: 0
Labels: move-stm, platforms-re-triaged
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Any


Assigned Teams:
Server Tooling & Methods
Participants:

 Description   

I'll say up front that I'm not sure if this is worth doing. But it might be easy, and have some benefit, so maybe.

If pull request 102 is accepted, we will retrieve multiline JavaScript onto the command line when up-arrow is pressed following a multiline entry. In order to enable command line editing, we will have converted newlines into spaces. This converts the entry into something that we can edit on a single line.

It can also break legal JavaScript by preventing JavaScript's automatic semicolon insertion from seeing the newlines that the user entered.

MongoDB shell version: 2.1.0-pre-
connecting to: test
> f=function(){
... a=1 // automatic JavaScript semicolon insertion
... b=2
... }
function () {
    a = 1; // cool, mongo types better than I do, with better spacing and punctuation
    b = 2;
}
> f() // life is good
> f=function(){ a=1 b=2 } // press up-arrow, command is as-typed with newlines converted to spaces
Fri Oct 07 13:35:51 SyntaxError: missing ; before statement (shell):1 // but it's not legal JavaScript anymore

Note also that the "pretty print" code shows where semicolons would be inserted by this JavaScript feature. If we grabbed the text that this code displays (called the same code) and then converted the newlines into spaces then we could provide command line editing of legal code even if the user typed it in differently. We're already converting newlines into spaces to make this scenario possible, so it's not a big stretch to shove semicolons into the string to keep their legal JavaScript legal.



 Comments   
Comment by Brooke Miller [ 19/Nov/21 ]

We've deprecated the mongo shell in favor of the new mongosh . Unfortunately, we aren't able to pursue improvements to the deprecated shell except in extreme cases, such as critical security fixes. Can you please start making use of mongosh and let us know if it works for you in this case?

Comment by Andrew Morrow (Inactive) [ 17/Aug/18 ]

asya - If you want platforms to consider pulling old tickets forward from the backlog, I'd suggesting moving the ticket back to needs triage. We will pick it up and look at it again within a week.

Comment by Asya Kamsky [ 16/Aug/18 ]

I've been bitten by this a lot

Comment by Tad Marshall [ 08/Oct/11 ]

Using the output of the "pretty print" code directly isn't perfect, because of the spacing that it adds to give the nice indentation. So if the "insert semicolons" feature is added, it might need its own version of the pretty printing code to do the semicolon insertion without the indentation. Just speculation, I haven't looked at the code yet.

Comment by Tad Marshall [ 07/Oct/11 ]

Jira compressed some well formatted text into something that you can't read: I need to learn how to do insertion of program output into a bug report such that it isn't clobbered.

The idea is that you type two lines of text into a function without adding a semicolon between them because if this was a regular text file you wouldn't need to. Because of the line break, JavaScript sees two lines and knows how to interpret it. The transformation that we do to enable single-line editing (as of pull request 102) isn't doing anything to compensate for throwing away line breaks and this isn't as good as it could be.

The text that was clobbered by Jira consisted of typing "f=function()

{" [enter] "a=1" [enter] "b=2" [enter] "}

" [enter] "f()" [enter] [up-arrow] [enter]. The bit around the "cool" comment showed nicely formatted JavaScript with semicolons inserted where I hadn't bothered to type them because I didn't need to. Once the command is recalled, I need them.

Generated at Thu Feb 08 03:04:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.