Skip to content

Commit 96bb54e

Browse files
committed
fix: check if node is coming from Testing Library correctly
isNodeComingFromTestingLibrary wasn't checking if the imported declaration name matches some Testing Library valid module
1 parent e033b3b commit 96bb54e

File tree

4 files changed

+333
-7
lines changed

4 files changed

+333
-7
lines changed

docs/rules/no-unnecessary-act.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO

lib/create-testing-library-rule/detect-testing-library-utils.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import {
55
} from '@typescript-eslint/experimental-utils';
66

77
import {
8+
findClosestVariableDeclaratorNode,
89
getAssertNodeInfo,
910
getDeepestIdentifierNode,
1011
getImportModuleName,
1112
getPropertyIdentifierNode,
1213
getReferenceNode,
1314
hasImportMatch,
1415
ImportModuleNode,
16+
isCallExpression,
1517
isImportDeclaration,
1618
isImportDefaultSpecifier,
1719
isImportNamespaceSpecifier,
@@ -805,14 +807,45 @@ export function detectTestingLibraryUtils<
805807
return false;
806808
}
807809

810+
const importDeclaration = (() => {
811+
if (isImportDeclaration(importNode.parent)) {
812+
return importNode.parent;
813+
}
814+
815+
const variableDeclarator = findClosestVariableDeclaratorNode(
816+
importNode
817+
);
818+
819+
if (isCallExpression(variableDeclarator?.init)) {
820+
return variableDeclarator?.init;
821+
}
822+
823+
return undefined;
824+
})();
825+
826+
if (!importDeclaration) {
827+
return false;
828+
}
829+
830+
const importDeclarationName = getImportModuleName(importDeclaration);
831+
if (!importDeclarationName) {
832+
return false;
833+
}
834+
808835
const identifierName: string | undefined = getPropertyIdentifierNode(node)
809836
?.name;
810837

811838
if (!identifierName) {
812839
return false;
813840
}
814841

815-
return hasImportMatch(importNode, identifierName);
842+
const hasImportElementMatch = hasImportMatch(importNode, identifierName);
843+
const hasImportModuleMatch =
844+
/testing-library/g.test(importDeclarationName) ||
845+
(typeof customModuleSetting === 'string' &&
846+
importDeclarationName.endsWith(customModuleSetting));
847+
848+
return hasImportElementMatch && hasImportModuleMatch;
816849
};
817850

818851
const helpers: DetectionHelpers = {

lib/node-utils/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ export function findClosestCallExpressionNode(
7777
return findClosestCallExpressionNode(node.parent, shouldRestrictInnerScope);
7878
}
7979

80+
export function findClosestVariableDeclaratorNode(
81+
node: TSESTree.Node | undefined
82+
): TSESTree.VariableDeclarator | null {
83+
if (!node) {
84+
return null;
85+
}
86+
87+
if (ASTUtils.isVariableDeclarator(node)) {
88+
return node;
89+
}
90+
91+
return findClosestVariableDeclaratorNode(node.parent);
92+
}
93+
94+
/**
95+
* TODO: remove this one in favor of {@link findClosestCallExpressionNode}
96+
*/
8097
export function findClosestCallNode(
8198
node: TSESTree.Node,
8299
name: string

0 commit comments

Comments
 (0)