Skip to content

Commit

Permalink
feat: quote annotations that contain quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Jun 9, 2024
1 parent 0cfb90f commit 41e9c0f
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,23 +264,17 @@ pub(crate) fn quote_annotation(
}
}

// If the annotation already contains a quote, avoid attempting to re-quote it. For example:
// ```python
// from typing import Literal
//
// Set[Literal["Foo"]]
// ```
let text = locator.slice(expr);
if text.contains('\'') || text.contains('"') {
return Err(anyhow::anyhow!("Annotation already contains a quote"));
}

// Quote the entire expression.
let quote = stylist.quote();
let annotation = generator.expr(expr);

let annotation_new = if annotation.contains(stylist.quote().as_char()) {
annotation.replace(stylist.quote().as_char(), &stylist.quote().opposite().as_char().to_string())
} else {
annotation
};

Ok(Edit::range_replacement(
format!("{quote}{annotation}{quote}"),
format!("{quote}{annotation_new}{quote}"),
expr.range(),
))
}
Expand Down

0 comments on commit 41e9c0f

Please sign in to comment.