Skip to content

Commit 6ee6aae

Browse files
committed
doc: add no-var, prefer-const in doc eslintrc
PR-URL: #12563 Refs: #12557 (comment) Reviewed-By: Teddy Katz <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]>
1 parent b4fea2a commit 6ee6aae

8 files changed

+49
-42
lines changed

doc/.eslintrc.yaml

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
## Docs-specific linter rules
2+
13
rules:
2-
strict: 0
4+
# ease some restrictions in doc examples
35
no-restricted-properties: 0
46
no-undef: 0
57
no-unused-vars: 0
8+
strict: 0
9+
10+
# add new ECMAScript features gradually
11+
no-var: 2
12+
prefer-const: 2

doc/api/dgram.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ server.on('message', (msg, rinfo) => {
2020
});
2121

2222
server.on('listening', () => {
23-
var address = server.address();
23+
const address = server.address();
2424
console.log(`server listening ${address.address}:${address.port}`);
2525
});
2626

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

148148
server.on('listening', () => {
149-
var address = server.address();
149+
const address = server.address();
150150
console.log(`server listening ${address.address}:${address.port}`);
151151
});
152152

doc/api/stream.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ function writeOneMillionTimes(writer, data, encoding, callback) {
237237
let i = 1000000;
238238
write();
239239
function write() {
240-
var ok = true;
240+
let ok = true;
241241
do {
242242
i--;
243243
if (i === 0) {
@@ -869,7 +869,7 @@ the internal buffer is fully drained.
869869
```js
870870
const readable = getReadableStreamSomehow();
871871
readable.on('readable', () => {
872-
var chunk;
872+
let chunk;
873873
while (null !== (chunk = readable.read())) {
874874
console.log(`Received ${chunk.length} bytes of data.`);
875875
}
@@ -1004,14 +1004,14 @@ function parseHeader(stream, callback) {
10041004
stream.on('error', callback);
10051005
stream.on('readable', onReadable);
10061006
const decoder = new StringDecoder('utf8');
1007-
var header = '';
1007+
let header = '';
10081008
function onReadable() {
1009-
var chunk;
1009+
let chunk;
10101010
while (null !== (chunk = stream.read())) {
1011-
var str = decoder.write(chunk);
1011+
const str = decoder.write(chunk);
10121012
if (str.match(/\n\n/)) {
10131013
// found the header boundary
1014-
var split = str.split(/\n\n/);
1014+
const split = str.split(/\n\n/);
10151015
header += split.shift();
10161016
const remaining = split.join('\n\n');
10171017
const buf = Buffer.from(remaining, 'utf8');
@@ -1598,12 +1598,12 @@ class Counter extends Readable {
15981598
}
15991599

16001600
_read() {
1601-
var i = this._index++;
1601+
const i = this._index++;
16021602
if (i > this._max)
16031603
this.push(null);
16041604
else {
1605-
var str = '' + i;
1606-
var buf = Buffer.from(str, 'ascii');
1605+
const str = '' + i;
1606+
const buf = Buffer.from(str, 'ascii');
16071607
this.push(buf);
16081608
}
16091609
}

doc/api/util.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ it is marked as deprecated.
5959
const util = require('util');
6060

6161
exports.puts = util.deprecate(function() {
62-
for (var i = 0, len = arguments.length; i < len; ++i) {
62+
for (let i = 0, len = arguments.length; i < len; ++i) {
6363
process.stdout.write(arguments[i] + '\n');
6464
}
6565
}, 'util.puts: Use console.log instead');

doc/api/vm.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ const sandbox = {
116116
const script = new vm.Script('count += 1; name = "kitty";');
117117

118118
const context = new vm.createContext(sandbox);
119-
for (var i = 0; i < 10; ++i) {
119+
for (let i = 0; i < 10; ++i) {
120120
script.runInContext(context);
121121
}
122122

@@ -203,7 +203,7 @@ global.globalVar = 0;
203203

204204
const script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' });
205205

206-
for (var i = 0; i < 1000; ++i) {
206+
for (let i = 0; i < 1000; ++i) {
207207
script.runInThisContext();
208208
}
209209

@@ -231,14 +231,14 @@ will remain unchanged.
231231
const util = require('util');
232232
const vm = require('vm');
233233

234-
var globalVar = 3;
234+
global.globalVar = 3;
235235

236236
const sandbox = { globalVar: 1 };
237237
vm.createContext(sandbox);
238238

239239
vm.runInContext('globalVar *= 2;', sandbox);
240240

241-
console.log(util.inspect(sandbox)); // 2
241+
console.log(util.inspect(sandbox)); // { globalVar: 2 }
242242

243243
console.log(util.inspect(globalVar)); // 3
244244
```
@@ -296,7 +296,7 @@ const vm = require('vm');
296296
const sandbox = { globalVar: 1 };
297297
vm.createContext(sandbox);
298298

299-
for (var i = 0; i < 10; ++i) {
299+
for (let i = 0; i < 10; ++i) {
300300
vm.runInContext('globalVar *= 2;', sandbox);
301301
}
302302
console.log(util.inspect(sandbox));
@@ -399,9 +399,10 @@ local scope, but does have access to the current `global` object.
399399
The following example illustrates using both `vm.runInThisContext()` and
400400
the JavaScript [`eval()`][] function to run the same code:
401401

402+
<!-- eslint-disable prefer-const -->
402403
```js
403404
const vm = require('vm');
404-
var localVar = 'initial value';
405+
let localVar = 'initial value';
405406

406407
const vmResult = vm.runInThisContext('localVar = "vm";');
407408
console.log('vmResult:', vmResult);

doc/api/zlib.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const request = http.get({ host: 'example.com',
6969
port: 80,
7070
headers: { 'Accept-Encoding': 'gzip,deflate' } });
7171
request.on('response', (response) => {
72-
var output = fs.createWriteStream('example.com_index.html');
72+
const output = fs.createWriteStream('example.com_index.html');
7373

7474
switch (response.headers['content-encoding']) {
7575
// or, just use zlib.createUnzip() to handle both cases
@@ -94,8 +94,8 @@ const zlib = require('zlib');
9494
const http = require('http');
9595
const fs = require('fs');
9696
http.createServer((request, response) => {
97-
var raw = fs.createReadStream('index.html');
98-
var acceptEncoding = request.headers['accept-encoding'];
97+
const raw = fs.createReadStream('index.html');
98+
let acceptEncoding = request.headers['accept-encoding'];
9999
if (!acceptEncoding) {
100100
acceptEncoding = '';
101101
}

doc/guides/using-internal-errors.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ are intended to replace existing `Error` objects within the Node.js source.
2727
For instance, an existing `Error` such as:
2828

2929
```js
30-
var err = new TypeError('Expected string received ' + type);
30+
const err = new TypeError('Expected string received ' + type);
3131
```
3232

3333
Can be replaced by first adding a new error key into the `internal/errors.js`
@@ -42,7 +42,7 @@ Then replacing the existing `new TypeError` in the code:
4242
```js
4343
const errors = require('internal/errors');
4444
// ...
45-
var err = new errors.TypeError('FOO', type);
45+
const err = new errors.TypeError('FOO', type);
4646
```
4747

4848
## Adding new errors
@@ -80,8 +80,8 @@ codes.
8080
```js
8181
const errors = require('internal/errors');
8282

83-
var arg1 = 'foo';
84-
var arg2 = 'bar';
83+
const arg1 = 'foo';
84+
const arg2 = 'bar';
8585
const myError = new errors.Error('KEY', arg1, arg2);
8686
throw myError;
8787
```
@@ -100,8 +100,8 @@ The `myError` object will have a `code` property equal to the `key` and a
100100
```js
101101
const errors = require('internal/errors');
102102

103-
var arg1 = 'foo';
104-
var arg2 = 'bar';
103+
const arg1 = 'foo';
104+
const arg2 = 'bar';
105105
const myError = new errors.TypeError('KEY', arg1, arg2);
106106
throw myError;
107107
```
@@ -120,8 +120,8 @@ The `myError` object will have a `code` property equal to the `key` and a
120120
```js
121121
const errors = require('internal/errors');
122122

123-
var arg1 = 'foo';
124-
var arg2 = 'bar';
123+
const arg1 = 'foo';
124+
const arg2 = 'bar';
125125
const myError = new errors.RangeError('KEY', arg1, arg2);
126126
throw myError;
127127
```

doc/guides/writing-tests.md

+11-12
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,22 @@ this with a real test from the test suite.
141141

142142
```javascript
143143
'use strict';
144-
var common = require('../common');
145-
var assert = require('assert');
146-
var http = require('http');
144+
require('../common');
145+
const assert = require('assert');
146+
const http = require('http');
147147

148-
var request = 0;
149-
var response = 0;
148+
let request = 0;
149+
let response = 0;
150150
process.on('exit', function() {
151151
assert.equal(request, 1, 'http server "request" callback was not called');
152152
assert.equal(response, 1, 'http request "response" callback was not called');
153153
});
154154

155-
var server = http.createServer(function(req, res) {
155+
const server = http.createServer(function(req, res) {
156156
request++;
157157
res.end();
158158
}).listen(0, function() {
159-
var options = {
159+
const options = {
160160
agent: null,
161161
port: this.address().port
162162
};
@@ -172,14 +172,13 @@ This test could be greatly simplified by using `common.mustCall` like this:
172172

173173
```javascript
174174
'use strict';
175-
var common = require('../common');
176-
var assert = require('assert');
177-
var http = require('http');
175+
const common = require('../common');
176+
const http = require('http');
178177

179-
var server = http.createServer(common.mustCall(function(req, res) {
178+
const server = http.createServer(common.mustCall(function(req, res) {
180179
res.end();
181180
})).listen(0, function() {
182-
var options = {
181+
const options = {
183182
agent: null,
184183
port: this.address().port
185184
};

0 commit comments

Comments
 (0)