From 24a98dd810d0713d736fb82aec284773b855064e Mon Sep 17 00:00:00 2001 From: Teddy Katz Date: Wed, 30 Nov 2016 02:12:35 -0500 Subject: [PATCH] repl: fix generator function preprocessing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/9852 Fixes: https://github.com/nodejs/node/issues/9850 Reviewed-By: Prince John Wesley Reviewed-By: Michaƫl Zasso --- lib/repl.js | 6 ++++-- test/parallel/test-repl.js | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index fe30bdd57b..8bddfb5e3a 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -569,8 +569,10 @@ 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+)([^(]+)/, + (_, genStar, name) => `var ${name} = function ${genStar || ''}${name}` + ); } // Append a \n so that it will be either // terminated, or continued onto the next expression if it's an diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 29c85e3607..fd540e77c5 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -339,6 +339,19 @@ function error_test() { // Avoid emitting stack trace { client: client_unix, send: 'a = 3.5e', expect: /^(?!\s+at\s)/gm }, + + // https://github.com/nodejs/node/issues/9850 + { client: client_unix, send: 'function* foo() {}; foo().next();', + expect: '{ value: undefined, done: true }' }, + + { client: client_unix, send: 'function *foo() {}; foo().next();', + expect: '{ value: undefined, done: true }' }, + + { client: client_unix, send: 'function*foo() {}; foo().next();', + expect: '{ value: undefined, done: true }' }, + + { client: client_unix, send: 'function * foo() {}; foo().next()', + expect: '{ value: undefined, done: true }' }, ]); }