-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Conversation
cc/ @nodejs/testing cc/ @silverwind @Trott @not-an-aardvark Is there an easy way to make rules only apply to |
Add the rule to |
Thanks, done |
There was a problem hiding this 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.
CI: https://ci.nodejs.org/job/node-test-commit/7103/ EDIT: CI passed |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2
means error
There was a problem hiding this comment.
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.
CI 2: https://ci.nodejs.org/job/node-test-commit/7112/ EDIT: Still green |
@@ -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++) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unneccesary wrapping?
There was a problem hiding this comment.
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}` | |||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unneccesary wrapping?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
81 chars again
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unneccesary wrapping?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unneccesary wrapping?
There was a problem hiding this comment.
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\');'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unneccesary wrapping?
There was a problem hiding this comment.
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++) { |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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++) { |
There was a problem hiding this comment.
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++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const
here too.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do
@silverwind Updated, PTAL |
Actually, I just realised that the eslint rule isn't showing up all the vars for some reason, I'll investigate later. |
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>
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>
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>
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>
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>
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>
This will need backport PRs in order to land on v6 or v4 |
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. |
v4.x backport: #10685 |
@gibfahn did can you do a v6.x backport too? |
@MylesBorins I'll do it once I get the v4.x backport working. There's more pig-wrestling than I'd expected. |
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>
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>
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:
Run this:
eslint --fix --rulesdir=tools/eslint-rules "test/**/*.js"
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
test