Skip to content
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

refactor(linter): impl serde and schemars traits for LintPlugins #5706

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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