Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 15 additions & 7 deletions packages/cli-repl/src/js-multiline-to-singleline.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { transformSync } from '@babel/core';

export function makeMultilineJSIntoSingleLine(src: string): string {
// We use babel without any actual transofmration steps, and only for ASI
// We use babel without any actual transformation steps, and only for ASI
// effects here, e.g. turning `return\n42` into `return;\n42;`
// since without the added semicolons semantics would be different.
// This unfortunately modifies the code in some other ways as well, e.g.
// removing unnecessary parentheses, but correctness seems to be more
// important here than keeping aesthetics intact.
// It would be nice to have a dedicated package at some point that does
// ASI and *only* ASI and leaves the source intact otherwise.
const postASI = transformSync(src, {
retainLines: true,
compact: false,
code: true,
comments: false
})?.code ?? src;
let postASI: string;
try {
postASI = transformSync(src, {
retainLines: true,
compact: false,
code: true,
comments: false
})?.code ?? src;
} catch {
// The src might still be invalid, e.g. because a recoverable error was not fixed
// and is now an unrecoverable error. Best we can do is just keep the lines now.
postASI = src;
}

const asSingleLine = postASI.split(/[\r\n]+/g)
.map(line => line.trim())
.join(' ')
Expand Down
22 changes: 22 additions & 0 deletions packages/cli-repl/src/mongosh-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,28 @@ describe('MongoshNodeRepl', () => {
expect(output).not.to.include('Error');
});

it('allows to enter and fix recoverable errors', async() => {
input.write('24 %\n');
await waitEval(bus);
expect(output).to.not.include('Error');

input.write('6\n');
await waitEval(bus);
expect(output).to.include(0);
expect(output).to.not.include('Error');
});

it('behaves correctly on non-recoverable multi-line errors', async() => {
input.write('24 %\n');
await waitEval(bus);
expect(output).to.not.include('Error');

input.write(';\n');
await waitEval(bus);
expect(output).to.not.include('MongoshInternalError');
expect(output).to.include('SyntaxError');
});

it('pressing Ctrl+C twice exits the shell', async() => {
input.write('\u0003');
await tick();
Expand Down