Skip to content

Fix prefer-explicit-assert raising error on destructuring statement #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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