Skip to content

Commit 7b8c2c6

Browse files
thomaslombartBelco90
authored andcommitted
fix(prefer-explicit-assert): check if node property is accessed (#52)
In cases like `expect(getBy("foo").bar)`, `bar` was considered as an identifier thus triggering the visited function (`CallExpression Identifier`) and reporting an error . Instead of adding another check, the implementation is simpler: checking whether the node is at the top level or not (`ExpressionStatement`).
1 parent ea8816c commit 7b8c2c6

File tree

2 files changed

+27
-38
lines changed

2 files changed

+27
-38
lines changed

lib/rules/prefer-explicit-assert.js

+24-38
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,16 @@
11
'use strict';
22

3-
const { findParent, getDocsUrl, ALL_QUERIES_METHODS } = require('../utils');
3+
const { getDocsUrl, ALL_QUERIES_METHODS } = require('../utils');
44

55
const ALL_GET_BY_QUERIES = ALL_QUERIES_METHODS.map(
66
queryMethod => `get${queryMethod}`
77
);
88

9-
const findCallExpressionParent = node =>
10-
findParent(node, node => node.type === 'CallExpression');
11-
129
const isValidQuery = (node, customQueryNames = []) =>
1310
ALL_GET_BY_QUERIES.includes(node.name) ||
1411
customQueryNames.includes(node.name);
1512

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';
3214

3315
module.exports = {
3416
meta: {
@@ -58,28 +40,32 @@ module.exports = {
5840
},
5941

6042
create: function(context) {
43+
const getQueryCalls = [];
44+
const customQueryNames =
45+
(context.options && context.options.length > 0
46+
? context.options[0].customQueryNames
47+
: []) || [];
48+
6149
return {
6250
'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);
6853
}
54+
},
55+
'Program:exit'() {
56+
getQueryCalls.forEach(queryCall => {
57+
const node =
58+
queryCall.parent.type === 'MemberExpression'
59+
? queryCall.parent
60+
: queryCall;
6961

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+
});
8369
},
8470
};
8571
},

tests/lib/rules/prefer-explicit-assert.js

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ ruleTester.run('prefer-explicit-assert', rule, {
3232
{
3333
code: `expect(getByText('foo')).toBeInTheDocument();`,
3434
},
35+
{
36+
code: `expect(getByText('foo').bar).toBeInTheDocument()`,
37+
},
3538
{
3639
code: `async () => { await waitForElement(() => getByText('foo')) }`,
3740
},

0 commit comments

Comments
 (0)