Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
tools: auto fix custom eslint rule for crypto-check.js
1. Fixer for crypto-check.js
2. extends tests

Refs : #16636
  • Loading branch information
shobhitchittora committed Feb 12, 2018
commit b35d423167591f2b7e63e66d2816f5a61aa737ed
24 changes: 18 additions & 6 deletions test/parallel/test-eslint-crypto-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,32 @@ new RuleTester().run('crypto-check', rule, {
'foo',
'crypto',
`
if (!common.hasCrypto) {
common.skip();
}
require('crypto');
if (!common.hasCrypto) {
common.skip();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this (and other occurences below) pass the test? I think the 'missing crypto' message is missing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the string 'missing crypto' in tests. But there's still something wrong. Any help?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done now.

}
require('crypto');
`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: unnecessary change.

],
invalid: [
{
code: 'require("crypto")',
errors: [{ message }]
errors: [{ message }],
output: `
if (!common.hasCrypto) {
common.skip();
}
require('crypto');
`
},
{
code: 'if (common.foo) {} require("crypto")',
errors: [{ message }]
errors: [{ message }],
output: `
if (!common.hasCrypto) {
common.skip();
}
require('crypto');
`
}
]
});
18 changes: 16 additions & 2 deletions tools/eslint-rules/crypto-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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".';
'when Node is built "--without-ssl".';

const cryptoModules = ['crypto', 'http2'];
const requireModules = cryptoModules.concat(['tls', 'https']);
Expand All @@ -23,13 +23,18 @@ const bindingModules = cryptoModules.concat(['tls_wrap']);
module.exports = function(context) {
const missingCheckNodes = [];
const requireNodes = [];
const commonModuleNodes = [];
var hasSkipCall = false;

function testCryptoUsage(node) {
if (utils.isRequired(node, requireModules) ||
utils.isBinding(node, bindingModules)) {
requireNodes.push(node);
}

if (utils.isCommonModule(node)) {
commonModuleNodes.push(node);
}
}

function testIfStatement(node) {
Expand Down Expand Up @@ -75,7 +80,16 @@ module.exports = function(context) {

function report(nodes) {
nodes.forEach((node) => {
context.report(node, msg);
context.report({
node,
message: msg,
fix: (fixer) => {
return fixer.insertTextAfter(
commonModuleNodes[0],
'\nif (!common.hasCrypto)\n common.skip("missing crypto");'
);
}
});
});
}

Expand Down
22 changes: 16 additions & 6 deletions tools/eslint-rules/rules-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ module.exports.isRequired = function(node, modules) {
modules.includes(node.arguments[0].value);
};

/**
* Return true if common module is required
* in AST Node under inspection
*/
var commonModuleRegExp = new RegExp(/^(\.\.\/)*common(\.js)?$/);
module.exports.isCommonModule = function(node) {
return node.callee.name === 'require' &&
commonModuleRegExp.test(node.arguments[0].value);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will produce access errors due to accessing nodes without arguments. Please guard against them the same as I suggested in the other PR.

};

/**
* Returns true if any of the passed in modules are used in
* binding calls.
Expand Down Expand Up @@ -45,8 +55,8 @@ module.exports.usesCommonProperty = function(node, properties) {
module.exports.inSkipBlock = function(node) {
var hasSkipBlock = false;
if (node.test &&
node.test.type === 'UnaryExpression' &&
node.test.operator === '!') {
node.test.type === 'UnaryExpression' &&
node.test.operator === '!') {
const consequent = node.consequent;
if (consequent.body) {
consequent.body.some(function(expressionStatement) {
Expand All @@ -64,8 +74,8 @@ module.exports.inSkipBlock = function(node) {

function hasSkip(expression) {
return expression &&
expression.callee &&
(expression.callee.name === 'skip' ||
expression.callee.property &&
expression.callee.property.name === 'skip');
expression.callee &&
(expression.callee.name === 'skip' ||
expression.callee.property &&
expression.callee.property.name === 'skip');
}