Skip to content
Merged
Show file tree
Hide file tree
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
33 changes: 29 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "ef9f9329be6923ac
"salsa_unstable",
"inventory",
] }
schemars = { version = "0.8.16" }
schemars = { version = "1.0.4" }
seahash = { version = "4.1.0" }
serde = { version = "1.0.197", features = ["derive"] }
serde-wasm-bindgen = { version = "0.6.4" }
Expand Down
6 changes: 4 additions & 2 deletions crates/ruff_dev/src/generate_json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;

use anyhow::{Result, bail};
use pretty_assertions::StrComparison;
use schemars::schema_for;
use schemars::generate::SchemaSettings;

use crate::ROOT_DIR;
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND};
Expand All @@ -17,7 +17,9 @@ pub(crate) struct Args {
}

pub(crate) fn main(args: &Args) -> Result<()> {
let schema = schema_for!(Options);
let settings = SchemaSettings::draft07();
let generator = settings.into_generator();
let schema = generator.into_root_schema_for::<Options>();
let schema_string = serde_json::to_string_pretty(&schema).unwrap();
let filename = "ruff.schema.json";
let schema_path = PathBuf::from(ROOT_DIR).join(filename);
Expand Down
6 changes: 4 additions & 2 deletions crates/ruff_dev/src/generate_ty_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;

use anyhow::{Result, bail};
use pretty_assertions::StrComparison;
use schemars::schema_for;
use schemars::generate::SchemaSettings;

use crate::ROOT_DIR;
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND};
Expand All @@ -17,7 +17,9 @@ pub(crate) struct Args {
}

