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

Backport removing let in for loop declaration #9553

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 lib/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
rules:
# Custom rules in tools/eslint-rules
require-buffer: 2
no-let-in-for-declaration: 2
8 changes: 4 additions & 4 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,17 +654,17 @@ Readable.prototype.unpipe = function(dest) {
state.pipesCount = 0;
state.flowing = false;

for (let i = 0; i < len; i++)
for (var i = 0; i < len; i++)
dests[i].emit('unpipe', this);
return this;
}

// try to find the right one.
const i = state.pipes.indexOf(dest);
if (i === -1)
const index = state.pipes.indexOf(dest);
if (index === -1)
return this;

state.pipes.splice(i, 1);
state.pipes.splice(index, 1);
state.pipesCount -= 1;
if (state.pipesCount === 1)
state.pipes = state.pipes[0];
Expand Down
10 changes: 6 additions & 4 deletions lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ exports.SecureContext = SecureContext;

exports.createSecureContext = function createSecureContext(options, context) {
if (!options) options = {};
var i;
var len;

var secureOptions = options.secureOptions;
if (options.honorCipherOrder)
Expand All @@ -47,7 +49,7 @@ exports.createSecureContext = function createSecureContext(options, context) {
// cert's issuer in C++ code.
if (options.ca) {
if (Array.isArray(options.ca)) {
for (let i = 0, len = options.ca.length; i < len; i++) {
for (i = 0, len = options.ca.length; i < len; i++) {
c.context.addCACert(options.ca[i]);
}
} else {
Expand All @@ -59,7 +61,7 @@ exports.createSecureContext = function createSecureContext(options, context) {

if (options.cert) {
if (Array.isArray(options.cert)) {
for (let i = 0; i < options.cert.length; i++)
for (i = 0; i < options.cert.length; i++)
c.context.setCert(options.cert[i]);
} else {
c.context.setCert(options.cert);
Expand All @@ -72,7 +74,7 @@ exports.createSecureContext = function createSecureContext(options, context) {
// which leads to the crash later on.
if (options.key) {
if (Array.isArray(options.key)) {
for (let i = 0; i < options.key.length; i++) {
for (i = 0; i < options.key.length; i++) {
var key = options.key[i];

if (key.passphrase)
Expand Down Expand Up @@ -103,7 +105,7 @@ exports.createSecureContext = function createSecureContext(options, context) {

if (options.crl) {
if (Array.isArray(options.crl)) {
for (let i = 0, len = options.crl.length; i < len; i++) {
for (i = 0, len = options.crl.length; i < len; i++) {
c.context.addCRL(options.crl[i]);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ function REPLServer(prompt,

// After executing the current expression, store the values of RegExp
// predefined properties back in `savedRegExMatches`
for (let idx = 1; idx < savedRegExMatches.length; idx += 1) {
for (var idx = 1; idx < savedRegExMatches.length; idx += 1) {
savedRegExMatches[idx] = RegExp[`$${idx}`];
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function check(hostParts, pattern, wildcards) {
return false;

// Check host parts from right to left first.
for (let i = hostParts.length - 1; i > 0; i -= 1)
for (var i = hostParts.length - 1; i > 0; i -= 1)
if (hostParts[i] !== patternParts[i])
return false;

Expand Down
12 changes: 6 additions & 6 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
if (typeof url !== 'string') {
throw new TypeError("Parameter 'url' must be a string, not " + typeof url);
}

var i, j, k, l;
// Copy chrome, IE, opera backslash-handling behavior.
// Back slashes before the query string get converted to forward slashes
// See: https://code.google.com/p/chromium/issues/detail?id=25916
Expand Down Expand Up @@ -169,7 +169,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {

// find the first instance of any hostEndingChars
var hostEnd = -1;
for (let i = 0; i < hostEndingChars.length; i++) {
for (i = 0; i < hostEndingChars.length; i++) {
const hec = rest.indexOf(hostEndingChars[i]);
if (hec !== -1 && (hostEnd === -1 || hec < hostEnd))
hostEnd = hec;
Expand Down Expand Up @@ -197,7 +197,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {

// the host is the remaining to the left of the first non-host char
hostEnd = -1;
for (let i = 0; i < nonHostChars.length; i++) {
for (i = 0; i < nonHostChars.length; i++) {
const hec = rest.indexOf(nonHostChars[i]);
if (hec !== -1 && (hostEnd === -1 || hec < hostEnd))
hostEnd = hec;
Expand All @@ -224,12 +224,12 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
// validate a little.
if (!ipv6Hostname) {
var hostparts = this.hostname.split(/\./);
for (let i = 0, l = hostparts.length; i < l; i++) {
for (i = 0, l = hostparts.length; i < l; i++) {
var part = hostparts[i];
if (!part) continue;
if (!part.match(hostnamePartPattern)) {
var newpart = '';
for (let j = 0, k = part.length; j < k; j++) {
for (j = 0, k = part.length; j < k; j++) {
if (part.charCodeAt(j) > 127) {
// we replace non-ASCII char with a temporary placeholder
// we need this to make sure size of hostname is not
Expand Down Expand Up @@ -294,7 +294,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
// First, make 100% sure that any "autoEscape" chars get
// escaped, even if encodeURIComponent doesn't think they
// need to be.
for (let i = 0, l = autoEscape.length; i < l; i++) {
for (i = 0, l = autoEscape.length; i < l; i++) {
var ae = autoEscape[i];
if (rest.indexOf(ae) === -1)
continue;
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-stream-pipe-unpipe-streams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
const common = require('../common');
const assert = require('assert');

const Stream = require('stream');
const Readable = Stream.Readable;
const Writable = Stream.Writable;

const source = Readable({read: () => {}});
const dest1 = Writable({write: () => {}});
const dest2 = Writable({write: () => {}});

source.pipe(dest1);
source.pipe(dest2);

dest1.on('unpipe', common.mustCall(() => {}));
dest2.on('unpipe', common.mustCall(() => {}));

assert.strictEqual(source._readableState.pipes[0], dest1);
assert.strictEqual(source._readableState.pipes[1], dest2);
assert.strictEqual(source._readableState.pipes.length, 2);

// Should be able to unpipe them in the reverse order that they were piped.

source.unpipe(dest2);

assert.strictEqual(source._readableState.pipes, dest1);
assert.notStrictEqual(source._readableState.pipes, dest2);

source.unpipe(dest1);

assert.strictEqual(source._readableState.pipes, null);
46 changes: 46 additions & 0 deletions tools/eslint-rules/no-let-in-for-declaration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @fileoverview Prohibit the use of `let` as the loop variable
* in the initialization of for, and the left-hand
* iterator in forIn and forOf loops.
*
* @author Jessica Quynh Tran
*/

'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
create(context) {

const msg = 'Use of `let` as the loop variable in a for-loop is ' +
'not recommended. Please use `var` instead.';

/**
* Report function to test if the for-loop is declared using `let`.
*/
function testForLoop(node) {
if (node.init && node.init.kind === 'let') {
context.report(node.init, msg);
}
}

/**
* Report function to test if the for-in or for-of loop
* is declared using `let`.
*/
function testForInOfLoop(node) {
if (node.left && node.left.kind === 'let') {
context.report(node.left, msg);
}
}

return {
'ForStatement': testForLoop,
'ForInStatement': testForInOfLoop,
'ForOfStatement': testForInOfLoop
};
}
};