|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const { findParent, getDocsUrl, ALL_QUERIES_METHODS } = require('../utils'); |
| 3 | +const { getDocsUrl, ALL_QUERIES_METHODS } = require('../utils'); |
4 | 4 |
|
5 | 5 | const ALL_GET_BY_QUERIES = ALL_QUERIES_METHODS.map( |
6 | 6 | queryMethod => `get${queryMethod}` |
7 | 7 | ); |
8 | 8 |
|
9 | | -const findCallExpressionParent = node => |
10 | | - findParent(node, node => node.type === 'CallExpression'); |
11 | | - |
12 | 9 | const isValidQuery = (node, customQueryNames = []) => |
13 | 10 | ALL_GET_BY_QUERIES.includes(node.name) || |
14 | 11 | customQueryNames.includes(node.name); |
15 | 12 |
|
16 | | -const isDirectlyCalledByFunction = node => |
17 | | - node.parent.type === 'CallExpression'; |
18 | | - |
19 | | -const isReturnedByArrowFunctionExpression = node => |
20 | | - node.parent.type === 'ArrowFunctionExpression'; |
21 | | - |
22 | | -const isDeclared = node => |
23 | | - !!findParent(node, node => node.type === 'VariableDeclarator'); |
24 | | - |
25 | | -const isReturnedByReturnStatement = node => |
26 | | - node.parent.type === 'ReturnStatement'; |
27 | | - |
28 | | -const isInDestructuringStatement = node => |
29 | | - (node.parent.type === 'Property' && |
30 | | - node.parent.parent.type === 'ObjectPattern') || |
31 | | - node.parent.type === 'ArrayPattern'; |
| 13 | +const isAtTopLevel = node => node.parent.parent.type === 'ExpressionStatement'; |
32 | 14 |
|
33 | 15 | module.exports = { |
34 | 16 | meta: { |
@@ -58,28 +40,32 @@ module.exports = { |
58 | 40 | }, |
59 | 41 |
|
60 | 42 | create: function(context) { |
| 43 | + const getQueryCalls = []; |
| 44 | + const customQueryNames = |
| 45 | + (context.options && context.options.length > 0 |
| 46 | + ? context.options[0].customQueryNames |
| 47 | + : []) || []; |
| 48 | + |
61 | 49 | return { |
62 | 50 | 'CallExpression Identifier'(node) { |
63 | | - const callExpressionNode = findCallExpressionParent(node); |
64 | | - |
65 | | - let customQueryNames; |
66 | | - if (context.options && context.options.length > 0) { |
67 | | - [{ customQueryNames }] = context.options; |
| 51 | + if (isValidQuery(node, customQueryNames)) { |
| 52 | + getQueryCalls.push(node); |
68 | 53 | } |
| 54 | + }, |
| 55 | + 'Program:exit'() { |
| 56 | + getQueryCalls.forEach(queryCall => { |
| 57 | + const node = |
| 58 | + queryCall.parent.type === 'MemberExpression' |
| 59 | + ? queryCall.parent |
| 60 | + : queryCall; |
69 | 61 |
|
70 | | - if ( |
71 | | - isValidQuery(node, customQueryNames) && |
72 | | - !isInDestructuringStatement(node) && |
73 | | - !isDirectlyCalledByFunction(callExpressionNode) && |
74 | | - !isReturnedByArrowFunctionExpression(callExpressionNode) && |
75 | | - !isDeclared(callExpressionNode) && |
76 | | - !isReturnedByReturnStatement(callExpressionNode) |
77 | | - ) { |
78 | | - context.report({ |
79 | | - node, |
80 | | - messageId: 'preferExplicitAssert', |
81 | | - }); |
82 | | - } |
| 62 | + if (isAtTopLevel(node)) { |
| 63 | + context.report({ |
| 64 | + node: queryCall, |
| 65 | + messageId: 'preferExplicitAssert', |
| 66 | + }); |
| 67 | + } |
| 68 | + }); |
83 | 69 | }, |
84 | 70 | }; |
85 | 71 | }, |
|
0 commit comments