@@ -77,9 +77,14 @@ export function detectTestingLibraryUtils<
77
77
} ;
78
78
79
79
// Instructions for Testing Library detection.
80
- // `ImportDeclaration` must be first in order to know if Testing Library
81
- // is imported ASAP.
82
80
const detectionInstructions : TSESLint . RuleListener = {
81
+ /**
82
+ * This ImportDeclaration rule listener will check if Testing Library related
83
+ * modules are loaded. Since imports happen first thing in a file, it's
84
+ * safe to use `isImportingTestingLibraryModule` and `isImportingCustomModule`
85
+ * since they will have corresponding value already updated when reporting other
86
+ * parts of the file.
87
+ */
83
88
ImportDeclaration ( node : TSESTree . ImportDeclaration ) {
84
89
if ( ! isImportingTestingLibraryModule ) {
85
90
// check only if testing library import not found yet so we avoid
@@ -102,14 +107,12 @@ export function detectTestingLibraryUtils<
102
107
const ruleInstructions = ruleCreate ( context , optionsWithDefault , helpers ) ;
103
108
const enhancedRuleInstructions : TSESLint . RuleListener = { } ;
104
109
105
- // The order here is important too: detection instructions must come before
106
- // than rule instructions to:
107
- // - detect Testing Library things before the rule is applied
108
- // - be able to prevent the rule about to be applied if necessary
109
110
const allKeys = new Set (
110
111
Object . keys ( detectionInstructions ) . concat ( Object . keys ( ruleInstructions ) )
111
112
) ;
112
113
114
+ // Iterate over ALL instructions keys so we can override original rule instructions
115
+ // to prevent their execution if conditions to report errors are not met.
113
116
allKeys . forEach ( ( instruction ) => {
114
117
enhancedRuleInstructions [ instruction ] = ( node ) => {
115
118
if ( instruction in detectionInstructions ) {
0 commit comments