repl: fix generator function preprocessing

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 <princejohnwesley@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
Teddy Katz 2016-11-30 02:12:35 -05:00
parent b49b496a92
commit 24a98dd810
No known key found for this signature in database
GPG Key ID: B79EC7ABC0AA53A0
2 changed files with 17 additions and 2 deletions

View File

@ -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

View File

@ -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 }' },
]);
}