-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
/
Copy pathprefer-common-mustsucceed.js
68 lines (60 loc) Β· 2.17 KB
/
prefer-common-mustsucceed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
const mustCall = 'CallExpression[callee.object.name="common"]' +
'[callee.property.name="mustCall"]';
module.exports = {
create(context) {
function isAssertIfError(node) {
return node.type === 'MemberExpression' &&
node.object.type === 'Identifier' &&
node.object.name === 'assert' &&
node.property.type === 'Identifier' &&
node.property.name === 'ifError';
}
function isCallToIfError(node, errName) {
return node.type === 'CallExpression' &&
isAssertIfError(node.callee) &&
node.arguments.length > 0 &&
node.arguments[0].type === 'Identifier' &&
node.arguments[0].name === errName;
}
function bodyStartsWithCallToIfError(body, errName) {
while (body.type === 'BlockStatement' && body.body.length > 0)
body = body.body[0];
let expr;
switch (body.type) {
case 'ReturnStatement':
expr = body.argument;
break;
case 'ExpressionStatement':
expr = body.expression;
break;
default:
expr = body;
}
return isCallToIfError(expr, errName);
}
return {
[`${mustCall}:exit`]: (mustCall) => {
if (mustCall.arguments.length > 0) {
const callback = mustCall.arguments[0];
if (isAssertIfError(callback)) {
context.report(mustCall, 'Please use common.mustSucceed instead of ' +
'common.mustCall(assert.ifError).');
}
if (callback.type === 'ArrowFunctionExpression' ||
callback.type === 'FunctionExpression') {
if (callback.params.length > 0 &&
callback.params[0].type === 'Identifier') {
const errName = callback.params[0].name;
if (bodyStartsWithCallToIfError(callback.body, errName)) {
context.report(mustCall, 'Please use common.mustSucceed instead' +
' of common.mustCall with' +
' assert.ifError.');
}
}
}
}
},
};
},
};