Skip to content

Commit ac76e41

Browse files
afontcuthomaslombart
authored andcommitted
fix: prefer-explicit-assert raising error on destructuring statement (#43)
* fix(prefer-explicit-assert): avoid raising error on destructuring * fix(prefer-explicit-assert): avoid raising error on array statements * fix(prefer-explicit-assert): allow object declarations
1 parent 629a7c1 commit ac76e41

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

lib/rules/prefer-explicit-assert.js

+10-3
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,11 +19,17 @@ 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

28+
const isInDestructuringStatement = node =>
29+
(node.parent.type === 'Property' &&
30+
node.parent.parent.type === 'ObjectPattern') ||
31+
node.parent.type === 'ArrayPattern';
32+
2733
module.exports = {
2834
meta: {
2935
type: 'suggestion',
@@ -63,6 +69,7 @@ module.exports = {
6369

6470
if (
6571
isValidQuery(node, customQueryNames) &&
72+
!isInDestructuringStatement(node) &&
6673
!isDirectlyCalledByFunction(callExpressionNode) &&
6774
!isReturnedByArrowFunctionExpression(callExpressionNode) &&
6875
!isDeclared(callExpressionNode) &&

lib/utils.js

+11
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+
}
57+
58+
return null;
59+
};
60+
5161
module.exports = {
62+
findParent,
5263
getDocsUrl,
5364
SYNC_QUERIES_VARIANTS,
5465
ASYNC_QUERIES_VARIANTS,

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

+15
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ ruleTester.run('prefer-explicit-assert', rule, {
5050
{
5151
code: `getByIcon('foo')`, // custom `getBy` query not extended through options
5252
},
53+
{
54+
code: `const { getByText } = render()`,
55+
},
56+
{
57+
code: `it('test', () => { const { getByText } = render() })`,
58+
},
59+
{
60+
code: `it('test', () => { const [ getByText ] = render() })`,
61+
},
62+
{
63+
code: `const a = [ getByText('foo') ]`,
64+
},
65+
{
66+
code: `const a = { foo: getByText('bar') }`,
67+
},
5368
],
5469

5570
invalid: [

0 commit comments

Comments
 (0)