-
Notifications
You must be signed in to change notification settings - Fork 147
Move rules settings to ESLint shared config: part 1 - utils detection #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
9c2cce9
c30a623
644d1c4
647ffc4
620e200
401ab97
71af089
15ae867
f35e95f
0ec9949
a229e44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just realized I lost some types I added correctly like |
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit lost, but given default exports just take the name you want, we could just export if you wanted to enforce the name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a stupid function not necessary anymore. I first implemented this by calling |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would
options
be where the eslint configuration (for instance: the custom import) is?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, these options are always the rule options. If you want to access shared ones, you'll need to get them through
context
. The idea is not having to check shared options manually within rules, as the TL detection will check which are those options to decide if a method is from Testing Library or not.