Browse Source

repl: preprocess only for defaultEval

Code preprocessing is applicable only for default
eval function. Therefore, Moved `preprocess` function
invocation inside `defaultEval` function.

Fixes: https://github.com/nodejs/node/issues/9743
PR-URL: https://github.com/nodejs/node/pull/9752
Reviewed-By: Anna Henningsen <anna@addaleax.net>
v6
Prince J Wesley 8 years ago
parent
commit
9366b8d90a
No known key found for this signature in database GPG Key ID: 9D76A33B02A4C46B
  1. 51
      lib/repl.js
  2. 10
      test/parallel/test-repl-eval.js

51
lib/repl.js

@ -238,7 +238,33 @@ function REPLServer(prompt,
eval_ = eval_ || defaultEval; eval_ = eval_ || defaultEval;
function preprocess(code) {
let cmd = code;
if (/^\s*\{/.test(cmd) && /\}\s*$/.test(cmd)) {
// It's confusing for `{ a : 1 }` to be interpreted as a block
// statement rather than an object literal. So, we first try
// to wrap it in parentheses, so that it will be interpreted as
// an expression.
cmd = `(${cmd})`;
self.wrappedCmd = true;
} else {
// Mitigate https://github.com/nodejs/node/issues/548
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
// unexpected end of input.
return `${cmd}\n`;
}
function defaultEval(code, context, file, cb) { function defaultEval(code, context, file, cb) {
// Remove trailing new line
code = code.replace(/\n$/, '');
code = preprocess(code);
var err, result, retry = false, input = code, wrappedErr; var err, result, retry = false, input = code, wrappedErr;
// first, create the Script object to check the syntax // first, create the Script object to check the syntax
@ -499,8 +525,7 @@ function REPLServer(prompt,
} }
} }
var evalCmd = self.bufferedCommand + cmd; const evalCmd = self.bufferedCommand + cmd + '\n';
evalCmd = preprocess(evalCmd);
debug('eval %j', evalCmd); debug('eval %j', evalCmd);
self.eval(evalCmd, self.context, 'repl', finish); self.eval(evalCmd, self.context, 'repl', finish);
@ -557,28 +582,6 @@ function REPLServer(prompt,
// Display prompt again // Display prompt again
self.displayPrompt(); self.displayPrompt();
} }
function preprocess(code) {
let cmd = code;
if (/^\s*\{/.test(cmd) && /\}\s*$/.test(cmd)) {
// It's confusing for `{ a : 1 }` to be interpreted as a block
// statement rather than an object literal. So, we first try
// to wrap it in parentheses, so that it will be interpreted as
// an expression.
cmd = `(${cmd})`;
self.wrappedCmd = true;
} else {
// Mitigate https://github.com/nodejs/node/issues/548
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
// unexpected end of input.
return `${cmd}\n`;
}
}); });
self.on('SIGCONT', function onSigCont() { self.on('SIGCONT', function onSigCont() {

10
test/parallel/test-repl-eval.js

@ -10,7 +10,8 @@ const repl = require('repl');
eval: common.mustCall((cmd, context) => { eval: common.mustCall((cmd, context) => {
// Assertions here will not cause the test to exit with an error code // Assertions here will not cause the test to exit with an error code
// so set a boolean that is checked in process.on('exit',...) instead. // so set a boolean that is checked in process.on('exit',...) instead.
evalCalledWithExpectedArgs = (cmd === 'foo\n' && context.foo === 'bar'); evalCalledWithExpectedArgs = (cmd === 'function f() {}\n' &&
context.foo === 'bar');
}) })
}; };
@ -18,7 +19,12 @@ const repl = require('repl');
r.context = {foo: 'bar'}; r.context = {foo: 'bar'};
try { try {
r.write('foo\n'); // Default preprocessor transforms
// function f() {} to
// var f = function f() {}
// Test to ensure that original input is preserved.
// Reference: https://github.com/nodejs/node/issues/9743
r.write('function f() {}\n');
} finally { } finally {
r.write('.exit\n'); r.write('.exit\n');
} }

Loading…
Cancel
Save