Skip to content

Commit

Permalink
doc: add no-var, prefer-const in doc eslintrc
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
vsemozhetbyt committed Apr 24, 2017
1 parent b4fea2a commit 6ee6aae
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 42 deletions.
9 changes: 8 additions & 1 deletion doc/.eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## Docs-specific linter rules

rules:
strict: 0
# 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
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
18 changes: 9 additions & 9 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function writeOneMillionTimes(writer, data, encoding, callback) {
let i = 1000000;
write();
function write() {
var ok = true;
let ok = true;
do {
i--;
if (i === 0) {
Expand Down Expand Up @@ -869,7 +869,7 @@ the internal buffer is fully drained.
```js
const readable = getReadableStreamSomehow();
readable.on('readable', () => {
var chunk;
let chunk;
while (null !== (chunk = readable.read())) {
console.log(`Received ${chunk.length} bytes of data.`);
}
Expand Down Expand Up @@ -1004,14 +1004,14 @@ function parseHeader(stream, callback) {
stream.on('error', callback);
stream.on('readable', onReadable);
const decoder = new StringDecoder('utf8');
var header = '';
let header = '';
function onReadable() {
var chunk;
let chunk;
while (null !== (chunk = stream.read())) {
var str = decoder.write(chunk);
const str = decoder.write(chunk);
if (str.match(/\n\n/)) {
// found the header boundary
var split = str.split(/\n\n/);
const split = str.split(/\n\n/);
header += split.shift();
const remaining = split.join('\n\n');
const buf = Buffer.from(remaining, 'utf8');
Expand Down Expand Up @@ -1598,12 +1598,12 @@ class Counter extends Readable {
}

_read() {
var i = this._index++;
const i = this._index++;
if (i > this._max)
this.push(null);
else {
var str = '' + i;
var buf = Buffer.from(str, 'ascii');
const str = '' + i;
const buf = Buffer.from(str, 'ascii');
this.push(buf);
}
}
Expand Down
2 changes: 1 addition & 1 deletion doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ it is marked as deprecated.
const util = require('util');

exports.puts = util.deprecate(function() {
for (var i = 0, len = arguments.length; i < len; ++i) {
for (let i = 0, len = arguments.length; i < len; ++i) {
process.stdout.write(arguments[i] + '\n');
}
}, 'util.puts: Use console.log instead');
Expand Down
13 changes: 7 additions & 6 deletions doc/api/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const sandbox = {
const script = new vm.Script('count += 1; name = "kitty";');

const context = new vm.createContext(sandbox);
for (var i = 0; i < 10; ++i) {
for (let i = 0; i < 10; ++i) {
script.runInContext(context);
}

Expand Down Expand Up @@ -203,7 +203,7 @@ global.globalVar = 0;

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

for (var i = 0; i < 1000; ++i) {
for (let i = 0; i < 1000; ++i) {
script.runInThisContext();
}

Expand Down Expand Up @@ -231,14 +231,14 @@ will remain unchanged.
const util = require('util');
const vm = require('vm');

var globalVar = 3;
global.globalVar = 3;

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

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

console.log(util.inspect(sandbox)); // 2
console.log(util.inspect(sandbox)); // { globalVar: 2 }

console.log(util.inspect(globalVar)); // 3
```
Expand Down Expand Up @@ -296,7 +296,7 @@ const vm = require('vm');
const sandbox = { globalVar: 1 };
vm.createContext(sandbox);

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

<!-- eslint-disable prefer-const -->
```js
const vm = require('vm');
var localVar = 'initial value';
let localVar = 'initial value';

const vmResult = vm.runInThisContext('localVar = "vm";');
console.log('vmResult:', vmResult);
Expand Down
6 changes: 3 additions & 3 deletions doc/api/zlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const request = http.get({ host: 'example.com',
port: 80,
headers: { 'Accept-Encoding': 'gzip,deflate' } });
request.on('response', (response) => {
var output = fs.createWriteStream('example.com_index.html');
const output = fs.createWriteStream('example.com_index.html');

switch (response.headers['content-encoding']) {
// or, just use zlib.createUnzip() to handle both cases
Expand All @@ -94,8 +94,8 @@ const zlib = require('zlib');
const http = require('http');
const fs = require('fs');
http.createServer((request, response) => {
var raw = fs.createReadStream('index.html');
var acceptEncoding = request.headers['accept-encoding'];
const raw = fs.createReadStream('index.html');
let acceptEncoding = request.headers['accept-encoding'];
if (!acceptEncoding) {
acceptEncoding = '';
}
Expand Down
16 changes: 8 additions & 8 deletions doc/guides/using-internal-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ are intended to replace existing `Error` objects within the Node.js source.
For instance, an existing `Error` such as:

```js
var err = new TypeError('Expected string received ' + type);
const err = new TypeError('Expected string received ' + type);
```

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

## Adding new errors
Expand Down Expand Up @@ -80,8 +80,8 @@ codes.
```js
const errors = require('internal/errors');

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

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

var arg1 = 'foo';
var arg2 = 'bar';
const arg1 = 'foo';
const arg2 = 'bar';
const myError = new errors.RangeError('KEY', arg1, arg2);
throw myError;
```
Expand Down
23 changes: 11 additions & 12 deletions doc/guides/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,22 @@ this with a real test from the test suite.

```javascript
'use strict';
var common = require('../common');
var assert = require('assert');
var http = require('http');
require('../common');
const assert = require('assert');
const http = require('http');

var request = 0;
var response = 0;
let request = 0;
let response = 0;
process.on('exit', function() {
assert.equal(request, 1, 'http server "request" callback was not called');
assert.equal(response, 1, 'http request "response" callback was not called');
});

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

```javascript
'use strict';
var common = require('../common');
var assert = require('assert');
var http = require('http');
const common = require('../common');
const http = require('http');

var server = http.createServer(common.mustCall(function(req, res) {
const server = http.createServer(common.mustCall(function(req, res) {
res.end();
})).listen(0, function() {
var options = {
const options = {
agent: null,
port: this.address().port
};
Expand Down

0 comments on commit 6ee6aae

Please sign in to comment.