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

test: convert var->const/let in tests #10685

Merged
merged 2 commits into from
Jan 11, 2017
Merged

Conversation

gibfahn
Copy link
Member

@gibfahn gibfahn commented Jan 8, 2017

Overview

Use eslint to convert all var to const/let in test/, manually fix anything that eslint messed up.

If we're going to go ES6 in test/, we might as well go all the way.

To fix rules with eslint

Apply this:

diff --git a/.eslintrc b/.eslintrc
index cf1f36c86b..e09fd6ac64 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -110,6 +110,8 @@ rules:
 
   # ECMAScript 6
   # http://eslint.org/docs/rules/#ecmascript-6
+  no-var: error
+  prefer-const: error
   arrow-parens: [2, always]
   arrow-spacing: [2, {before: true, after: true}]
   constructor-super: 2
@@ -119,7 +121,6 @@ rules:
   no-dupe-class-members: 2
   no-new-symbol: 2
   no-this-before-super: 2
-  prefer-const: [2, {ignoreReadBeforeAssign: true}]
   rest-spread-spacing: 2
   template-curly-spacing: 2

Run this:

eslint --fix --rulesdir=tools/eslint-rules "test/**/*.js"

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

test

@gibfahn gibfahn added the test Issues and PRs related to the tests. label Jan 8, 2017
@gibfahn
Copy link
Member Author

gibfahn commented Jan 8, 2017

cc/ @nodejs/testing

cc/ @silverwind @Trott @not-an-aardvark Is there an easy way to make rules only apply to test/? Also should @not-an-aardvark be on the tools/eslint cc list in onboarding-extras?

@targos
Copy link
Member

targos commented Jan 8, 2017

Is there an easy way to make rules only apply to test/

Add the rule to test/.eslintrc.

@gibfahn
Copy link
Member Author

gibfahn commented Jan 8, 2017

@targos

Add the rule to test/.eslintrc.

Thanks, done

Copy link
Contributor

@cjihrig cjihrig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rubber stamp LGTM if the CI passes.

@gibfahn
Copy link
Member Author

gibfahn commented Jan 8, 2017

CI: https://ci.nodejs.org/job/node-test-commit/7103/

EDIT: CI passed

@gibfahn
Copy link
Member Author

gibfahn commented Jan 8, 2017

It's probably worth checking the second and third commits:

  • second (manual fixups of eslint issues): 27d10c0
  • third (require let/const in test/): 21725bf

Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rubber-stamp LGTM

@@ -4,3 +4,5 @@ rules:
## common module is mandatory in tests
required-modules: [2, common]
prefer-assert-methods: 2
no-var: error
prefer-const: error
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be:

no-var: 2
prefer-const: 2

? I'm not sure what the 2 represents here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 means error

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed error to 2 for consistency.

@gibfahn
Copy link
Member Author

gibfahn commented Jan 9, 2017

CI 2: https://ci.nodejs.org/job/node-test-commit/7112/

EDIT: Still green

@gibfahn
Copy link
Member Author

gibfahn commented Jan 10, 2017

