From d159b827952b48f9ae6f6e61c0d86752e8341da9 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Tue, 8 Jan 2019 10:36:28 -0500 Subject: [PATCH] tools: lint for use of internalBinding() Use of process.binding() has largely been replaced by internalBinding(). This commit updates the custom crypto check ESLint rule to check for both process.binding() and internalBinding(). Refs: https://github.com/nodejs/node/pull/24952 PR-URL: https://github.com/nodejs/node/pull/25395 Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Ruben Bridgewater Reviewed-By: Joyee Cheung --- test/parallel/test-eslint-crypto-check.js | 18 ++++++++++++++++++ tools/eslint-rules/rules-utils.js | 13 +++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/test/parallel/test-eslint-crypto-check.js b/test/parallel/test-eslint-crypto-check.js index 1829e86b020313..2325a04354c53b 100644 --- a/test/parallel/test-eslint-crypto-check.js +++ b/test/parallel/test-eslint-crypto-check.js @@ -19,6 +19,12 @@ new RuleTester().run('crypto-check', rule, { common.skip("missing crypto"); } require("crypto"); + `, + ` + if (!common.hasCrypto) { + common.skip("missing crypto"); + } + internalBinding("crypto"); ` ], invalid: [ @@ -51,6 +57,18 @@ new RuleTester().run('crypto-check', rule, { '}\n' + 'if (common.foo) {}\n' + 'require("crypto")' + }, + { + code: 'require("common")\n' + + 'if (common.foo) {}\n' + + 'internalBinding("crypto")', + errors: [{ message }], + output: 'require("common")\n' + + 'if (!common.hasCrypto) {' + + ' common.skip("missing crypto");' + + '}\n' + + 'if (common.foo) {}\n' + + 'internalBinding("crypto")' } ] }); diff --git a/tools/eslint-rules/rules-utils.js b/tools/eslint-rules/rules-utils.js index 0e89a715450edb..315e7ca0adf2ef 100644 --- a/tools/eslint-rules/rules-utils.js +++ b/tools/eslint-rules/rules-utils.js @@ -33,14 +33,15 @@ module.exports.isCommonModule = function(node) { /** * Returns true if any of the passed in modules are used in - * binding calls. + * process.binding() or internalBinding() calls. */ module.exports.isBinding = function(node, modules) { - if (node.callee.object) { - return node.callee.object.name === 'process' && - node.callee.property.name === 'binding' && - modules.includes(node.arguments[0].value); - } + const isProcessBinding = node.callee.object && + node.callee.object.name === 'process' && + node.callee.property.name === 'binding'; + + return (isProcessBinding || node.callee.name === 'internalBinding') && + modules.includes(node.arguments[0].value); }; /**