eslint-plugin-testing-library
Advanced tools
Comparing version 1.3.3 to 1.3.4
'use strict'; | ||
const { findParent, getDocsUrl, ALL_QUERIES_METHODS } = require('../utils'); | ||
const { getDocsUrl, ALL_QUERIES_METHODS } = require('../utils'); | ||
@@ -9,26 +9,8 @@ const ALL_GET_BY_QUERIES = ALL_QUERIES_METHODS.map( | ||
const findCallExpressionParent = node => | ||
findParent(node, node => node.type === 'CallExpression'); | ||
const isValidQuery = (node, customQueryNames = []) => | ||
const isValidQuery = (node, customQueryNames) => | ||
ALL_GET_BY_QUERIES.includes(node.name) || | ||
customQueryNames.includes(node.name); | ||
const isDirectlyCalledByFunction = node => | ||
node.parent.type === 'CallExpression'; | ||
const isAtTopLevel = node => node.parent.parent.type === 'ExpressionStatement'; | ||
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'; | ||
module.exports = { | ||
@@ -62,24 +44,28 @@ meta: { | ||
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', | ||
}); | ||
} | ||
}); | ||
}, | ||
@@ -86,0 +72,0 @@ }; |
@@ -51,14 +51,3 @@ 'use strict'; | ||
const findParent = (node, cb) => { | ||
if (cb(node)) { | ||
return node; | ||
} else if (node.parent) { | ||
return findParent(node.parent, cb); | ||
} | ||
return null; | ||
}; | ||
module.exports = { | ||
findParent, | ||
getDocsUrl, | ||
@@ -65,0 +54,0 @@ SYNC_QUERIES_VARIANTS, |
{ | ||
"name": "eslint-plugin-testing-library", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"description": "ESLint rules for Testing Library", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32136
601