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 <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
cjihrig 2019-01-08 10:36:28 -05:00
parent c69ea3b1d5
commit cf9bcdeabe
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5
2 changed files with 25 additions and 6 deletions

View File

@ -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")'
}
]
});

View File

@ -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);
};
/**