Skip to content

Commit

Permalink
repl: fix generator function preprocessing
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
not-an-aardvark authored and addaleax committed Dec 5, 2016
1 parent c286312 commit 9099664
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,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
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }' },
]);
}

Expand Down

0 comments on commit 9099664

Please sign in to comment.