Skip to content

Commit

Permalink
fix(prefer-explicit-assert): check if node property is accessed (#52)
Browse files Browse the repository at this point in the history
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`).
  • Loading branch information
Thomas Lombart authored and Belco90 committed Dec 10, 2019
1 parent ea8816c commit 7b8c2c6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 38 deletions.
62 changes: 24 additions & 38 deletions lib/rules/prefer-explicit-assert.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
'use strict';

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

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

const findCallExpressionParent = node =>
findParent(node, node => node.type === 'CallExpression');

const isValidQuery = (node, customQueryNames = []) =>
ALL_GET_BY_QUERIES.includes(node.name) ||
customQueryNames.includes(node.name);

const isDirectlyCalledByFunction = node =>
node.parent.type === 'CallExpression';

const isReturnedByArrowFunctionExpression = node =>
node.parent.type === 'ArrowFunctionExpression';

const isDeclared = node =>
!!findParent(node, node => node.type === 'VariableDeclarator');

const isReturnedByReturnStatement = node =>
node.parent.type === 'ReturnStatement';

const isInDestructuringStatement = node =>
(node.parent.type === 'Property' &&
node.parent.parent.type === 'ObjectPattern') ||
node.parent.type === 'ArrayPattern';
const isAtTopLevel = node => node.parent.parent.type === 'ExpressionStatement';

module.exports = {
meta: {
Expand Down Expand Up @@ -58,28 +40,32 @@ module.exports = {
},

create: function(context) {
const getQueryCalls = [];
const customQueryNames =
(context.options && context.options.length > 0
? context.options[0].customQueryNames
: []) || [];

return {
'CallExpression Identifier'(node) {
const callExpressionNode = findCallExpressionParent(node);

let customQueryNames;
if (context.options && context.options.length > 0) {
[{ customQueryNames }] = context.options;
if (isValidQuery(node, customQueryNames)) {
getQueryCalls.push(node);
}
},
'Program:exit'() {
getQueryCalls.forEach(queryCall => {
const node =
queryCall.parent.type === 'MemberExpression'
? queryCall.parent
: queryCall;

if (
isValidQuery(node, customQueryNames) &&
!isInDestructuringStatement(node) &&
!isDirectlyCalledByFunction(callExpressionNode) &&
!isReturnedByArrowFunctionExpression(callExpressionNode) &&
!isDeclared(callExpressionNode) &&
!isReturnedByReturnStatement(callExpressionNode)
) {
context.report({
node,
messageId: 'preferExplicitAssert',
});
}
if (isAtTopLevel(node)) {
context.report({
node: queryCall,
messageId: 'preferExplicitAssert',
});
}
});
},
};
},
Expand Down
3 changes: 3 additions & 0 deletions tests/lib/rules/prefer-explicit-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ ruleTester.run('prefer-explicit-assert', rule, {
{
code: `expect(getByText('foo')).toBeInTheDocument();`,
},
{
code: `expect(getByText('foo').bar).toBeInTheDocument()`,
},
{
code: `async () => { await waitForElement(() => getByText('foo')) }`,
},
Expand Down

0 comments on commit 7b8c2c6

Please sign in to comment.