-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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 #10160
doc: some fixes in repl.md #10160
Changes from 7 commits
9615910
3e18789
743f84d
a7501f2
402c625
e20c785
4357059
9c7054b
cdeb85e
da077ee
22e2c47
a244147
1b25214
61d6293
ac78812
b0c10a2
586b787
df4018e
29b2e88
c6eae5a
8960383
83d9cd8
fd6999e
57f993d
e6a0c39
558fa1c
deb9cc0
68488e9
1e4b9a1
2d0ce51
6967ed4
df39784
a0a6ff2
6dd0754
9a5d475
f0da38a
444b907
6aacef7
a8137dd
def6dfb
b8fc9a3
bc335c0
f9aadfb
d8c7534
3d24856
cffbb32
0cd1f54
4d11c2c
7c2dbd1
aa77b76
8dbf1af
8621ccc
f5c2c8c
9f58e02
a84017a
5607228
14b0b44
10929f6
f418a22
9ee915b
d4f00fe
c3839f7
d4050b3
475b8db
a28e949
caa7fa9
db50307
fa4f158
a8e8708
499fc7a
50cb3a3
7346e55
10891a1
8bad37a
5f18b40
bed9aae
9bef034
67c2a7d
d665e39
5f31da7
d5861ad
0c32eb5
936263e
588a5d5
10c0137
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,15 +78,16 @@ 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. | ||
declared either implicitly or using the `const` or `let` keywords | ||
are declared at the `global` scope (as well as with the `var` keyword | ||
outside of functions). | ||
|
||
#### Global and Local Scope | ||
|
||
|
@@ -96,7 +97,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; | ||
``` | ||
|
@@ -115,7 +116,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', { | ||
|
@@ -140,18 +141,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 | ||
|
@@ -182,8 +187,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) { | ||
|
@@ -217,10 +222,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) { | ||
|
@@ -275,7 +280,7 @@ function initializeContext(context) { | |
context.m = 'test'; | ||
} | ||
|
||
var r = repl.start({prompt: '>'}); | ||
const r = repl.start({prompt: '> '}); | ||
initializeContext(r.context); | ||
|
||
r.on('reset', initializeContext); | ||
|
@@ -286,15 +291,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' | ||
> | ||
``` | ||
|
@@ -321,17 +326,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: function sayHello(name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't add any value. I'd rather just leave at is it, but do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've just thought the docs should promote the style appropriate for the code base: https://github.com/nodejs/node/pulls?q=is%3Apr+name+anonymous+functions+is%3Aclosed But if I'm wrong, I'll roll back these two with just space insertion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand. Also I do not have strong feelings about this, but in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Well, if nobody will support the additions, I will change as you advise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, but I think we only try to do that in cases where the name is not inferred (well) by V8. Here, If you want to change style here, I’d be +1 on just the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider also a method shorthand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. O, I have not seen @addaleax comment) |
||
this.lineParser.reset(); | ||
this.bufferedCommand = ''; | ||
console.log(`Hello, ${name}!`); | ||
this.displayPrompt(); | ||
} | ||
}); | ||
replServer.defineCommand('saybye', function() { | ||
replServer.defineCommand('saybye', function sayBye() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider also an arrow function. |
||
console.log('Goodbye!'); | ||
this.close(); | ||
}); | ||
|
@@ -371,8 +376,9 @@ within the action function for commands registered using the | |
added: v0.1.91 | ||
--> | ||
|
||
* `options` {Object} | ||
* `prompt` {String} The input prompt to display. Defaults to `> `. | ||
* `options` {Object | String} | ||
* `prompt` {String} The input prompt to display. Defaults to `> ` | ||
(with a trailing space). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (...)-content doesn't add any value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The trailing space is not shown in the rendered page, it is wiped out. Is this not somehow confusing? |
||
* `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 | ||
|
@@ -411,6 +417,8 @@ added: v0.1.91 | |
`SIGINT` is received, i.e. `Ctrl+C` is pressed. This cannot be used together | ||
with a custom `eval` function. Defaults to `false`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add reference to your new name here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
|
||
If `options` is a string, then it specifies the input prompt. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't know whether this is true. If so it would be a major change in documentation, since also the type is just displaying There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it is almost documented in some examples. See: https://github.com/nodejs/node/blame/master/doc/api/repl.md#L101 If it is not official API, maybe we should change these fragments too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually how it works, although I don't know how "official" it is. See |
||
The `repl.start()` method creates and starts a `repl.REPLServer` instance. | ||
|
||
## The Node.js REPL | ||
|
@@ -421,7 +429,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); | ||
|
@@ -493,7 +501,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> ', | ||
|
@@ -535,10 +543,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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a cumbersome distinction between
const
andvar
, and `let. Couldn't it just be something like this?Especially since the phrase is immediately preceded by "Unless otherwise scoped within blocks or functions".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lance But it is exactly this remark — "Unless otherwise scoped within blocks or functions" — that prevents placing
const
,var
, andlet
in the same list:var
is not block scoped. Maybe you or somebody else could propose the whole clear concise sentence there? I am not a native speaker, so I am a bit clumsy there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
const
andlet
declarations are block scoped, and thevar
declaration is function scoped. I think this sentence covers both. I'm not sure if I have a wording that is clearer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. Done.