
As you can see in the attached image, generators can only be specified with the star placed right next to the function keyword, as if it is placed in another place (right before the function name for example), it enters in multi-line edition mode.
It looks like this affects v6+, but not v4.
@not-an-aardvark its a preprocess issue
@not-an-aardvark this may help
node 🙈 ₹ git:(upstream ⚡ repl.9743) git diff
diff --git a/lib/repl.js b/lib/repl.js
index 70428ce..ff37cfb 100644
--- a/lib/repl.js
+++ b/lib/repl.js
@@ -249,8 +249,8 @@ function REPLServer(prompt,
self.wrappedCmd = true;
} else {
// Mitigate https://github.com/nodejs/node/issues/548
- cmd = cmd.replace(/^\s*function\s+([^(]+)/,
- (_, name) => `var ${name} = function ${name}`);
+ cmd = cmd.replace(/^\s*function(\s+(?:\*\s*)?|\*\s+)([^(]+)/,
+ (_, gen, name) => `var ${name} = function${gen}${name}`);
}
// Append a \n so that it will be either
// terminated, or continued onto the next expression if it's an
node 🙈 ₹ git:(upstream ⚡ repl.9743)
@princejwesley Thanks, that's very helpful. However, I think your diff fails with:
function*foo() {}
because whitespace is not required before/after a generator star.
I think I have a fix; I'll make a PR shortly.
@not-an-aardvark Remember to land your PR after this
While fixing this, I noticed another bug:
function foo() {} foo()
This is a SyntaxError in the REPL, but it's valid JS. It occurs because it gets preprocessed to
var foo = function foo() {} foo()
...which is a syntax error.
To fix this, we need to put a semicolon after the var foo = function foo() {} declaration. I'll add another commit to the PR.
@not-an-aardvark User input can be a partial/multi-line function. We can't insert semicolon here. Here is one more edge case
Ah, good point. Okay, I'll just submit the PR with the generator function fix then.
Most helpful comment
@not-an-aardvark its a preprocess issue