-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Generators in REPL can't be specified using function *gen() {} #9850
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 |
@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. |
Function declarations in the REPL are preprocessed into variable declarations before being evaluated. However, the preprocessing logic did not account for the star in a generator function declaration, which caused the preprocessor to output invalid syntax in some circumstances. PR-URL: nodejs#9852 Fixes: nodejs#9850 Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
Function declarations in the REPL are preprocessed into variable declarations before being evaluated. However, the preprocessing logic did not account for the star in a generator function declaration, which caused the preprocessor to output invalid syntax in some circumstances. PR-URL: #9852 Fixes: #9850 Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
Function declarations in the REPL are preprocessed into variable declarations before being evaluated. However, the preprocessing logic did not account for the star in a generator function declaration, which caused the preprocessor to output invalid syntax in some circumstances. PR-URL: nodejs#9852 Fixes: nodejs#9850 Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
Function declarations in the REPL are preprocessed into variable declarations before being evaluated. However, the preprocessing logic did not account for the star in a generator function declaration, which caused the preprocessor to output invalid syntax in some circumstances. PR-URL: #9852 Fixes: #9850 Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
Function declarations in the REPL are preprocessed into variable declarations before being evaluated. However, the preprocessing logic did not account for the star in a generator function declaration, which caused the preprocessor to output invalid syntax in some circumstances. PR-URL: #9852 Fixes: #9850 Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
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.
The text was updated successfully, but these errors were encountered: