Skip to content

Commit 2ecf517

Browse files
tools: auto fix custom eslint rule for crypto-check.js
1. Refactors test 2. Removes commonModule AST node as array. Refs : #16636
1 parent 1f52446 commit 2ecf517

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

test/parallel/test-eslint-crypto-check.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ new RuleTester().run('crypto-check', rule, {
1212
valid: [
1313
'foo',
1414
'crypto',
15-
`
16-
if (!common.hasCrypto) {
15+
`if (!common.hasCrypto) {
1716
common.skip();
1817
}
1918
require("crypto");
@@ -23,8 +22,8 @@ new RuleTester().run('crypto-check', rule, {
2322
{
2423
code: 'require("crypto")',
2524
errors: [{ message }],
26-
output: `
27-
if (!common.hasCrypto) {
25+
output:
26+
`if (!common.hasCrypto) {
2827
common.skip("missing crypto");
2928
}
3029
require("crypto");
@@ -33,8 +32,8 @@ new RuleTester().run('crypto-check', rule, {
3332
{
3433
code: 'if (common.foo) {} require("crypto")',
3534
errors: [{ message }],
36-
output: `
37-
if (!common.hasCrypto) {
35+
output:
36+
`if (!common.hasCrypto) {
3837
common.skip("missing crypto");
3938
}
4039
require("crypto");

tools/eslint-rules/crypto-check.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const bindingModules = cryptoModules.concat(['tls_wrap']);
2323
module.exports = function(context) {
2424
const missingCheckNodes = [];
2525
const requireNodes = [];
26-
const commonModuleNodes = [];
26+
var commonModuleNode = null;
2727
var hasSkipCall = false;
2828

2929
function testCryptoUsage(node) {
@@ -33,7 +33,7 @@ module.exports = function(context) {
3333
}
3434

3535
if (utils.isCommonModule(node)) {
36-
commonModuleNodes.push(node);
36+
commonModuleNode = node;
3737
}
3838
}
3939

@@ -84,10 +84,12 @@ module.exports = function(context) {
8484
node,
8585
message: msg,
8686
fix: (fixer) => {
87-
if (commonModuleNodes.length) {
87+
if (commonModuleNode) {
8888
return fixer.insertTextAfter(
89-
commonModuleNodes[0],
90-
'\nif (!common.hasCrypto)\n common.skip("missing crypto");'
89+
commonModuleNode,
90+
`\nif (!common.hasCrypto) {
91+
common.skip("missing crypto");
92+
}`
9193
);
9294
}
9395
}

0 commit comments

Comments
 (0)