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

Fix REPL bug with previous line carryover #1480

Merged
merged 8 commits into from Oct 6, 2021
12 changes: 11 additions & 1 deletion src/repl.ts
Expand Up @@ -208,13 +208,23 @@ export function createRepl(options: CreateReplOptions = {}) {
context: Context;
}) {
const { code, enableTopLevelAwait, context } = options;
return appendCompileAndEvalInput({
const result = appendCompileAndEvalInput({
service: service!,
state,
input: code,
enableTopLevelAwait,
context,
});
// A semicolon is added to make sure that the code doesn't interact with the next line,
// for example to prevent `2\n+ 2` from producing 4.
// This is safe since the output will not change since we can only get here with successful inputs,
// and adding a semicolon to the end of a successful input won't ever change the output.
// We check to make sure the previous line ends in a newline just in case, even though it should
// always be the case.
if (/\n$/.test(state.input)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

A detailed explanation within the code! Love it; thanks for this.

state.input = state.input.slice(0, state.input.length - 1) + ';\n';
}
return result;
}

function nodeEval(
Expand Down