Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: some fixes in repl.md #10244

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions doc/api/repl.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ The default evaluator supports direct evaluation of JavaScript expressions:
```js
> 1 + 1
2
> var m = 2
> const m = 2
undefined
> m + 1
3
```

Unless otherwise scoped within blocks (e.g. `{ ... }`) or functions, variables
declared either implicitly or using the `var` keyword are declared at the
`global` scope.
Unless otherwise scoped within blocks or functions, variables declared
either implicitly or using the `const`, `let`, or `var` keywords
are declared at the global scope.

#### Global and Local Scope

Expand All @@ -96,7 +96,7 @@ it to the `context` object associated with each `REPLServer`. For example:

```js
const repl = require('repl');
var msg = 'message';
const msg = 'message';

repl.start('> ').context.m = msg;
```
Expand All @@ -115,7 +115,7 @@ To specify read-only globals, context properties must be defined using

```js
const repl = require('repl');
var msg = 'message';
const msg = 'message';

const r = repl.start('> ');
Object.defineProperty(r.context, 'm', {
Expand All @@ -140,18 +140,22 @@ global or scoped variable, the input `fs` will be evaluated on-demand as

The default evaluator will, by default, assign the result of the most recently
evaluated expression to the special variable `_` (underscore).
Explicitly setting `_` to a value will disable this behavior.

```js
> [ 'a', 'b', 'c' ]
[ 'a', 'b', 'c' ]
> _.length
3
> _ += 1
Expression assignment to _ now disabled.
4
> 1 + 1
2
> _
4
```

Explicitly setting `_` to a value will disable this behavior.

### Custom Evaluation Functions

When a new `repl.REPLServer` is created, a custom evaluation function may be
Expand Down Expand Up @@ -182,8 +186,8 @@ multi-line input, the eval function can return an instance of `repl.Recoverable`
to the provided callback function:

```js
function eval(cmd, context, filename, callback) {
var result;
function myEval(cmd, context, filename, callback) {
let result;
try {
result = vm.runInThisContext(cmd);
} catch (e) {
Expand Down Expand Up @@ -217,10 +221,10 @@ following example, for instance, simply converts any input text to upper case:
```js
const repl = require('repl');

const r = repl.start({prompt: '>', eval: myEval, writer: myWriter});
const r = repl.start({prompt: '> ', eval: myEval, writer: myWriter});

function myEval(cmd, context, filename, callback) {
callback(null,cmd);
callback(null, cmd);
}

function myWriter(output) {
Expand Down Expand Up @@ -275,7 +279,7 @@ function initializeContext(context) {
context.m = 'test';
}

var r = repl.start({prompt: '>'});
const r = repl.start({prompt: '> '});
initializeContext(r.context);

r.on('reset', initializeContext);
Expand All @@ -286,15 +290,15 @@ reset to its initial value using the `.clear` command:

```js
$ ./node example.js
>m
> m
'test'
>m = 1
> m = 1
1
>m
> m
1
>.clear
> .clear
Clearing context...
>m
> m
'test'
>
```
Expand All @@ -321,17 +325,17 @@ The following example shows two new commands added to the REPL instance:
```js
const repl = require('repl');

var replServer = repl.start({prompt: '> '});
const replServer = repl.start({prompt: '> '});
replServer.defineCommand('sayhello', {
help: 'Say hello',
action: function(name) {
action(name) {
this.lineParser.reset();
this.bufferedCommand = '';
console.log(`Hello, ${name}!`);
this.displayPrompt();
}
});
replServer.defineCommand('saybye', function() {
replServer.defineCommand('saybye', () => {
console.log('Goodbye!');
this.close();
});
Expand Down Expand Up @@ -372,7 +376,8 @@ added: v0.1.91
-->

* `options` {Object | String}
* `prompt` {String} The input prompt to display. Defaults to `> `.
* `prompt` {String} The input prompt to display. Defaults to `> `
(with a trailing space).
* `input` {Readable} The Readable stream from which REPL input will be read.
Defaults to `process.stdin`.
* `output` {Writable} The Writable stream to which REPL output will be
Expand Down Expand Up @@ -430,7 +435,7 @@ without passing any arguments (or by passing the `-i` argument):

```js
$ node
> a = [1, 2, 3];
> const a = [1, 2, 3];
[ 1, 2, 3 ]
> a.forEach((v) => {
... console.log(v);
Expand Down Expand Up @@ -502,7 +507,7 @@ socket, and a TCP socket:
```js
const net = require('net');
const repl = require('repl');
var connections = 0;
let connections = 0;

repl.start({
prompt: 'Node.js via stdin> ',
Expand Down Expand Up @@ -544,10 +549,11 @@ possible to connect to a long-running Node.js process without restarting it.
For an example of running a "full-featured" (`terminal`) REPL over
a `net.Server` and `net.Socket` instance, see: https://gist.github.com/2209310

For an example of running a REPL instance over curl(1),
For an example of running a REPL instance over [curl(1)][],
see: https://gist.github.com/2053342

[stream]: stream.html
[`util.inspect()`]: util.html#util_util_inspect_object_options
[`readline.Interface`]: readline.html#readline_class_interface
[`readline.InterfaceCompleter`]: readline.html#readline_use_of_the_completer_function
[curl(1)]: https://curl.haxx.se/docs/manpage.html