Browse Source

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>
v6
Teddy Katz 8 years ago
parent
commit
24a98dd810
No known key found for this signature in database GPG Key ID: B79EC7ABC0AA53A0
  1. 6
      lib/repl.js
  2. 13
      test/parallel/test-repl.js

6
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

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

Loading…
Cancel
Save