Skip to content

Commit

Permalink
refactor(linter): impl serde and schemars traits for LintPlugins (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Sep 14, 2024
1 parent 7caae5b commit f61e8b5
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions crates/oxc_linter/src/options/plugins.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -105,6 +107,60 @@ impl From<&str> for LintPlugins {
}
}

impl From<LintPlugins> 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<S: AsRef<str>> FromIterator<S> for LintPlugins {
fn from_iter<T: IntoIterator<Item = S>>(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<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Vec::<&str>::deserialize(deserializer).map(|vec| vec.into_iter().collect())
}
}

impl Serialize for LintPlugins {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
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::<Vec<&str>>()
}
}

#[derive(Debug)]
#[non_exhaustive]
pub struct LintPluginOptions {
Expand Down

0 comments on commit f61e8b5

Please sign in to comment.