refactor(linter): make jest/vitest rule mapping more clear (#6273)

- Renamed the `map_jest` method to make it more clear what it does, now called `map_jest_rule_to_vitest`
- Tried to use a `phf_set!` over a list of strings for checking if we should map rules. Probably not faster, but should be simpler a constant amount of work if we expand the list of rules in the future.
- Made it easier to not mess up the arguments to the `map_jest` function by making it accept a `RuleWithSeverity` instead of just strings.
This commit is contained in:
camchenry 2024-10-04 01:41:33 +00:00 committed by Cameron McHenry
parent 7ca70ddab6
commit ba9c372a15
2 changed files with 23 additions and 21 deletions

View file

@ -145,7 +145,7 @@ impl<'a> ContextHost<'a> {
/// Creates a new [`LintContext`] for a specific rule. /// Creates a new [`LintContext`] for a specific rule.
pub fn spawn(self: Rc<Self>, rule: &RuleWithSeverity) -> LintContext<'a> { pub fn spawn(self: Rc<Self>, rule: &RuleWithSeverity) -> LintContext<'a> {
let rule_name = rule.name(); let rule_name = rule.name();
let plugin_name = self.map_jest(rule.plugin_name(), rule_name); let plugin_name = self.map_jest_rule_to_vitest(rule);
LintContext { LintContext {
parent: self, parent: self,
@ -176,10 +176,11 @@ impl<'a> ContextHost<'a> {
/// ///
/// Many Vitest rules are essentially ports of the Jest plugin rules with minor modifications. /// Many Vitest rules are essentially ports of the Jest plugin rules with minor modifications.
/// For these rules, we use the corresponding jest rules with some adjustments for compatibility. /// For these rules, we use the corresponding jest rules with some adjustments for compatibility.
fn map_jest(&self, plugin_name: &'static str, rule_name: &str) -> &'static str { fn map_jest_rule_to_vitest(&self, rule: &RuleWithSeverity) -> &'static str {
let plugin_name = rule.plugin_name();
if self.plugins.has_vitest() if self.plugins.has_vitest()
&& plugin_name == "jest" && plugin_name == "jest"
&& utils::is_jest_rule_adapted_to_vitest(rule_name) && utils::is_jest_rule_adapted_to_vitest(rule.name())
{ {
"vitest" "vitest"
} else { } else {

View file

@ -17,11 +17,8 @@ pub use self::{
tree_shaking::*, unicorn::*, vitest::*, tree_shaking::*, unicorn::*, vitest::*,
}; };
/// Check if the Jest rule is adapted to Vitest. /// List of Jest rules that have Vitest equivalents.
/// Many Vitest rule are essentially ports of Jest plugin rules with minor modifications. const VITEST_COMPATIBLE_JEST_RULES: phf::Set<&'static str> = phf::phf_set! {
/// For these rules, we use the corresponding jest rules with some adjustments for compatibility.
pub fn is_jest_rule_adapted_to_vitest(rule_name: &str) -> bool {
let jest_rules: &[&str] = &[
"consistent-test-it", "consistent-test-it",
"expect-expect", "expect-expect",
"no-alias-methods", "no-alias-methods",
@ -36,9 +33,13 @@ pub fn is_jest_rule_adapted_to_vitest(rule_name: &str) -> bool {
"prefer-hooks-in-order", "prefer-hooks-in-order",
"valid-describe-callback", "valid-describe-callback",
"valid-expect", "valid-expect",
]; };
jest_rules.contains(&rule_name) /// Check if the Jest rule is adapted to Vitest.
/// Many Vitest rule are essentially ports of Jest plugin rules with minor modifications.
/// For these rules, we use the corresponding jest rules with some adjustments for compatibility.
pub fn is_jest_rule_adapted_to_vitest(rule_name: &str) -> bool {
VITEST_COMPATIBLE_JEST_RULES.contains(rule_name)
} }
pub fn read_to_string(path: &Path) -> io::Result<String> { pub fn read_to_string(path: &Path) -> io::Result<String> {