Skip to content

Commit

Permalink
tools: auto fix custom eslint rule for crypto-check.js
Browse files Browse the repository at this point in the history
1. Refactors test
2. Removes commonModule AST node as array.

Refs : #16636
  • Loading branch information
shobhitchittora committed Feb 12, 2018
1 parent 1f52446 commit 2ecf517
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
11 changes: 5 additions & 6 deletions test/parallel/test-eslint-crypto-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ new RuleTester().run('crypto-check', rule, {
valid: [
'foo',
'crypto',
`
if (!common.hasCrypto) {
`if (!common.hasCrypto) {
common.skip();
}
require("crypto");
Expand All @@ -23,8 +22,8 @@ new RuleTester().run('crypto-check', rule, {
{
code: 'require("crypto")',
errors: [{ message }],
output: `
if (!common.hasCrypto) {
output:
`if (!common.hasCrypto) {
common.skip("missing crypto");
}
require("crypto");
Expand All @@ -33,8 +32,8 @@ new RuleTester().run('crypto-check', rule, {
{
code: 'if (common.foo) {} require("crypto")',
errors: [{ message }],
output: `
if (!common.hasCrypto) {
output:
`if (!common.hasCrypto) {
common.skip("missing crypto");
}
require("crypto");
Expand Down
12 changes: 7 additions & 5 deletions tools/eslint-rules/crypto-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const bindingModules = cryptoModules.concat(['tls_wrap']);
module.exports = function(context) {
const missingCheckNodes = [];
const requireNodes = [];
const commonModuleNodes = [];
var commonModuleNode = null;
var hasSkipCall = false;

function testCryptoUsage(node) {
Expand All @@ -33,7 +33,7 @@ module.exports = function(context) {
}

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

Expand Down Expand Up @@ -84,10 +84,12 @@ module.exports = function(context) {
node,
message: msg,
fix: (fixer) => {
if (commonModuleNodes.length) {
if (commonModuleNode) {
return fixer.insertTextAfter(
commonModuleNodes[0],
'\nif (!common.hasCrypto)\n common.skip("missing crypto");'
commonModuleNode,
`\nif (!common.hasCrypto) {
common.skip("missing crypto");
}`
);
}
}
Expand Down

0 comments on commit 2ecf517

Please sign in to comment.