-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
tools: auto fix custom eslint rule for prefer-assert-iferror.js #16648
tools: auto fix custom eslint rule for prefer-assert-iferror.js #16648
Conversation
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.
Thanks for starting the work on this @shobhitchittora — for all of these fixers, could you expand the existing tests located in test/parallel/test-eslint-*
?
See https://eslint.org/docs/developer-guide/nodejs-api#ruletester and https://github.com/eslint/eslint/blob/master/tests/lib/rules/arrow-body-style.js for more info.
Thanks for pointing tests out. I'll start working on them after reading the docs you mentioned. Thanks again. |
After going through |
The idea would be to expand the following: {
code: 'if (err) throw err;',
errors: [{ message: 'Use assert.ifError(err) instead.' }]
},
{
code: 'if (error) { throw error; }',
errors: [{ message: 'Use assert.ifError(error) instead.' }]
} with https://eslint.org/docs/developer-guide/nodejs-api#ruletester
|
Done. Extended the current tests with the output key. Expecting your review @apapirovski. Thanks. |
}, | ||
{ | ||
code: 'if (error) { throw error; }', | ||
errors: [{ message: 'Use assert.ifError(error) instead.' }] | ||
errors: [{ message: 'Use assert.ifError(error) instead.' }], | ||
output: 'assert.ifError(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.
These should be assert.ifError(err);
and assert.ifError(error);
with an ending semicolon.
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.
@apapirovski Done. 👍🏻
fix: (fixer) => { | ||
return fixer.replaceText( | ||
node, | ||
`assert.ifError(${argument})` |
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.
You'll need a semicolon at the end.
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.
@apapirovski 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.
LGTM — thanks for the adjustments @shobhitchittora
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.
Seems correct (although I haven't tested it)
Thank you all for being good sport. @apapirovski just wondering why the build is failing for linux and windows.
|
We have a test that's failing on those platforms. It's not related to this PR. :) |
This seems like it would cause incorrect code if the |
@not-an-aardvark I think this comes down to what the expectation the user sets for the fixers...personally I would take it with a grain of salt and fix the tricky ones by myself before I run it to fix something like whitespace changes, but it may be just me. |
Hm, we could further check if assert is imported or not and fix that as well. But it might be a bit to much? @not-an-aardvark what is your call about the PR? |
Not a fan of fixing missing requires. It's pretty hard to detect where exactly the require needs to go within the file. |
I agree that it could be difficult to automatically insert require calls. I have no strong opinion either way on whether this would be worthwhile without inserting require calls. Another option would be to only do an autofix if there is an |
I think that would be best! |
@shobhitchittora would you be so kind and update the PR to also check if there is an |
@BridgeAR Done 👍🏻. |
@@ -15,11 +15,13 @@ new RuleTester().run('prefer-assert-iferror', rule, { | |||
invalid: [ | |||
{ | |||
code: 'if (err) throw err;', | |||
errors: [{ message: 'Use assert.ifError(err) instead.' }] | |||
errors: [{ message: 'Use assert.ifError(err) instead.' }], | |||
output: 'assert.ifError(err);' |
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'm guessing this won't work without having require('assert');
in code
and output
. Same thing below.
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.
@shobhitchittora would you be so kind and have a look at this comment?
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 test would actually work without the require('assert')
specified. While using assert the user has to require assert to use it right. Also in every other test for rules, this is what is followed.
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 far as I can tell @apapirovski is right about his comment. I am going to run a CI anyway, so we do not have to speculate on it.
1. Adds the fixer method 2. Extends Tests Refs: nodejs#16636
e62d6da
to
94b97b0
Compare
1. Adds the fixer method 2. Extends Tests Refs: nodejs#16636
Mini CI (should be enough to test the rule): https://ci.nodejs.org/job/node-test-commit-light/145/ |
|
Ping @shobhitchittora |
@BridgeAR apologies for keeping this hanging. Been a little pre-occupied. Looking into it.😇 |
1. Fixes the failing test for the rule. Adds require("assert") to code and output. Refs: nodejs#16636
…ora/node into prefer-assert-iferror-fixer
@BridgeAR I pushed a change and now the tests are working locally. Used this to test - Thanks for being so patient. |
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.
LG if CI is green.
PR-URL: nodejs#16648 Refs: nodejs#16636 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
Landed in d797775 🎉 |
PR-URL: #16648 Refs: #16636 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
PR-URL: #16648 Refs: #16636 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
PR-URL: #16648 Refs: #16636 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
PR-URL: #16648 Refs: #16636 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
PR-URL: nodejs#16648 Refs: nodejs#16636 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
This implements an eslint fixer function to replace
if(err) throw err;
, usages toassert.ifError(err);
.Refs: #16636
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passespython ./tools/test.py parallel/test-eslint-prefer-assert-iferror
Affected core subsystem(s)
Tools