Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: lint for use of internalBinding() #25395

Merged
merged 1 commit into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions test/parallel/test-eslint-crypto-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ new RuleTester().run('crypto-check', rule, {
common.skip("missing crypto");
}
require("crypto");
`,
`
if (!common.hasCrypto) {
Copy link
Member

@joyeecheung joyeecheung Jan 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be in a separate test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. We don't have separate testing for tools/eslint-rules/rules-utils.js (nor do I think we need it at this point).

common.skip("missing crypto");
}
internalBinding("crypto");
`
],
invalid: [
Expand Down Expand Up @@ -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")'
}
]
});
13 changes: 7 additions & 6 deletions tools/eslint-rules/rules-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down