Skip to content

Commit 50bcfdd

Browse files
committed
fix(prefer-explicit-assert): allow object declarations
1 parent 6f8bfc4 commit 50bcfdd

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

lib/rules/prefer-explicit-assert.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22

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

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

99
const findCallExpressionParent = node =>
10-
node.type === 'CallExpression' ? node : findCallExpressionParent(node.parent);
10+
findParent(node, node => node.type === 'CallExpression');
1111

1212
const isValidQuery = (node, customQueryNames = []) =>
1313
ALL_GET_BY_QUERIES.includes(node.name) ||
@@ -19,14 +19,12 @@ const isDirectlyCalledByFunction = node =>
1919
const isReturnedByArrowFunctionExpression = node =>
2020
node.parent.type === 'ArrowFunctionExpression';
2121

22-
const isDeclared = node => node.parent.type === 'VariableDeclarator';
22+
const isDeclared = node =>
23+
!!findParent(node, node => node.type === 'VariableDeclarator');
2324

2425
const isReturnedByReturnStatement = node =>
2526
node.parent.type === 'ReturnStatement';
2627

27-
const isInsideArrayDeclaration = node =>
28-
node.parent.parent.type === 'ArrayExpression';
29-
3028
const isInDestructuringStatement = node =>
3129
(node.parent.type === 'Property' &&
3230
node.parent.parent.type === 'ObjectPattern') ||
@@ -72,7 +70,6 @@ module.exports = {
7270
if (
7371
isValidQuery(node, customQueryNames) &&
7472
!isInDestructuringStatement(node) &&
75-
!isInsideArrayDeclaration(node) &&
7673
!isDirectlyCalledByFunction(callExpressionNode) &&
7774
!isReturnedByArrowFunctionExpression(callExpressionNode) &&
7875
!isDeclared(callExpressionNode) &&

lib/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,18 @@ const ALL_QUERIES_COMBINATIONS = [
4848
ASYNC_QUERIES_COMBINATIONS,
4949
];
5050

51+
const findParent = (node, cb) => {
52+
if (cb(node)) {
53+
return node;
54+
} else if (node.parent) {
55+
return findParent(node.parent, cb);
56+
} else {
57+
return null;
58+
}
59+
};
60+
5161
module.exports = {
62+
findParent,
5263
getDocsUrl,
5364
SYNC_QUERIES_VARIANTS,
5465
ASYNC_QUERIES_VARIANTS,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ ruleTester.run('prefer-explicit-assert', rule, {
6262
{
6363
code: `const a = [ getByText('foo') ]`,
6464
},
65+
{
66+
code: `const a = { foo: getByText('bar') }`,
67+
},
6568
],
6669

6770
invalid: [

0 commit comments

Comments
 (0)