Skip to content

Commit

Permalink
fix: Avoid TypeError when exec stub is used with no arguments (#97)
Browse files Browse the repository at this point in the history
This extends the fix in 7f97815

In some situations, such as code using a sinon stub, a
TypeError is thrown as the AST node has no 'arguments' property.

Closes 69
Refs 69

Co-authored-by: Luke Hudson <lucas@speak.geek.nz>
  • Loading branch information
lingo and Luke Hudson committed Dec 6, 2022
1 parent 08ba476 commit 9c18f16
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rules/detect-child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {
},
MemberExpression: function (node) {
if (node.property.name === 'exec' && names.indexOf(node.object.name) > -1) {
if (node.parent && node.parent.arguments.length && node.parent.arguments[0].type !== 'Literal') {
if (node.parent && node.parent.arguments && node.parent.arguments.length && node.parent.arguments[0].type !== 'Literal') {
return context.report({ node: node, message: 'Found child_process.exec() with non Literal first argument' });
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/detect-child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ tester.run(ruleName, rule, {
code: "var child = require('child_process'); child.exec()",
errors: [{ message: 'Found require("child_process")' }],
},
{
code: "var child = sinon.stub(require('child_process')); child.exec.returns({});",
errors: [{ message: 'Found require("child_process")' }],
},
],
});

0 comments on commit 9c18f16

Please sign in to comment.