diff --git a/crates/oxc_linter/src/options/plugins.rs b/crates/oxc_linter/src/options/plugins.rs index d5b78e8103556..3c4cab9ab855b 100644 --- a/crates/oxc_linter/src/options/plugins.rs +++ b/crates/oxc_linter/src/options/plugins.rs @@ -1,4 +1,4 @@ -#[derive(Debug, Clone)] +#[derive(Debug)] #[non_exhaustive] pub struct LintPluginOptions { /// On by default. @@ -77,3 +77,90 @@ impl LintPluginOptions { } } } + +impl> FromIterator<(S, bool)> for LintPluginOptions { + fn from_iter>(iter: I) -> Self { + let mut options = Self::default(); + for (s, enabled) in iter { + match s.as_ref() { + "react" | "react-hooks" => options.react = enabled, + "unicorn" => options.unicorn = enabled, + "typescript" | "typescript-eslint" | "@typescript-eslint" => { + options.typescript = enabled; + } + // deepscan for backwards compatibility. Those rules have been + // moved into oxc + "oxc" | "deepscan" => options.oxc = enabled, + "import" => options.import = enabled, + "jsdoc" => options.jsdoc = enabled, + "jest" => options.jest = enabled, + "vitest" => options.vitest = enabled, + "jsx-a11y" => options.jsx_a11y = enabled, + "nextjs" => options.nextjs = enabled, + "react-perf" => options.react_perf = enabled, + "promise" => options.promise = enabled, + _ => { /* ignored */ } + } + } + options + } +} + +impl<'s> FromIterator<&'s str> for LintPluginOptions { + fn from_iter>(iter: T) -> Self { + iter.into_iter().map(|s| (s, true)).collect() + } +} + +#[cfg(test)] +mod test { + use super::*; + impl PartialEq for LintPluginOptions { + fn eq(&self, other: &Self) -> bool { + self.react == other.react + && self.unicorn == other.unicorn + && self.typescript == other.typescript + && self.oxc == other.oxc + && self.import == other.import + && self.jsdoc == other.jsdoc + && self.jest == other.jest + && self.vitest == other.vitest + && self.jsx_a11y == other.jsx_a11y + && self.nextjs == other.nextjs + && self.react_perf == other.react_perf + && self.promise == other.promise + } + } + + #[test] + fn test_collect_empty() { + let empty: &[&str] = &[]; + let plugins: LintPluginOptions = empty.iter().copied().collect(); + assert_eq!(plugins, LintPluginOptions::default()); + + let empty: Vec<(String, bool)> = vec![]; + let plugins: LintPluginOptions = empty.into_iter().collect(); + assert_eq!(plugins, LintPluginOptions::default()); + } + + #[test] + fn test_collect_strings() { + let enabled = vec!["react", "typescript", "jest"]; + let plugins: LintPluginOptions = enabled.into_iter().collect(); + let expected = LintPluginOptions { + react: true, + unicorn: true, + typescript: true, + oxc: true, + import: false, + jsdoc: false, + jest: true, + vitest: false, + jsx_a11y: false, + nextjs: false, + react_perf: false, + promise: false, + }; + assert_eq!(plugins, expected); + } +}