mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-24 12:24:36 +00:00
Formatter quoting for f-strings with triple quotes (#7826)
**Summary** Quoting of f-strings can change if they are triple quoted and only contain single quotes inside. Fixes #6841 **Test Plan** New fixtures --------- Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
This commit is contained in:
parent
a1ee6d28ce
commit
644011fb14
3 changed files with 25 additions and 1 deletions
|
@ -48,10 +48,19 @@ impl<'a> AnyString<'a> {
|
|||
match self {
|
||||
Self::Constant(_) => Quoting::CanChange,
|
||||
Self::FString(f_string) => {
|
||||
let unprefixed = locator
|
||||
.slice(f_string.range)
|
||||
.trim_start_matches(|c| c != '"' && c != '\'');
|
||||
let triple_quoted =
|
||||
unprefixed.starts_with(r#"""""#) || unprefixed.starts_with(r#"'''"#);
|
||||
if f_string.values.iter().any(|value| match value {
|
||||
Expr::FormattedValue(ast::ExprFormattedValue { range, .. }) => {
|
||||
let string_content = locator.slice(*range);
|
||||
string_content.contains(['"', '\''])
|
||||
if triple_quoted {
|
||||
string_content.contains(r#"""""#) || string_content.contains("'''")
|
||||
} else {
|
||||
string_content.contains(['"', '\''])
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
}) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue