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