Skip to content
Open
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
27 changes: 26 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const {
StringPrototypeCharAt,
StringPrototypeEndsWith,
StringPrototypeIncludes,
StringPrototypeLastIndexOf,
StringPrototypeRepeat,
StringPrototypeSlice,
StringPrototypeStartsWith,
Expand Down Expand Up @@ -802,7 +803,31 @@ class REPLServer extends Interface {
}

// Check REPL keywords and empty lines against a trimmed line input.
const trimmedCmd = StringPrototypeTrim(cmd);
let trimmedCmd = StringPrototypeTrim(cmd);

// TTY multiline input is stored as one editable line by readline. Check
// the newest physical line so dot-commands work from a continuation
// prompt, while keeping the previous lines buffered.
if (self.terminal &&
!self[kBufferedCommandSymbol] &&
StringPrototypeIncludes(cmd, '\n')) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I tested the changes on Windows, and the issue does not appear to be resolved yet. From my debugging, it looks like this condition-based check is not getting satisfied, so the logic may need additional updates.

const lastNewlineIndex = StringPrototypeLastIndexOf(cmd, '\n');
const commandLine = StringPrototypeSlice(cmd, lastNewlineIndex + 1);
const trimmedCommandLine = StringPrototypeTrim(commandLine);

if (StringPrototypeCharAt(trimmedCommandLine, 0) === '.' &&
StringPrototypeCharAt(trimmedCommandLine, 1) !== '.' &&
NumberIsNaN(NumberParseFloat(trimmedCommandLine))) {
const matches = RegExpPrototypeExec(/^\.([^\s]+)\s*(.*)$/, trimmedCommandLine);
const keyword = matches?.[1];

if (self.commands[keyword]) {
self[kBufferedCommandSymbol] =
StringPrototypeSlice(cmd, 0, lastNewlineIndex + 1);
trimmedCmd = trimmedCommandLine;
}
}
}

// Check to see if a REPL keyword was used. If it returns true,
// display next prompt and return.
Expand Down
46 changes: 46 additions & 0 deletions test/parallel/test-repl-multiline.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const assert = require('assert');
const { startNewREPLServer } = require('../common/repl');

const input = ['const foo = {', '};', 'foo'];
const dotCommandSyntaxError =
/Uncaught SyntaxError: Unexpected token '\.'/;

function run({ useColors }) {
const { replServer, output } = startNewREPLServer({ useColors });
Expand All @@ -27,3 +29,47 @@ function run({ useColors }) {

run({ useColors: true });
run({ useColors: false });

function runDotCommandAfterRecoverable(command, validate) {
const { replServer, output } = startNewREPLServer();

replServer.on('exit', common.mustCall());
replServer.write('function a() {\n');
replServer.write(`${command}\n`);

assert.doesNotMatch(output.accumulator, dotCommandSyntaxError);
validate(replServer, output);

replServer.close();
}

runDotCommandAfterRecoverable('.break', (replServer, output) => {
replServer.write('1 + 1\n');
assert.doesNotMatch(output.accumulator, dotCommandSyntaxError);
assert.match(output.accumulator, /2\n/);
});

runDotCommandAfterRecoverable('.help', (replServer, output) => {
assert.match(output.accumulator, /\.break\s+Sometimes you get stuck/);
assert.match(output.accumulator, /\.help\s+Print this help message/);

replServer.write('.break\n');
replServer.write('1 + 1\n');

assert.doesNotMatch(output.accumulator, dotCommandSyntaxError);
assert.match(output.accumulator, /2\n/);
});

{
const { replServer, output } = startNewREPLServer();
let exited = false;

replServer.on('exit', common.mustCall(() => {
exited = true;
}));
replServer.write('function a() {\n');
replServer.write('.exit\n');

assert.strictEqual(exited, true);
assert.doesNotMatch(output.accumulator, dotCommandSyntaxError);
}
Loading