@@ -35,7 +35,8 @@ const server = http.Server(function(req, res) {


server.listen(0, function() {
for (var i = 0; i < total; i++) {
let i;
for (i = 0; i < total; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can const this one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

➜  node git:(var2const) tools/test.py test/parallel/test-http-get-pipeline-problem.js 
=== release test-http-get-pipeline-problem ===                    
Path: parallel/test-http-get-pipeline-problem
image.length = 45658
/Users/gib/wrk/com/node/test/parallel/test-http-get-pipeline-problem.js:38
  for (const i = 0; i < total; i++) {
                                ^

TypeError: Assignment to constant variable.

If this is what you meant, I don't think so. I can do for (let i = 0; though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, I could swear const loop variables once worked, but I see it's failing now. Your let suggestion sounds good.

@@ -28,7 +28,8 @@ const server = http.createServer(function(req, res) {
});

server.listen(0, function() {
const client = net.connect({ port: this.address().port, allowHalfOpen: true });
const client = net.connect({ port: this.address().port,
allowHalfOpen: true });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unneccesary wrapping?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The semicolon puts it over 80 chars 😭

@@ -18,7 +18,8 @@ const server = http.createServer(function(request, response) {
});

server.listen(0, function() {
const testURL = url.parse(`http://asdf:qwer@localhost:${this.address().port}`);
const testURL = url.parse(`http://asdf:qwer@localhost:${this.address().port}`
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unneccesary wrapping?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

81 chars again

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the string to a variable so it doesn't look so hideous? :)

@@ -13,7 +13,8 @@ const server = net.createServer(common.mustCall(function(socket) {
}));

server.listen(0, common.localhostIPv4, function() {
const client = net.createConnection(this.address().port, common.localhostIPv4);
const client = net.createConnection(this.address()
.port, common.localhostIPv4);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unneccesary wrapping?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

➜  node git:(var2const) ✗ make lint
./node tools/eslint/bin/eslint.js --cache --rulesdir=tools/eslint-rules \
	  benchmark lib test tools

/Users/gib/wrk/com/node/test/parallel/test-net-local-address-port.js
  16:1  error  Line 16 exceeds the maximum line length of 80  max-len

@@ -6,7 +6,8 @@ const zlib = require('zlib');

// just use builtin stream inherited from Duplex
const putIn = zlib.createGzip();
const testMe = repl.start('', putIn, function(cmd, context, filename, callback) {
const testMe = repl.start('', putIn, function(cmd, context, filename,
callback) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unneccesary wrapping?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above

'if (typeof baz !== \'undefined\') throw new Error(\'test fail\');';
'bar = 2;' +
'if (typeof baz !== \'undefined\')' +
'throw new Error(\'test fail\');';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unneccesary wrapping?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above

@@ -127,7 +127,8 @@ function testUint(clazz) {
let val = 1;

// Test 0 to 5 bytes.
for (var i = 0; i <= 5; i++) {
let i;
for (i = 0; i <= 5; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, can probably use const.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to let as above (const didn't work)

let ok = true;
const testNum = ++done;
for (var i = 0; i < Math.max(c.length, test.length); i++) {
let i;
for (i = 0; i < Math.max(c.length, test.length); i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const again.

@@ -15,7 +15,7 @@ pummel();
function pummel() {
console.log('Round', rounds, '/', ROUNDS);

for (var pending = 0; pending < ATTEMPTS_PER_ROUND; pending++) {
for (let pending = 0; pending < ATTEMPTS_PER_ROUND; pending++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const here too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const doesn't work (the pending++ would be modifying it)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, as is the test failed (it's not run in CI), to fix I had to move the let to the previous line.

@@ -5,3 +5,5 @@ rules:
required-modules: [2, common]
prefer-assert-iferror: 2
prefer-assert-methods: 2
no-var: 2
prefer-const: 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe alphabetically sort the rules?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

@gibfahn
Copy link
Member Author

gibfahn commented Jan 10, 2017

@silverwind Updated, PTAL

@gibfahn
Copy link
Member Author

gibfahn commented Jan 10, 2017

Actually, I just realised that the eslint rule isn't showing up all the vars for some reason, I'll investigate later.

targos pushed a commit that referenced this pull request Jan 28, 2017
Manually fix issues that eslint --fix couldn't do automatically.

PR-URL: #10685
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
targos pushed a commit that referenced this pull request Jan 28, 2017
PR-URL: #10685
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
@italoacasas italoacasas mentioned this pull request Jan 29, 2017
@gibfahn gibfahn restored the var2const branch January 29, 2017 22:30
@gibfahn gibfahn deleted the var2const branch January 29, 2017 22:32
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
Manually fix issues that eslint --fix couldn't do automatically.

PR-URL: nodejs#10685
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
PR-URL: nodejs#10685
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
Manually fix issues that eslint --fix couldn't do automatically.

PR-URL: nodejs#10685
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
PR-URL: nodejs#10685
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
@jasnell
Copy link
Member

jasnell commented Mar 8, 2017

This will need backport PRs in order to land on v6 or v4

@bnoordhuis
Copy link
Member

This is likely preventing a lot of pull requests from back-porting cleanly so I think it should be back-ported with some urgency. Likewise for #10698.

@gibfahn gibfahn restored the var2const branch March 9, 2017 08:28
@gibfahn
Copy link
Member Author

gibfahn commented Mar 9, 2017

v4.x backport: #10685

@MylesBorins
Copy link
Contributor

@gibfahn did can you do a v6.x backport too?

@gibfahn
Copy link
Member Author

gibfahn commented Mar 9, 2017

@MylesBorins I'll do it once I get the v4.x backport working. There's more pig-wrestling than I'd expected.

MylesBorins pushed a commit that referenced this pull request Apr 13, 2017
Backport-PR-URL: #11775
PR-URL: #10685
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
MylesBorins pushed a commit that referenced this pull request Apr 13, 2017
Manually fix issues that eslint --fix couldn't do automatically.

Backport-PR-URL: #11775
PR-URL: #10685
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
MylesBorins pushed a commit that referenced this pull request Apr 19, 2017
Backport-PR-URL: #11775
PR-URL: #10685
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
MylesBorins pushed a commit that referenced this pull request Apr 19, 2017
Manually fix issues that eslint --fix couldn't do automatically.

Backport-PR-URL: #11775
PR-URL: #10685
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
@MylesBorins MylesBorins mentioned this pull request Apr 19, 2017
@gibfahn gibfahn deleted the var2const branch June 3, 2017 14:34
andrew749 pushed a commit to michielbaird/node that referenced this pull request Jul 19, 2017
Backport-PR-URL: nodejs/node#11775
PR-URL: nodejs/node#10685
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
andrew749 pushed a commit to michielbaird/node that referenced this pull request Jul 19, 2017
Manually fix issues that eslint --fix couldn't do automatically.

Backport-PR-URL: nodejs/node#11775
PR-URL: nodejs/node#10685
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.