Skip to content

Commit

Permalink
fix: prefer-explicit-assert raising error on destructuring statement (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
afontcu authored and Thomas Lombart committed Nov 6, 2019
1 parent 629a7c1 commit ac76e41
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/rules/prefer-explicit-assert.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

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

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

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

const isValidQuery = (node, customQueryNames = []) =>
ALL_GET_BY_QUERIES.includes(node.name) ||
Expand All @@ -19,11 +19,17 @@ const isDirectlyCalledByFunction = node =>
const isReturnedByArrowFunctionExpression = node =>
node.parent.type === 'ArrowFunctionExpression';

const isDeclared = node => node.parent.type === 'VariableDeclarator';
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';

module.exports = {
meta: {
type: 'suggestion',
Expand Down Expand Up @@ -63,6 +69,7 @@ module.exports = {

if (
isValidQuery(node, customQueryNames) &&
!isInDestructuringStatement(node) &&
!isDirectlyCalledByFunction(callExpressionNode) &&
!isReturnedByArrowFunctionExpression(callExpressionNode) &&
!isDeclared(callExpressionNode) &&
Expand Down
11 changes: 11 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@ const ALL_QUERIES_COMBINATIONS = [
ASYNC_QUERIES_COMBINATIONS,
];

const findParent = (node, cb) => {
if (cb(node)) {
return node;
} else if (node.parent) {
return findParent(node.parent, cb);
}

return null;
};

module.exports = {
findParent,
getDocsUrl,
SYNC_QUERIES_VARIANTS,
ASYNC_QUERIES_VARIANTS,
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/prefer-explicit-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ ruleTester.run('prefer-explicit-assert', rule, {
{
code: `getByIcon('foo')`, // custom `getBy` query not extended through options
},
{
code: `const { getByText } = render()`,
},
{
code: `it('test', () => { const { getByText } = render() })`,
},
{
code: `it('test', () => { const [ getByText ] = render() })`,
},
{
code: `const a = [ getByText('foo') ]`,
},
{
code: `const a = { foo: getByText('bar') }`,
},
],

invalid: [
Expand Down

0 comments on commit ac76e41

Please sign in to comment.