-
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 crypto-check.js #16647
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! Left some minor feedback and please see the other PR re: the need for tests.
tools/eslint-rules/crypto-check.js
Outdated
fix: (fixer) => { | ||
return fixer.insertTextAfter( | ||
node, | ||
'\n\nif (!common.hasCrypto)\n\tcommon.skip("missing crypto");' |
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.
What does the node
refer to in this context? This should be getting inserted right after common
is required rather than somewhere random.
Also, the project doesn't use tabs but rather spaces so \t
should be a double space.
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 the comments. Working on commit to fix.
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've pushed the changes. Also please note that I've added a regExp in rules-utils.js, please verify the validity of the same, or suggest if there are any better ways of doing that.
The reason for doing this was because the common module was addressed with different relative paths, such as ../../common
or ../common
.
Thanks for the updates! I'll be happy to review again once we have the tests for all of these fixers, just so we can be sure that they're working properly (it's a bit hard to tell without actually running the code). |
tools/eslint-rules/crypto-check.js
Outdated
message: msg, | ||
fix: (fixer) => { | ||
return fixer.insertTextAfter( | ||
commonModuleNode, |
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.
Need help here @apapirovski . commonModuleNode
comes as null here when running make -j4 test
. I'm setting commonModuleNode
in testCryptoUsage
method.
PS: This works fine when running eslint with --fix
to fix a generated 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.
Ping @apapirovski
tools/eslint-rules/rules-utils.js
Outdated
@@ -13,6 +13,16 @@ module.exports.isRequired = function(node, modules) { | |||
}; | |||
|
|||
/** | |||
* Return true if common module is present as a require call | |||
* in AST | |||
*/ |
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.
This description is not entirely accurate. The given node must be a call to require
, it does not work for nodes which contain such a call.
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.
@tniessen Please check now. Suggest if any corrections.
Much like with the other PR:
Sorry @shobhitchittora. |
@BridgeAR What are your views on landing this? |
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 personally do not see as much concern as @apapirovski here as these rules are only active for our tests and we should realize if something goes wrong. The latest in the code review.
tools/eslint-rules/rules-utils.js
Outdated
* in AST Node under inspection | ||
*/ | ||
module.exports.isCommonModule = function(node) { | ||
var regExp = new RegExp(/^(\.\.\/)*common(\.js)?$/); |
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.
Why do you not write the RegExp directly? There is no dynamic part as far as I see it.
So /^(\.\.\/)*common(\.js)?$/
does the job and can be moved outside of the function. That way it is only created once.
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.
done.
d607020
to
d8d5713
Compare
@BridgeAR would you be so kind and run CI for this too? |
} | ||
require('crypto'); | ||
if (!common.hasCrypto) { | ||
common.skip(); |
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.
Does this (and other occurences below) pass the test? I think the 'missing crypto'
message is missing.
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.
Added the string 'missing crypto'
in tests. But there's still something wrong. Any help?
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.
Done now.
@shobhitchittora this has similar issues as the other PRs. Please run the tests by calling |
You can also run the linter only with |
Ping @shobhitchittora |
@BridgeAR I'm on it. |
@BridgeAR I cannot spot the diff when running
Any suggestions? Can you spot the 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.
lgtm.
`if (!common.hasCrypto) { | ||
common.skip(); | ||
} | ||
require("crypto"); | ||
` |
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.
Nit: unnecessary change.
`if (!common.hasCrypto) { | ||
common.skip("missing crypto"); | ||
} | ||
require("crypto"); |
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.
This can not be the proper output because there is no common imported in the code.
`if (!common.hasCrypto) { | ||
common.skip("missing crypto"); | ||
} | ||
require("crypto"); |
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.
This can not be the expected output because common is not imported. But not only that, the first statement if (common.foo) {}
was just removed? That is definitely not right.
@shobhitchittora please rebase and run the code again. In that case the output is going to show what check failed. Please carefully check what change you expect and what not with what input. Please add another test with a case where common is actually imported in the checked code part. |
1. Fixer for crypto-check.js 2. extends tests Refs : nodejs#16636
Removes unwanted indentation Refs : nodejs#16636
Adds check for fixing on when require('common') is present. Refs : nodejs#16636
Adds "missing crypto" message in tests. Refs : nodejs#16636
1. Refactors test 2. Removes commonModule AST node as array. Refs : nodejs#16636
94c7b1e
to
2ecf517
Compare
Review comments + updates test cases Refs : nodejs#16636
d6d6c26
to
1018b5b
Compare
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.
Working tests now. Please check.
} | ||
require('crypto'); | ||
if (!common.hasCrypto) { | ||
common.skip(); |
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.
Done now.
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 with the comment addressed and a green CI.
tools/eslint-rules/rules-utils.js
Outdated
var commonModuleRegExp = new RegExp(/^(\.\.\/)*common(\.js)?$/); | ||
module.exports.isCommonModule = function(node) { | ||
return node.callee.name === 'require' && | ||
commonModuleRegExp.test(node.arguments[0].value); |
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.
This will produce access errors due to accessing nodes without arguments. Please guard against them the same as I suggested in the other PR.
Seems like there are linting errors. |
Removes extra spaces ( lint-error ) + rules-utils argument check suggestoin Refs : nodejs#16636
f4c1193
to
46206b7
Compare
1. Fixer for crypto-check.js 2. Extends tests PR-URL: nodejs#16647 Refs: nodejs#16636 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Khaidi Chu <i@2333.moe> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Landed in f242d4b 🎉 @shobhitchittora thanks for being patient :-) |
1. Fixer for crypto-check.js 2. Extends tests PR-URL: nodejs#16647 Refs: nodejs#16636 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Khaidi Chu <i@2333.moe> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This implements an eslint fixer function to auto insert common.hasCryto check if missed out.
Refs: #16636
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passespython ./tools/test.py parallel/test-eslint-crypto-check
Affected core subsystem(s)
Tools