pub(crate) fn main(args: &Args) -> Result<()> {
let schema = schema_for!(Options);
let settings = SchemaSettings::draft07();
let generator = settings.into_generator();
let schema = generator.into_root_schema_for::<Options>();
let schema_string = serde_json::to_string_pretty(&schema).unwrap();
let filename = "ty.schema.json";
let schema_path = PathBuf::from(ROOT_DIR).join(filename);
Expand Down
114 changes: 57 additions & 57 deletions crates/ruff_linter/src/rule_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,74 +257,74 @@ pub struct PreviewOptions {
#[cfg(feature = "schemars")]
mod schema {
use itertools::Itertools;
use schemars::_serde_json::Value;
use schemars::JsonSchema;
use schemars::schema::{InstanceType, Schema, SchemaObject};
use schemars::{JsonSchema, Schema, SchemaGenerator};
use serde_json::Value;
use strum::IntoEnumIterator;

use crate::RuleSelector;
use crate::registry::RuleNamespace;
use crate::rule_selector::{Linter, RuleCodePrefix};

impl JsonSchema for RuleSelector {
fn schema_name() -> String {
"RuleSelector".to_string()
fn schema_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("RuleSelector")
}

fn json_schema(_gen: &mut schemars::r#gen::SchemaGenerator) -> Schema {
Schema::Object(SchemaObject {
instance_type: Some(InstanceType::String.into()),
enum_values: Some(
[
// Include the non-standard "ALL" selectors.
"ALL".to_string(),
// Include the legacy "C" and "T" selectors.
"C".to_string(),
"T".to_string(),
// Include some common redirect targets for those legacy selectors.
"C9".to_string(),
"T1".to_string(),
"T2".to_string(),
]
.into_iter()
.chain(
RuleCodePrefix::iter()
.map(|p| {
let prefix = p.linter().common_prefix();
let code = p.short_code();
format!("{prefix}{code}")
})
.chain(Linter::iter().filter_map(|l| {
let prefix = l.common_prefix();
(!prefix.is_empty()).then(|| prefix.to_string())
})),
)
.filter(|p| {
// Exclude any prefixes where all of the rules are removed
if let Ok(Self::Rule { prefix, .. } | Self::Prefix { prefix, .. }) =
RuleSelector::parse_no_redirect(p)
{
!prefix.rules().all(|rule| rule.is_removed())
} else {
true
}
fn json_schema(_gen: &mut SchemaGenerator) -> Schema {
let enum_values: Vec<String> = [
// Include the non-standard "ALL" selectors.
"ALL".to_string(),
// Include the legacy "C" and "T" selectors.
"C".to_string(),
"T".to_string(),
// Include some common redirect targets for those legacy selectors.
"C9".to_string(),
"T1".to_string(),
"T2".to_string(),
]
.into_iter()
.chain(
RuleCodePrefix::iter()
.map(|p| {
let prefix = p.linter().common_prefix();
let code = p.short_code();
format!("{prefix}{code}")
})
.filter(|_rule| {
// Filter out all test-only rules
#[cfg(any(feature = "test-rules", test))]
#[expect(clippy::used_underscore_binding)]
if _rule.starts_with("RUF9") || _rule == "PLW0101" {
return false;
}

true
})
.sorted()
.map(Value::String)
.collect(),
),
..SchemaObject::default()
.chain(Linter::iter().filter_map(|l| {
let prefix = l.common_prefix();
(!prefix.is_empty()).then(|| prefix.to_string())
})),
)
.filter(|p| {
// Exclude any prefixes where all of the rules are removed
if let Ok(Self::Rule { prefix, .. } | Self::Prefix { prefix, .. }) =
RuleSelector::parse_no_redirect(p)
{
!prefix.rules().all(|rule| rule.is_removed())
} else {
true
}
})
.filter(|_rule| {
// Filter out all test-only rules
#[cfg(any(feature = "test-rules", test))]
#[expect(clippy::used_underscore_binding)]
if _rule.starts_with("RUF9") || _rule == "PLW0101" {
return false;
}

true
})
.sorted()
.collect();

let mut schema = schemars::json_schema!({ "type": "string" });
schema.ensure_object().insert(
"enum".to_string(),
Value::Array(enum_values.into_iter().map(Value::String).collect()),
);

schema
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_linter/src/settings/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,12 @@ impl TryFrom<String> for RequiredVersion {

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for RequiredVersion {
fn schema_name() -> String {
"RequiredVersion".to_string()
fn schema_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("RequiredVersion")
}

fn json_schema(generator: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
generator.subschema_for::<String>()
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
<String as schemars::JsonSchema>::json_schema(generator)
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_python_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ rustc-hash = { workspace = true }
salsa = { workspace = true, optional = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
thiserror = { workspace = true }

[features]
schemars = ["dep:schemars"]
schemars = ["dep:schemars", "dep:serde_json"]
cache = ["dep:ruff_cache", "dep:ruff_macros"]
serde = [
"dep:serde",
Expand Down
34 changes: 5 additions & 29 deletions crates/ruff_python_ast/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ use crate::generated::ExprName;
#[cfg_attr(feature = "cache", derive(ruff_macros::CacheKey))]
#[cfg_attr(feature = "salsa", derive(salsa::Update))]
#[cfg_attr(feature = "get-size", derive(get_size2::GetSize))]
#[cfg_attr(
feature = "schemars",
derive(schemars::JsonSchema),
schemars(with = "String")
)]
pub struct Name(compact_str::CompactString);

impl Name {
Expand Down Expand Up @@ -201,35 +206,6 @@ impl PartialEq<Name> for &String {
}
}

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for Name {
fn is_referenceable() -> bool {
String::is_referenceable()
}

fn schema_name() -> String {
String::schema_name()
}

fn schema_id() -> std::borrow::Cow<'static, str> {
String::schema_id()
}

fn json_schema(generator: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
String::json_schema(generator)
}

fn _schemars_private_non_optional_json_schema(
generator: &mut schemars::r#gen::SchemaGenerator,
) -> schemars::schema::Schema {
String::_schemars_private_non_optional_json_schema(generator)
}

fn _schemars_private_is_option() -> bool {
String::_schemars_private_is_option()
}
}

/// A representation of a qualified name, like `typing.List`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct QualifiedName<'a>(SegmentsVec<'a>);
Expand Down
Loading
Loading