-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathtesting-library-detection.ts
52 lines (42 loc) · 1.45 KB
/
testing-library-detection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { TSESTree } from '@typescript-eslint/experimental-utils';
/**
* Enhances a given rule with helpers to detect Testing Library utils.
*/
function testingLibraryDetection(rule: any) {
return (context: any, options: any) => {
let isImportingTestingLibrary = false;
// TODO: init here options based on shared ESLint config
// helpers for Testing Library detection
const helpers = {
getIsImportingTestingLibrary() {
return isImportingTestingLibrary;
},
};
// instructions for Testing Library detection
const detectionInstructions = {
ImportDeclaration(node: TSESTree.ImportDeclaration) {
isImportingTestingLibrary = /testing-library/g.test(
node.source.value as string
);
},
};
// update given rule to inject Testing Library detection
const ruleInstructions = rule(context, options, helpers);
const enhancedRuleInstructions = Object.assign({}, ruleInstructions);
Object.keys(detectionInstructions).forEach((instruction) => {
enhancedRuleInstructions[instruction] = (node: any) => {
if (instruction in detectionInstructions) {
detectionInstructions[instruction](node);
}
if (ruleInstructions[instruction]) {
return ruleInstructions[instruction](node);
}
};
});
return enhancedRuleInstructions;
};
}
function detect(rule: any) {
return testingLibraryDetection(rule);
}
export default detect;