Node: Generators in REPL can't be specified using function *gen() {}

Created on 30 Nov 2016  Â·  8Comments  Â·  Source: nodejs/node

  • Version: v7.2.0
  • Platform: Linux gwg-nb01 4.8.0-1-amd64 #1 SMP Debian 4.8.7-1 (2016-11-13) x86_64 GNU/Linux


image

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.

confirmed-bug repl

Most helpful comment

@not-an-aardvark its a preprocess issue

All 8 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seishun picture seishun  Â·  3Comments

addaleax picture addaleax  Â·  3Comments

srl295 picture srl295  Â·  3Comments

stevenvachon picture stevenvachon  Â·  3Comments

dfahlander picture dfahlander  Â·  3Comments