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, tools: use eslint-plugin-markdown #12563

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ test/tmp*/
tools/eslint
node_modules
benchmark/tmp/
doc/**/*.js
3 changes: 3 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
root: true

plugins:
- markdown

env:
node: true
es6: true
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,8 @@ bench-ci: bench

jslint:
@echo "Running JS linter..."
$(NODE) tools/eslint/bin/eslint.js --cache --rulesdir=tools/eslint-rules \
benchmark lib test tools
$(NODE) tools/eslint/bin/eslint.js --cache --rulesdir=tools/eslint-rules --ext=.js,.md \
benchmark doc lib test tools

jslint-ci:
@echo "Running JS linter..."
Expand Down
12 changes: 12 additions & 0 deletions doc/.eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Docs-specific linter rules

rules:
# ease some restrictions in doc examples
no-restricted-properties: 0
no-undef: 0
no-unused-vars: 0
strict: 0

# add new ECMAScript features gradually
no-var: 2
prefer-const: 2
31 changes: 16 additions & 15 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ are evaluated also:
const assert = require('assert');

const obj1 = {
a : {
b : 1
a: {
b: 1
}
};
const obj2 = {
a : {
b : 2
a: {
b: 2
}
};
const obj3 = {
a : {
b : 1
a: {
b: 1
}
};
const obj4 = Object.create(obj1);
Expand Down Expand Up @@ -322,18 +322,18 @@ Tests for any deep inequality. Opposite of [`assert.deepEqual()`][].
const assert = require('assert');

const obj1 = {
a : {
b : 1
a: {
b: 1
}
};
const obj2 = {
a : {
b : 2
a: {
b: 2
}
};
const obj3 = {
a : {
b : 1
a: {
b: 1
}
};
const obj4 = Object.create(obj1);
Expand Down Expand Up @@ -368,10 +368,10 @@ Tests for deep strict inequality. Opposite of [`assert.deepStrictEqual()`][].
```js
const assert = require('assert');

assert.notDeepEqual({a:1}, {a:'1'});
assert.notDeepEqual({a: 1}, {a: '1'});
// AssertionError: { a: 1 } notDeepEqual { a: '1' }

assert.notDeepStrictEqual({a:1}, {a:'1'});
assert.notDeepStrictEqual({a: 1}, {a: '1'});
// OK
```

Expand Down Expand Up @@ -542,7 +542,7 @@ assert.throws(
throw new Error('Wrong value');
},
function(err) {
if ( (err instanceof Error) && /value/.test(err) ) {
if ((err instanceof Error) && /value/.test(err)) {
return true;
}
},
Expand All @@ -554,6 +554,7 @@ Note that `error` can not be a string. If a string is provided as the second
argument, then `error` is assumed to be omitted and the string will be used for
`message` instead. This can lead to easy-to-miss mistakes:

<!-- eslint-disable assert-throws-arguments -->
```js
// THIS IS A MISTAKE! DO NOT DO THIS!
assert.throws(myFunction, 'missing foo', 'did not throw with expected message');
Expand Down
16 changes: 8 additions & 8 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ Example: Copy an ASCII string into a `Buffer`, one byte at a time
const str = 'Node.js';
const buf = Buffer.allocUnsafe(str.length);

for (let i = 0; i < str.length ; i++) {
for (let i = 0; i < str.length; i++) {
buf[i] = str.charCodeAt(i);
}

Expand Down Expand Up @@ -1087,7 +1087,7 @@ byte 16 through byte 19 into `buf2`, starting at the 8th byte in `buf2`
const buf1 = Buffer.allocUnsafe(26);
const buf2 = Buffer.allocUnsafe(26).fill('!');

for (let i = 0 ; i < 26 ; i++) {
for (let i = 0; i < 26; i++) {
// 97 is the decimal ASCII value for 'a'
buf1[i] = i + 97;
}
Expand All @@ -1104,7 +1104,7 @@ overlapping region within the same `Buffer`
```js
const buf = Buffer.allocUnsafe(26);

for (let i = 0 ; i < 26 ; i++) {
for (let i = 0; i < 26; i++) {
// 97 is the decimal ASCII value for 'a'
buf[i] = i + 97;
}
Expand Down Expand Up @@ -1871,7 +1871,7 @@ one byte from the original `Buffer`
```js
const buf1 = Buffer.allocUnsafe(26);

for (let i = 0 ; i < 26 ; i++) {
for (let i = 0; i < 26; i++) {
// 97 is the decimal ASCII value for 'a'
buf1[i] = i + 97;
}
Expand Down Expand Up @@ -2021,9 +2021,9 @@ const json = JSON.stringify(buf);
console.log(json);

const copy = JSON.parse(json, (key, value) => {
return value && value.type === 'Buffer'
? Buffer.from(value.data)
: value;
return value && value.type === 'Buffer' ?
Buffer.from(value.data) :
value;
});

// Prints: <Buffer 01 02 03 04 05>
Expand All @@ -2049,7 +2049,7 @@ Examples:
```js
const buf1 = Buffer.allocUnsafe(26);

for (let i = 0 ; i < 26 ; i++) {
for (let i = 0; i < 26; i++) {
// 97 is the decimal ASCII value for 'a'
buf1[i] = i + 97;
}
Expand Down
22 changes: 14 additions & 8 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ bat.stderr.on('data', (data) => {
bat.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
});
```

```js
// OR...
const exec = require('child_process').exec;
exec('my.bat', (err, stdout, stderr) => {
Expand Down Expand Up @@ -195,14 +197,14 @@ The `options` argument may be passed as the second argument to customize how
the process is spawned. The default options are:

```js
{
const defaults = {
encoding: 'utf8',
timeout: 0,
maxBuffer: 200*1024,
maxBuffer: 200 * 1024,
killSignal: 'SIGTERM',
cwd: null,
env: null
}
};
```

If `timeout` is greater than `0`, the parent will send the signal
Expand Down Expand Up @@ -362,10 +364,10 @@ trigger arbitrary command execution.**
A third argument may be used to specify additional options, with these defaults:

```js
{
const defaults = {
cwd: undefined,
env: process.env
}
};
```

Use `cwd` to specify the working directory from which the process is spawned.
Expand Down Expand Up @@ -931,13 +933,17 @@ as in this example:
'use strict';
const spawn = require('child_process').spawn;

const child = spawn('sh', ['-c',
`node -e "setInterval(() => {
const child = spawn(
'sh',
[
'-c',
`node -e "setInterval(() => {
console.log(process.pid, 'is alive')
}, 500);"`
], {
stdio: ['inherit', 'inherit', 'inherit']
});
}
);

setTimeout(() => {
child.kill(); // does not terminate the node process in the shell
Expand Down
2 changes: 1 addition & 1 deletion doc/api/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ This can be used to restart the worker by calling `.fork()` again.
```js
cluster.on('exit', (worker, code, signal) => {
console.log('worker %d died (%s). restarting...',
worker.process.pid, signal || code);
worker.process.pid, signal || code);
cluster.fork();
});
```
Expand Down
8 changes: 5 additions & 3 deletions doc/api/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ or `console.Console`:

```js
const Console = require('console').Console;
```

```js
const Console = console.Console;
```

Expand Down Expand Up @@ -132,6 +135,7 @@ by extending Node.js' `console` and overriding the `console.assert()` method.
In the following example, a simple module is created that extends and overrides
the default behavior of `console` in Node.js.

<!-- eslint-disable func-name-matching -->
```js
'use strict';

Expand Down Expand Up @@ -273,9 +277,7 @@ prints the result to `stdout`:

```js
console.time('100-elements');
for (let i = 0; i < 100; i++) {
;
}
for (let i = 0; i < 100; i++) {}
console.timeEnd('100-elements');
// prints 100-elements: 225.438ms
```
Expand Down
6 changes: 4 additions & 2 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ decipher.on('end', () => {
// Prints: some clear text data
});

const encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
const encrypted =
'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
decipher.write(encrypted, 'hex');
decipher.end();
```
Expand All @@ -312,7 +313,8 @@ Example: Using the [`decipher.update()`][] and [`decipher.final()`][] methods:
const crypto = require('crypto');
const decipher = crypto.createDecipher('aes192', 'a password');

const encrypted = 'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
const encrypted =
'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
console.log(decrypted);
Expand Down
1 change: 1 addition & 0 deletions doc/api/debugger.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ inspection are possible.
Inserting the statement `debugger;` into the source code of a script will
enable a breakpoint at that position in the code:

<!-- eslint-disable no-debugger -->
```js
// myscript.js
global.x = 5;
Expand Down
4 changes: 2 additions & 2 deletions doc/api/dgram.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ server.on('message', (msg, rinfo) => {
});

server.on('listening', () => {
var address = server.address();
const address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
});

Expand Down Expand Up @@ -146,7 +146,7 @@ server.on('message', (msg, rinfo) => {
});

server.on('listening', () => {
var address = server.address();
const address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
});

Expand Down
3 changes: 3 additions & 0 deletions doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ function will contain an array of objects with the following properties:

For example:

<!-- eslint-disable -->
```js
{
flags: 's',
Expand Down Expand Up @@ -352,6 +353,7 @@ be an object with the following properties:
* `expire`
* `minttl`

<!-- eslint-disable -->
```js
{
nsname: 'ns.example.com',
Expand Down Expand Up @@ -382,6 +384,7 @@ be an array of objects with the following properties:
* `port`
* `name`

<!-- eslint-disable -->
```js
{
priority: 10,
Expand Down
2 changes: 1 addition & 1 deletion doc/api/domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function handleRequest(req, res) {
setTimeout(() => {
// Whoops!
flerb.bark();
});
}, timeout);
break;
default:
res.end('ok');
Expand Down
10 changes: 5 additions & 5 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ synchronous counterparts are of this type.
For a regular file [`util.inspect(stats)`][] would return a string very
similar to this:

```js
```
Stats {
dev: 2114,
ino: 48064969,
Expand Down Expand Up @@ -630,13 +630,13 @@ default value of 64 kb for the same parameter.
`options` is an object or string with the following defaults:

```js
{
const defaults = {
flags: 'r',
encoding: null,
fd: null,
mode: 0o666,
autoClose: true
}
};
```

`options` can include `start` and `end` values to read a range of bytes from
Expand Down Expand Up @@ -696,13 +696,13 @@ Returns a new [`WriteStream`][] object. (See [Writable Stream][]).
`options` is an object or string with the following defaults:

```js
{
const defaults = {
flags: 'w',
defaultEncoding: 'utf8',
fd: null,
mode: 0o666,
autoClose: true
}
};
```

`options` may also include a `start` option to allow writing data at
Expand Down
Loading