Skip to content

Commit

Permalink
tools: update crypo check rule
Browse files Browse the repository at this point in the history
This commit updates the custom crypto-check ESLint rule to
detect require() calls that come before any hasCrypto
checks.

PR-URL: #25399
Refs: #25388
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Daniel Bevenius <[email protected]>
  • Loading branch information
cjihrig authored and addaleax committed Jan 14, 2019
1 parent 46ec26f commit 2a85cc7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions test/parallel/test-eslint-crypto-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")',
Expand Down
16 changes: 16 additions & 0 deletions tools/eslint-rules/crypto-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 2a85cc7

Please sign in to comment.