|
| 1 | +/** |
| 2 | + * @fileoverview Check that common.hasCrypto is used if crypto, tls, |
| 3 | + * https, or http2 modules are required. |
| 4 | + * |
| 5 | + * This rule can be ignored using // eslint-disable-line crypto-check |
| 6 | + * |
| 7 | + * @author Daniel Bevenius <[email protected]> |
| 8 | + */ |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +const utils = require('./rules-utils.js'); |
| 12 | + |
| 13 | +//------------------------------------------------------------------------------ |
| 14 | +// Rule Definition |
| 15 | +//------------------------------------------------------------------------------ |
| 16 | +const msg = 'Please add a hasCrypto check to allow this test to be skipped ' + |
| 17 | + 'when Node is built "--without-ssl".'; |
| 18 | + |
| 19 | +module.exports = function(context) { |
| 20 | + const missingCheckNodes = []; |
| 21 | + const requireNodes = []; |
| 22 | + var hasSkipCall = false; |
| 23 | + |
| 24 | + function testCryptoUsage(node) { |
| 25 | + if (utils.isRequired(node, ['crypto', 'tls', 'https', 'http2'])) { |
| 26 | + requireNodes.push(node); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + function testIfStatement(node) { |
| 31 | + if (node.test.argument === undefined) { |
| 32 | + return; |
| 33 | + } |
| 34 | + if (isCryptoCheck(node.test.argument)) { |
| 35 | + checkCryptoCall(node); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + function isCryptoCheck(node) { |
| 40 | + return utils.usesCommonProperty(node, ['hasCrypto', 'hasFipsCrypto']); |
| 41 | + } |
| 42 | + |
| 43 | + function checkCryptoCall(node) { |
| 44 | + if (utils.inSkipBlock(node)) { |
| 45 | + hasSkipCall = true; |
| 46 | + } else { |
| 47 | + missingCheckNodes.push(node); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + function testMemberExpression(node) { |
| 52 | + if (isCryptoCheck(node)) { |
| 53 | + checkCryptoCall(node); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + function reportIfMissingCheck(node) { |
| 58 | + if (hasSkipCall) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + if (requireNodes.length > 0) { |
| 63 | + if (missingCheckNodes.length > 0) { |
| 64 | + report(missingCheckNodes); |
| 65 | + } else { |
| 66 | + report(requireNodes); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + function report(nodes) { |
| 72 | + nodes.forEach((node) => { |
| 73 | + context.report(node, msg); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + return { |
| 78 | + 'CallExpression': (node) => testCryptoUsage(node), |
| 79 | + 'IfStatement:exit': (node) => testIfStatement(node), |
| 80 | + 'MemberExpression:exit': (node) => testMemberExpression(node), |
| 81 | + 'Program:exit': (node) => reportIfMissingCheck(node) |
| 82 | + }; |
| 83 | +}; |
0 commit comments