Skip to content

Commit feb77ea

Browse files
committed
Fix REPL for named functions
add some tests.
1 parent b563717 commit feb77ea

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/repl.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,17 @@ function REPLServer(prompt, stream) {
115115
// and statements e.g.
116116
// 'for (var i = 0; i < 10; i++) console.log(i);'
117117

118-
var ret;
118+
var ret, success = false;
119119
try {
120120
// First we attempt to eval as expression with parens.
121121
// This catches '{a : 1}' properly.
122122
ret = vm.runInContext('(' + self.bufferedCommand + ')', context, 'repl');
123+
if (typeof ret !== 'function') success = true;
123124
} catch (e) {
125+
success = false;
126+
}
127+
128+
if (!success) {
124129
// Now as statement without parens.
125130
ret = vm.runInContext(self.bufferedCommand, context, 'repl');
126131
}

test/simple/test-repl.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,26 @@ function error_test() {
9797
// invalid input to JSON.parse error is special case of syntax error,
9898
// should throw
9999
{ client: client_unix, send: 'JSON.parse(\'{invalid: \\\'json\\\'}\');',
100-
expect: /^SyntaxError: Unexpected token ILLEGAL/ }
100+
expect: /^SyntaxError: Unexpected token ILLEGAL/ },
101+
// Named functions can be used:
102+
{ client: client_unix, send: 'function blah() { return 1; }',
103+
expect: prompt_unix },
104+
{ client: client_unix, send: 'blah()',
105+
expect: "1\n" + prompt_unix },
106+
// Multiline object
107+
{ client: client_unix, send: '{ a: ',
108+
expect: prompt_multiline },
109+
{ client: client_unix, send: '1 }',
110+
expect: "{ a: 1 }" },
111+
// Multiline anonymous function with comment
112+
{ client: client_unix, send: '(function () {',
113+
expect: prompt_multiline },
114+
{ client: client_unix, send: '// blah',
115+
expect: prompt_multiline },
116+
{ client: client_unix, send: 'return 1;',
117+
expect: prompt_multiline },
118+
{ client: client_unix, send: '})()',
119+
expect: "1" },
101120
]);
102121
}
103122

0 commit comments

Comments
 (0)