-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools: add eslint rule for hasCrypto checking
The motivation for this commit is to pick up early on missing checks for crypto support (when Node is built --without-ssl). There are currently usages of common.hasCrypto which are not just for detecting if crypto support is available and then skip the test in question. For these case we still want to have a lint error generated which can then be disabled using an ESLint comment. PR-URL: #13813 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Teddy Katz <[email protected]>
- Loading branch information
1 parent
959b270
commit 1d97ff4
Showing
8 changed files
with
152 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* @fileoverview Check that common.hasCrypto is used if crypto, tls, | ||
* https, or http2 modules are required. | ||
* | ||
* This rule can be ignored using // eslint-disable-line crypto-check | ||
* | ||
* @author Daniel Bevenius <[email protected]> | ||
*/ | ||
'use strict'; | ||
|
||
const utils = require('./rules-utils.js'); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
const msg = 'Please add a hasCrypto check to allow this test to be skipped ' + | ||
'when Node is built "--without-ssl".'; | ||
|
||
module.exports = function(context) { | ||
const missingCheckNodes = []; | ||
const requireNodes = []; | ||
var hasSkipCall = false; | ||
|
||
function testCryptoUsage(node) { | ||
if (utils.isRequired(node, ['crypto', 'tls', 'https', 'http2'])) { | ||
requireNodes.push(node); | ||
} | ||
} | ||
|
||
function testIfStatement(node) { | ||
if (node.test.argument === undefined) { | ||
return; | ||
} | ||
if (isCryptoCheck(node.test.argument)) { | ||
checkCryptoCall(node); | ||
} | ||
} | ||
|
||
function isCryptoCheck(node) { | ||
return utils.usesCommonProperty(node, ['hasCrypto', 'hasFipsCrypto']); | ||
} | ||
|
||
function checkCryptoCall(node) { | ||
if (utils.inSkipBlock(node)) { | ||
hasSkipCall = true; | ||
} else { | ||
missingCheckNodes.push(node); | ||
} | ||
} | ||
|
||
function testMemberExpression(node) { | ||
if (isCryptoCheck(node)) { | ||
checkCryptoCall(node); | ||
} | ||
} | ||
|
||
function reportIfMissingCheck(node) { | ||
if (hasSkipCall) { | ||
return; | ||
} | ||
|
||
if (requireNodes.length > 0) { | ||
if (missingCheckNodes.length > 0) { | ||
report(missingCheckNodes); | ||
} else { | ||
report(requireNodes); | ||
} | ||
} | ||
} | ||
|
||
function report(nodes) { | ||
nodes.forEach((node) => { | ||
context.report(node, msg); | ||
}); | ||
} | ||
|
||
return { | ||
'CallExpression': (node) => testCryptoUsage(node), | ||
'IfStatement:exit': (node) => testIfStatement(node), | ||
'MemberExpression:exit': (node) => testMemberExpression(node), | ||
'Program:exit': (node) => reportIfMissingCheck(node) | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Utility functions common to ESLint rules. | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* Returns true if any of the passed in modules are used in | ||
* require calls. | ||
*/ | ||
module.exports.isRequired = function(node, modules) { | ||
return node.callee.name === 'require' && | ||
modules.includes(node.arguments[0].value); | ||
}; | ||
|
||
/** | ||
* Returns true is the node accesses any property in the properties | ||
* array on the 'common' object. | ||
*/ | ||
module.exports.usesCommonProperty = function(node, properties) { | ||
if (node.name) { | ||
return properties.includes(node.name); | ||
} | ||
if (node.property) { | ||
return properties.includes(node.property.name); | ||
} | ||
return false; | ||
}; | ||
|
||
/** | ||
* Returns true if the passed in node is inside an if statement block, | ||
* and the block also has a call to skip. | ||
*/ | ||
module.exports.inSkipBlock = function(node) { | ||
var hasSkipBlock = false; | ||
if (node.test && | ||
node.test.type === 'UnaryExpression' && | ||
node.test.operator === '!') { | ||
const consequent = node.consequent; | ||
if (consequent.body) { | ||
consequent.body.some(function(expressionStatement) { | ||
if (hasSkip(expressionStatement.expression)) { | ||
return hasSkipBlock = true; | ||
} | ||
return false; | ||
}); | ||
} else { | ||
if (hasSkip(consequent.expression)) { | ||
hasSkipBlock = true; | ||
} | ||
} | ||
} | ||
return hasSkipBlock; | ||
}; | ||
|
||
function hasSkip(expression) { | ||
return expression && | ||
expression.callee && | ||
(expression.callee.name === 'skip' || | ||
expression.callee.property && | ||
expression.callee.property.name === 'skip'); | ||
} |