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 <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
  • Loading branch information
cjihrig authored and BethGriggs committed May 10, 2019
1 parent c0accb8 commit e7e0a1f
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 e7e0a1f

Please sign in to comment.