mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
Fix REPL for named functions
add some tests.
This commit is contained in:
1 parent
b5637170b1
commit
feb77eab65
2 files changed
+26
-2
No files matched your search
+6
-1
@@ -115,12 +115,17 @@ function REPLServer(prompt, stream) {
|
||||
// and statements e.g.
|
||||
// 'for (var i = 0; i < 10; i++) console.log(i);'
|
||||
|
||||
var ret;
|
||||
var ret, success = false;
|
||||
try {
|
||||
// First we attempt to eval as expression with parens.
|
||||
// This catches '{a : 1}' properly.
|
||||
ret = vm.runInContext('(' + self.bufferedCommand + ')', context, 'repl');
|
||||
if (typeof ret !== 'function') success = true;
|
||||
} catch (e) {
|
||||
success = false;
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
// Now as statement without parens.
|
||||
ret = vm.runInContext(self.bufferedCommand, context, 'repl');
|
||||
}
|
||||
|
||||
@@ -97,7 +97,26 @@ function error_test() {
|
||||
// invalid input to JSON.parse error is special case of syntax error,
|
||||
// should throw
|
||||
{ client: client_unix, send: 'JSON.parse(\'{invalid: \\\'json\\\'}\');',
|
||||
expect: /^SyntaxError: Unexpected token ILLEGAL/ }
|
||||
expect: /^SyntaxError: Unexpected token ILLEGAL/ },
|
||||
// Named functions can be used:
|
||||
{ client: client_unix, send: 'function blah() { return 1; }',
|
||||
expect: prompt_unix },
|
||||
{ client: client_unix, send: 'blah()',
|
||||
expect: "1\n" + prompt_unix },
|
||||
// Multiline object
|
||||
{ client: client_unix, send: '{ a: ',
|
||||
expect: prompt_multiline },
|
||||
{ client: client_unix, send: '1 }',
|
||||
expect: "{ a: 1 }" },
|
||||
// Multiline anonymous function with comment
|
||||
{ client: client_unix, send: '(function () {',
|
||||
expect: prompt_multiline },
|
||||
{ client: client_unix, send: '// blah',
|
||||
expect: prompt_multiline },
|
||||
{ client: client_unix, send: 'return 1;',
|
||||
expect: prompt_multiline },
|
||||
{ client: client_unix, send: '})()',
|
||||
expect: "1" },
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user