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>
v7.x
Teddy Katz 8 years ago
committed by Anna Henningsen
parent
commit
9099664959
No known key found for this signature in database GPG Key ID: D8B9F5AEAE84E4CF
  1. 6
      lib/repl.js
  2. 13
      test/parallel/test-repl.js

6
lib/repl.js

@ -576,8 +576,10 @@ function REPLServer(prompt,
self.wrappedCmd = true; self.wrappedCmd = true;
} else { } else {
// Mitigate https://github.com/nodejs/node/issues/548 // Mitigate https://github.com/nodejs/node/issues/548
cmd = cmd.replace(/^\s*function\s+([^(]+)/, cmd = cmd.replace(
(_, name) => `var ${name} = function ${name}`); /^\s*function(?:\s*(\*)\s*|\s+)([^(]+)/,
(_, genStar, name) => `var ${name} = function ${genStar || ''}${name}`
);
} }
// Append a \n so that it will be either // Append a \n so that it will be either
// terminated, or continued onto the next expression if it's an // 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 // Avoid emitting stack trace
{ client: client_unix, send: 'a = 3.5e', { client: client_unix, send: 'a = 3.5e',
expect: /^(?!\s+at\s)/gm }, 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