@@ -262,8 +262,10 @@ the following values:
262
262
have ANSI/VT100 escape codes written to it. Defaults to checking ` isTTY `
263
263
on the ` output ` stream upon instantiation.
264
264
265
- - ` eval ` - function that will be used to eval each given line. Defaults to
266
- an async wrapper for ` eval() ` . See below for an example of a custom ` eval ` .
265
+ - ` eval ` - a function that will be used to eval each given line. Defaults to
266
+ an async wrapper for ` eval() ` . An ` eval ` function can error with
267
+ ` repl.Recoverable ` to indicate the code was incomplete and prompt for more
268
+ lines. See below for an example of a custom ` eval ` .
267
269
268
270
- ` useColors ` - a boolean which specifies whether or not the ` writer ` function
269
271
should output colors. If a different ` writer ` function is set then this does
@@ -287,11 +289,28 @@ the following values:
287
289
* ` repl.REPL_MODE_MAGIC ` - attempt to run commands in default mode. If they
288
290
fail to parse, re-try in strict mode.
289
291
290
- You can use your own ` eval ` function if it has following signature :
292
+ It is possible to use a custom ` eval ` function as illustrated below :
291
293
292
- function eval(cmd, context, filename, callback) {
293
- callback(null, result);
294
+ ``` js
295
+ function eval (cmd , context , filename , callback ) {
296
+ var result;
297
+ try {
298
+ result = vm .runInThisContext (cmd);
299
+ } catch (e) {
300
+ if (isRecoverableError (e)) {
301
+ return callback (new repl.Recoverable (e));
294
302
}
303
+ }
304
+ callback (null , result);
305
+ }
306
+
307
+ function isRecoverableError (error ) {
308
+ if (error .name === ' SyntaxError' ) {
309
+ return / ^ (Unexpected end of input| Unexpected token)/ .test (error .message );
310
+ }
311
+ return false ;
312
+ }
313
+ ```
295
314
296
315
On tab completion, ` eval ` will be called with ` .scope ` as an input string. It
297
316
is expected to return an array of scope names to be used for the auto-completion.
0 commit comments