From f61e8b53dd32be196db68d88965940c6826cf40f Mon Sep 17 00:00:00 2001 From: DonIsaac <22823424+DonIsaac@users.noreply.github.com> Date: Sat, 14 Sep 2024 11:16:43 +0000 Subject: [PATCH] refactor(linter): impl serde and schemars traits for `LintPlugins` (#5706) --- crates/oxc_linter/src/options/plugins.rs | 56 ++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/crates/oxc_linter/src/options/plugins.rs b/crates/oxc_linter/src/options/plugins.rs index 52e38be6b47cc..52355892fa633 100644 --- a/crates/oxc_linter/src/options/plugins.rs +++ b/crates/oxc_linter/src/options/plugins.rs @@ -1,4 +1,6 @@ use bitflags::bitflags; +use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema}; +use serde::{de::Deserializer, ser::Serializer, Deserialize, Serialize}; bitflags! { // NOTE: may be increased to a u32 if needed @@ -105,6 +107,60 @@ impl From<&str> for LintPlugins { } } +impl From for &'static str { + fn from(value: LintPlugins) -> Self { + match value { + LintPlugins::REACT => "react", + LintPlugins::UNICORN => "unicorn", + LintPlugins::TYPESCRIPT => "typescript", + LintPlugins::OXC => "oxc", + LintPlugins::IMPORT => "import", + LintPlugins::JSDOC => "jsdoc", + LintPlugins::JEST => "jest", + LintPlugins::VITEST => "vitest", + LintPlugins::JSX_A11Y => "jsx-a11y", + LintPlugins::NEXTJS => "nextjs", + LintPlugins::REACT_PERF => "react-perf", + LintPlugins::PROMISE => "promise", + LintPlugins::NODE => "node", + _ => "", + } + } +} + +impl> FromIterator for LintPlugins { + fn from_iter>(iter: T) -> Self { + iter.into_iter() + .map(|plugin| plugin.as_ref().into()) + .fold(LintPlugins::default(), LintPlugins::union) + } +} + +impl<'de> Deserialize<'de> for LintPlugins { + fn deserialize>(deserializer: D) -> Result { + Vec::<&str>::deserialize(deserializer).map(|vec| vec.into_iter().collect()) + } +} + +impl Serialize for LintPlugins { + fn serialize(&self, serializer: S) -> Result { + let vec: Vec<&str> = self.iter().map(Into::into).collect(); + vec.serialize(serializer) + } +} + +impl JsonSchema for LintPlugins { + fn schema_name() -> String { + "LintPlugins".to_string() + } + fn schema_id() -> std::borrow::Cow<'static, str> { + std::borrow::Cow::Borrowed("LintPlugins") + } + fn json_schema(gen: &mut SchemaGenerator) -> Schema { + gen.subschema_for::>() + } +} + #[derive(Debug)] #[non_exhaustive] pub struct LintPluginOptions {