eslint-plugin-testing-library
Advanced tools
Comparing version
@@ -196,2 +196,3 @@ "use strict"; | ||
'AwaitExpression > CallExpression'(node) { | ||
var _a; | ||
if (!utils_1.ASTUtils.isIdentifier(node.callee) || | ||
@@ -202,6 +203,55 @@ !helpers.isAsyncUtil(node.callee, ['waitFor'])) { | ||
const argument = node.arguments[0]; | ||
if (!(0, node_utils_1.isArrowFunctionExpression)(argument) || | ||
!(0, node_utils_1.isCallExpression)(argument.body)) { | ||
if (!(0, node_utils_1.isArrowFunctionExpression)(argument)) { | ||
return; | ||
} | ||
if ((0, node_utils_1.isBlockStatement)(argument.body) && argument.async) { | ||
const { body } = argument.body; | ||
const declarations = (_a = body | ||
.filter(node_utils_1.isVariableDeclaration)) === null || _a === void 0 ? void 0 : _a.flatMap((declaration) => declaration.declarations); | ||
const findByDeclarator = declarations.find((declaration) => { | ||
if (!utils_1.ASTUtils.isAwaitExpression(declaration.init) || | ||
!(0, node_utils_1.isCallExpression)(declaration.init.argument)) { | ||
return false; | ||
} | ||
const { callee } = declaration.init.argument; | ||
const node = (0, node_utils_1.getDeepestIdentifierNode)(callee); | ||
return node ? helpers.isFindQueryVariant(node) : false; | ||
}); | ||
const init = utils_1.ASTUtils.isAwaitExpression(findByDeclarator === null || findByDeclarator === void 0 ? void 0 : findByDeclarator.init) | ||
? findByDeclarator.init.argument | ||
: null; | ||
if (!(0, node_utils_1.isCallExpression)(init)) { | ||
return; | ||
} | ||
const queryIdentifier = (0, node_utils_1.getDeepestIdentifierNode)(init.callee); | ||
if (!queryIdentifier || !helpers.isAsyncQuery(queryIdentifier)) { | ||
return; | ||
} | ||
const fullQueryMethod = queryIdentifier.name; | ||
const queryMethod = fullQueryMethod.split('By')[1]; | ||
const queryVariant = getFindByQueryVariant(fullQueryMethod); | ||
reportInvalidUsage(node, { | ||
queryMethod, | ||
queryVariant, | ||
prevQuery: fullQueryMethod, | ||
fix(fixer) { | ||
const { parent: expressionStatement } = node.parent; | ||
const bodyText = sourceCode | ||
.getText(argument.body) | ||
.slice(1, -1) | ||
.trim(); | ||
const { line, column } = expressionStatement.loc.start; | ||
const indent = sourceCode.getLines()[line - 1].slice(0, column); | ||
const newText = bodyText | ||
.split('\n') | ||
.map((line) => line.trim()) | ||
.join(`\n${indent}`); | ||
return fixer.replaceText(expressionStatement, newText); | ||
}, | ||
}); | ||
return; | ||
} | ||
if (!(0, node_utils_1.isCallExpression)(argument.body)) { | ||
return; | ||
} | ||
if (isScreenSyncQuery(argument)) { | ||
@@ -208,0 +258,0 @@ const caller = getCaller(argument); |
{ | ||
"name": "eslint-plugin-testing-library", | ||
"version": "7.2.2", | ||
"version": "7.3.0", | ||
"description": "ESLint plugin to follow best practices and anticipate common mistakes when writing tests with Testing Library", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
272732
1.08%4633
1.09%