|
1 | 1 | /** |
2 | 2 | * Rule: no-return-wrap function |
3 | | - * Prevents uneccessary wrapping of results in Promise.resolve |
| 3 | + * Prevents unnecessary wrapping of results in Promise.resolve |
4 | 4 | * or Promise.reject as the Promise will do that for us |
5 | 5 | */ |
6 | 6 |
|
@@ -49,26 +49,33 @@ module.exports = { |
49 | 49 | const options = context.options[0] || {} |
50 | 50 | const allowReject = options.allowReject |
51 | 51 |
|
| 52 | + /** |
| 53 | + * Checks a call expression, reporting if necessary. |
| 54 | + * @param callExpression The call expression. |
| 55 | + * @param node The node to report. |
| 56 | + */ |
| 57 | + function checkCallExpression({ callee }, node) { |
| 58 | + if ( |
| 59 | + isInPromise(context) && |
| 60 | + callee.type === 'MemberExpression' && |
| 61 | + callee.object.name === 'Promise' |
| 62 | + ) { |
| 63 | + if (callee.property.name === 'resolve') { |
| 64 | + context.report({ node, messageId: 'resolve' }) |
| 65 | + } else if (!allowReject && callee.property.name === 'reject') { |
| 66 | + context.report({ node, messageId: 'reject' }) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
52 | 71 | return { |
53 | 72 | ReturnStatement(node) { |
54 | | - if (isInPromise(context)) { |
55 | | - if (node.argument) { |
56 | | - if (node.argument.type === 'CallExpression') { |
57 | | - if (node.argument.callee.type === 'MemberExpression') { |
58 | | - if (node.argument.callee.object.name === 'Promise') { |
59 | | - if (node.argument.callee.property.name === 'resolve') { |
60 | | - context.report({ node, messageId: 'resolve' }) |
61 | | - } else if ( |
62 | | - !allowReject && |
63 | | - node.argument.callee.property.name === 'reject' |
64 | | - ) { |
65 | | - context.report({ node, messageId: 'reject' }) |
66 | | - } |
67 | | - } |
68 | | - } |
69 | | - } |
70 | | - } |
| 73 | + if (node.argument && node.argument.type === 'CallExpression') { |
| 74 | + checkCallExpression(node.argument, node) |
71 | 75 | } |
| 76 | + }, |
| 77 | + 'ArrowFunctionExpression > CallExpression'(node) { |
| 78 | + checkCallExpression(node, node) |
72 | 79 | } |
73 | 80 | } |
74 | 81 | } |
|
0 commit comments