|
| 1 | +import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils'; |
| 2 | + |
| 3 | +export type DetectionHelpers = { |
| 4 | + getIsImportingTestingLibrary: () => boolean; |
| 5 | +}; |
| 6 | + |
| 7 | +/** |
| 8 | + * Enhances a given rule `create` with helpers to detect Testing Library utils. |
| 9 | + */ |
| 10 | +export function detectTestingLibraryUtils< |
| 11 | + TOptions extends readonly unknown[], |
| 12 | + TMessageIds extends string, |
| 13 | + TRuleListener extends TSESLint.RuleListener = TSESLint.RuleListener |
| 14 | +>( |
| 15 | + ruleCreate: ( |
| 16 | + context: Readonly<TSESLint.RuleContext<TMessageIds, TOptions>>, |
| 17 | + optionsWithDefault: Readonly<TOptions>, |
| 18 | + detectionHelpers: Readonly<DetectionHelpers> |
| 19 | + ) => TRuleListener |
| 20 | +) { |
| 21 | + return ( |
| 22 | + context: Readonly<TSESLint.RuleContext<TMessageIds, TOptions>>, |
| 23 | + optionsWithDefault: Readonly<TOptions> |
| 24 | + ): TRuleListener => { |
| 25 | + let isImportingTestingLibrary = false; |
| 26 | + |
| 27 | + // TODO: init here options based on shared ESLint config |
| 28 | + |
| 29 | + // helpers for Testing Library detection |
| 30 | + const helpers: DetectionHelpers = { |
| 31 | + getIsImportingTestingLibrary() { |
| 32 | + return isImportingTestingLibrary; |
| 33 | + }, |
| 34 | + }; |
| 35 | + |
| 36 | + // instructions for Testing Library detection |
| 37 | + const detectionInstructions: TSESLint.RuleListener = { |
| 38 | + ImportDeclaration(node: TSESTree.ImportDeclaration) { |
| 39 | + isImportingTestingLibrary = /testing-library/g.test( |
| 40 | + node.source.value as string |
| 41 | + ); |
| 42 | + }, |
| 43 | + }; |
| 44 | + |
| 45 | + // update given rule to inject Testing Library detection |
| 46 | + const ruleInstructions = ruleCreate(context, optionsWithDefault, helpers); |
| 47 | + const enhancedRuleInstructions = Object.assign({}, ruleInstructions); |
| 48 | + |
| 49 | + Object.keys(detectionInstructions).forEach((instruction) => { |
| 50 | + (enhancedRuleInstructions as TSESLint.RuleListener)[instruction] = ( |
| 51 | + node |
| 52 | + ) => { |
| 53 | + if (instruction in detectionInstructions) { |
| 54 | + detectionInstructions[instruction](node); |
| 55 | + } |
| 56 | + |
| 57 | + if (ruleInstructions[instruction]) { |
| 58 | + return ruleInstructions[instruction](node); |
| 59 | + } |
| 60 | + }; |
| 61 | + }); |
| 62 | + |
| 63 | + return enhancedRuleInstructions; |
| 64 | + }; |
| 65 | +} |
0 commit comments