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
Prev Previous commit
Next Next commit
tools: auto fix custom eslint rule for crypto-check.js
1. Refactors test
2. Removes commonModule AST node as array.

Refs : #16636
  • Loading branch information
shobhitchittora committed Feb 12, 2018
commit 2ecf517e565adb3a6a7480cae7e47dd1a7259ebd
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();
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");
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");
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 can not be the proper output because there is no common imported in the code.

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");
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 can not be the expected output because common is not imported. But not only that, the first statement if (common.foo) {} was just removed? That is definitely not right.

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