Skip to content

Commit

Permalink
The dotless_pathlib_with_suffix now handles the suffix='.'
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBits committed Dec 11, 2024
1 parent 92e6c6f commit 300a0f6
Show file tree
Hide file tree
Showing 3 changed files with 858 additions and 711 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::checkers::ast::Checker;
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use anyhow;
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::{self as ast, StringFlags};
use ruff_python_semantic::analyze::typing;
Expand Down Expand Up @@ -46,14 +47,16 @@ use ruff_text_size::Ranged;
#[derive(ViolationMetadata)]
pub(crate) struct DotlessPathlibWithSuffix;

impl AlwaysFixableViolation for DotlessPathlibWithSuffix {
impl Violation for DotlessPathlibWithSuffix {
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes;

#[derive_message_formats]
fn message(&self) -> String {
"Dotless suffix passed to `.with_suffix()`".to_string()
}

fn fix_title(&self) -> String {
"Add a leading dot".to_string()
fn fix_title(&self) -> Option<String> {
Some("Add a leading dot".to_string())
}
}

Expand All @@ -75,18 +78,30 @@ pub(crate) fn dotless_pathlib_with_suffix(checker: &mut Checker, call: &ast::Exp

let string_value = string.value.to_str();

if string_value.is_empty() || string_value.starts_with('.') {
if string_value.is_empty() {
return;
}

if string_value.starts_with('.') && string_value.len() > 1 {
return;
}

let [first_part, ..] = string.value.as_slice() else {
return;
};

let diagnostic = Diagnostic::new(DotlessPathlibWithSuffix, call.range);
let after_leading_quote = string.start() + first_part.flags.opener_len();
let fix = Fix::unsafe_edit(Edit::insertion(".".to_string(), after_leading_quote));
checker.diagnostics.push(diagnostic.with_fix(fix));
let mut diagnostic = Diagnostic::new(DotlessPathlibWithSuffix, call.range);

diagnostic.try_set_fix(|| {
if string_value == "." {
return Err(anyhow::anyhow!("Cannot fix the suffix `.`"));
}
let after_leading_quote = string.start() + first_part.flags.opener_len();
let fix = Fix::unsafe_edit(Edit::insertion(".".to_string(), after_leading_quote));
Ok(fix)
});

checker.diagnostics.push(diagnostic);
}

fn is_path_with_suffix_call(semantic: &SemanticModel, func: &ast::Expr) -> bool {
Expand Down
Loading

0 comments on commit 300a0f6

Please sign in to comment.