From 21500a81fc6d2facaf79dce1296ebdacc1978efd Mon Sep 17 00:00:00 2001 From: cjihrig Date: Tue, 8 Jan 2019 13:02:15 -0500 Subject: [PATCH] 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 Reviewed-By: James M Snell Reviewed-By: Daniel Bevenius --- test/parallel/test-eslint-crypto-check.js | 8 ++++++++ tools/eslint-rules/crypto-check.js | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/test/parallel/test-eslint-crypto-check.js b/test/parallel/test-eslint-crypto-check.js index 86c28d21218154..1829e86b020313 100644 --- a/test/parallel/test-eslint-crypto-check.js +++ b/test/parallel/test-eslint-crypto-check.js @@ -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")', diff --git a/tools/eslint-rules/crypto-check.js b/tools/eslint-rules/crypto-check.js index 7a93cba0ea5ff5..c83a51d589edae 100644 --- a/tools/eslint-rules/crypto-check.js +++ b/tools/eslint-rules/crypto-check.js @@ -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; }