Skip to content

Commit 9099664

Browse files
not-an-aardvarkaddaleax
authored andcommitted
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: #9852 Fixes: #9850 Reviewed-By: Prince John Wesley <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent c286312 commit 9099664

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/repl.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,10 @@ function REPLServer(prompt,
576576
self.wrappedCmd = true;
577577
} else {
578578
// Mitigate https://github.com/nodejs/node/issues/548
579-
cmd = cmd.replace(/^\s*function\s+([^(]+)/,
580-
(_, name) => `var ${name} = function ${name}`);
579+
cmd = cmd.replace(
580+
/^\s*function(?:\s*(\*)\s*|\s+)([^(]+)/,
581+
(_, genStar, name) => `var ${name} = function ${genStar || ''}${name}`
582+
);
581583
}
582584
// Append a \n so that it will be either
583585
// terminated, or continued onto the next expression if it's an

test/parallel/test-repl.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,19 @@ function error_test() {
339339
// Avoid emitting stack trace
340340
{ client: client_unix, send: 'a = 3.5e',
341341
expect: /^(?!\s+at\s)/gm },
342+
343+
// https://github.com/nodejs/node/issues/9850
344+
{ client: client_unix, send: 'function* foo() {}; foo().next();',
345+
expect: '{ value: undefined, done: true }' },
346+
347+
{ client: client_unix, send: 'function *foo() {}; foo().next();',
348+
expect: '{ value: undefined, done: true }' },
349+
350+
{ client: client_unix, send: 'function*foo() {}; foo().next();',
351+
expect: '{ value: undefined, done: true }' },
352+
353+
{ client: client_unix, send: 'function * foo() {}; foo().next()',
354+
expect: '{ value: undefined, done: true }' },
342355
]);
343356
}
344357

0 commit comments

Comments
 (0)