tools: update crypo check rule

This commit updates the custom crypto-check ESLint rule to
detect require() calls that come before any hasCrypto
checks.

PR-URL: https://github.com/nodejs/node/pull/25399
Refs: https://github.com/nodejs/node/pull/25388
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
This commit is contained in:
cjihrig 2019-01-08 13:02:15 -05:00 committed by Daniel Bevenius
parent 6cc74b038f
commit 4c9ea8f3fb
2 changed files with 24 additions and 0 deletions

View File

@ -22,6 +22,14 @@ new RuleTester().run('crypto-check', rule, {
`
],
invalid: [
{
code: 'require("common")\n' +
'require("crypto")\n' +
'if (!common.hasCrypto) {\n' +
' common.skip("missing crypto");\n' +
'}',
errors: [{ message }]
},
{
code: 'require("common")\n' +
'require("crypto")',

View File

@ -66,6 +66,22 @@ module.exports = function(context) {
function reportIfMissingCheck() {
if (hasSkipCall) {
// There is a skip, which is good, but verify that the require() calls
// in question come after at least one check.
if (missingCheckNodes.length > 0) {
requireNodes.forEach((requireNode) => {
const beforeAllChecks = missingCheckNodes.every((checkNode) => {
return requireNode.start < checkNode.start;
});
if (beforeAllChecks) {
context.report({
node: requireNode,
message: msg
});
}
});
}
return;
}