Skip to content

Commit

Permalink
tools: prepare ESLint rules for 8.0.0 requirements
Browse files Browse the repository at this point in the history
PR-URL: #40394
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
Trott authored and targos committed Oct 13, 2021
1 parent 282b6eb commit 43b97c7
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 122 deletions.
167 changes: 84 additions & 83 deletions tools/eslint-rules/crypto-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,107 +20,108 @@ const cryptoModules = ['crypto', 'http2'];
const requireModules = cryptoModules.concat(['tls', 'https']);
const bindingModules = cryptoModules.concat(['tls_wrap']);

module.exports = function(context) {
const missingCheckNodes = [];
const requireNodes = [];
let commonModuleNode = null;
let hasSkipCall = false;
module.exports = {
meta: {
fixable: 'code',
},
create(context) {
const missingCheckNodes = [];
const requireNodes = [];
let commonModuleNode = null;
let hasSkipCall = false;

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

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

if (utils.isCommonModule(node)) {
commonModuleNode = node;
function testIfStatement(node) {
if (node.test.argument === undefined) {
return;
}
if (isCryptoCheck(node.test.argument)) {
checkCryptoCall(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 isCryptoCheck(node) {
return utils.usesCommonProperty(node, ['hasCrypto', 'hasFipsCrypto']);
}

function checkCryptoCall(node) {
if (utils.inSkipBlock(node)) {
hasSkipCall = true;
} else {
missingCheckNodes.push(node);
function checkCryptoCall(node) {
if (utils.inSkipBlock(node)) {
hasSkipCall = true;
} else {
missingCheckNodes.push(node);
}
}
}

function testMemberExpression(node) {
if (isCryptoCheck(node)) {
checkCryptoCall(node);
function testMemberExpression(node) {
if (isCryptoCheck(node)) {
checkCryptoCall(node);
}
}
}

function reportIfMissingCheck() {
if (hasSkipCall) {
// There is a skip, which is good, but verify that the require() calls
// in question come after at least one check.
if (missingCheckNodes.length > 0) {
requireNodes.forEach((requireNode) => {
const beforeAllChecks = missingCheckNodes.every((checkNode) => {
return requireNode.range[0] < checkNode.range[0];
});

if (beforeAllChecks) {
context.report({
node: requireNode,
message: msg
function reportIfMissingCheck() {
if (hasSkipCall) {
// There is a skip, which is good, but verify that the require() calls
// in question come after at least one check.
if (missingCheckNodes.length > 0) {
requireNodes.forEach((requireNode) => {
const beforeAllChecks = missingCheckNodes.every((checkNode) => {
return requireNode.range[0] < checkNode.range[0];
});
}
});

if (beforeAllChecks) {
context.report({
node: requireNode,
message: msg
});
}
});
}
return;
}
return;
}

if (requireNodes.length > 0) {
if (missingCheckNodes.length > 0) {
report(missingCheckNodes);
} else {
report(requireNodes);
if (requireNodes.length > 0) {
if (missingCheckNodes.length > 0) {
report(missingCheckNodes);
} else {
report(requireNodes);
}
}
}
}

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

return {
'CallExpression': (node) => testCryptoUsage(node),
'IfStatement:exit': (node) => testIfStatement(node),
'MemberExpression:exit': (node) => testMemberExpression(node),
'Program:exit': () => reportIfMissingCheck()
};
};
}

module.exports.meta = {
fixable: 'code'
return {
'CallExpression': (node) => testCryptoUsage(node),
'IfStatement:exit': (node) => testIfStatement(node),
'MemberExpression:exit': (node) => testMemberExpression(node),
'Program:exit': () => reportIfMissingCheck()
};
}
};
79 changes: 40 additions & 39 deletions tools/eslint-rules/inspector-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,54 @@ const utils = require('./rules-utils.js');
const msg = 'Please add a skipIfInspectorDisabled() call to allow this ' +
'test to be skipped when Node is built \'--without-inspector\'.';

module.exports = function(context) {
const missingCheckNodes = [];
let commonModuleNode = null;
let hasInspectorCheck = false;
module.exports = {
meta: {
fixable: 'code',
},
create(context) {
const missingCheckNodes = [];
let commonModuleNode = null;
let hasInspectorCheck = false;

function testInspectorUsage(context, node) {
if (utils.isRequired(node, ['inspector'])) {
missingCheckNodes.push(node);
}
function testInspectorUsage(context, node) {
if (utils.isRequired(node, ['inspector'])) {
missingCheckNodes.push(node);
}

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

function checkMemberExpression(context, node) {
if (utils.usesCommonProperty(node, ['skipIfInspectorDisabled'])) {
hasInspectorCheck = true;
function checkMemberExpression(context, node) {
if (utils.usesCommonProperty(node, ['skipIfInspectorDisabled'])) {
hasInspectorCheck = true;
}
}
}

function reportIfMissing(context) {
if (!hasInspectorCheck) {
missingCheckNodes.forEach((node) => {
context.report({
node,
message: msg,
fix: (fixer) => {
if (commonModuleNode) {
return fixer.insertTextAfter(
commonModuleNode,
'\ncommon.skipIfInspectorDisabled();'
);
function reportIfMissing(context) {
if (!hasInspectorCheck) {
missingCheckNodes.forEach((node) => {
context.report({
node,
message: msg,
fix: (fixer) => {
if (commonModuleNode) {
return fixer.insertTextAfter(
commonModuleNode,
'\ncommon.skipIfInspectorDisabled();'
);
}
}
}
});
});
});
}
}
}

return {
'CallExpression': (node) => testInspectorUsage(context, node),
'MemberExpression': (node) => checkMemberExpression(context, node),
'Program:exit': () => reportIfMissing(context)
};
};

module.exports.meta = {
fixable: 'code'
return {
'CallExpression': (node) => testInspectorUsage(context, node),
'MemberExpression': (node) => checkMemberExpression(context, node),
'Program:exit': () => reportIfMissing(context)
};
}
};

0 comments on commit 43b97c7

Please sign in to comment.