Skip to content
This repository was archived by the owner on Oct 2, 2021. It is now read-only.

Commit 5bf40c8

Browse files
edemainedferber90
authored andcommitted
feat: allow assignment of check() return value (#785)
* Allow simple assignment statements in check() calls * Documentation
1 parent 465df2b commit 5bf40c8

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

docs/rules/audit-argument-checks.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,26 @@ Meteor.methods({
4141
}
4242
})
4343

44+
Meteor.methods({
45+
foo: function (bar) {
46+
var ret;
47+
ret = check(bar, Match.Any)
48+
}
49+
})
50+
4451
```
4552

53+
For a check function to be considered "called", it must be called at the
54+
top level of the method or publish function (not e.g. within an `if` block),
55+
either as a lone expression statement or as an assignment statement where the
56+
right-hand side is just the function call (as in the last example above).
57+
4658
### Options
4759

4860
If you define your own functions that call `check`, you can provide a list of
4961
such functions via the configuration `checkEquivalents`. This rule assumes
5062
that these functions effectively check their first argument (an identifier or
51-
a list of identifiers).
63+
an array of identifiers).
5264

5365
For example, in `.eslintrc.json`, you can specify the following configuration:
5466

lib/rules/audit-argument-checks.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ module.exports = {
5252
}
5353

5454
const checkedParams = [];
55+
function processCheckArgs(args) {
56+
if (args.length > 0 && args[0].type === 'Identifier') {
57+
checkedParams.push(args[0].name);
58+
}
59+
if (args.length > 0 && args[0].type === 'ArrayExpression') {
60+
args[0].elements.forEach((element) => {
61+
if (element.type === 'Identifier') checkedParams.push(element.name);
62+
});
63+
}
64+
}
5565

5666
// short-circuit
5767
if (node.params.length === 0) {
@@ -64,24 +74,17 @@ module.exports = {
6474
expression.type === 'ExpressionStatement' &&
6575
expression.expression.type === 'CallExpression' &&
6676
expression.expression.callee.type === 'Identifier' &&
67-
isCheck(expression.expression) &&
68-
expression.expression.arguments.length > 0 &&
69-
expression.expression.arguments[0].type === 'Identifier'
77+
isCheck(expression.expression)
7078
) {
71-
checkedParams.push(expression.expression.arguments[0].name);
72-
}
73-
if (
79+
processCheckArgs(expression.expression.arguments);
80+
} else if (
7481
expression.type === 'ExpressionStatement' &&
75-
expression.expression.type === 'CallExpression' &&
76-
expression.expression.callee.type === 'Identifier' &&
77-
isCheck(expression.expression) &&
78-
expression.expression.arguments.length > 0 &&
79-
expression.expression.arguments[0].type === 'ArrayExpression'
82+
expression.expression.type === 'AssignmentExpression' &&
83+
expression.expression.right.type === 'CallExpression' &&
84+
expression.expression.right.callee.type === 'Identifier' &&
85+
isCheck(expression.expression.right)
8086
) {
81-
expression.expression.arguments[0].elements.forEach((element) => {
82-
if (element.type === 'Identifier')
83-
checkedParams.push(element.name);
84-
});
87+
processCheckArgs(expression.expression.right.arguments);
8588
}
8689
});
8790
}

tests/lib/rules/audit-argument-checks.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ ruleTester.run('audit-argument-checks', rule, {
4242
code: 'Meteor.publish("foo", function (bar) { checkId(bar); })',
4343
options: [{ checkEquivalents: ['checkId'] }],
4444
},
45+
{
46+
code:
47+
'Meteor.publish("foo", function (bar) { var r; r = checkId(bar); })',
48+
options: [{ checkEquivalents: ['checkId'] }],
49+
},
50+
{
51+
code: 'Meteor.publish("foo", function () { checkId(); })',
52+
options: [{ checkEquivalents: ['checkId'] }],
53+
},
4554

4655
'Meteor.methods()',
4756
'Meteor.methods({ x: function () {} })',

0 commit comments

Comments
 (0)