mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +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
|
@ -57,3 +57,8 @@ result_f = (
|
||||||
),
|
),
|
||||||
2
|
2
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# https://github.com/astral-sh/ruff/issues/6841
|
||||||
|
x = f'''a{""}b'''
|
||||||
|
y = f'''c{1}d"""e'''
|
||||||
|
z = f'''a{""}b''' f'''c{1}d"""e'''
|
||||||
|
|
|
@ -48,10 +48,19 @@ impl<'a> AnyString<'a> {
|
||||||
match self {
|
match self {
|
||||||
Self::Constant(_) => Quoting::CanChange,
|
Self::Constant(_) => Quoting::CanChange,
|
||||||
Self::FString(f_string) => {
|
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 {
|
if f_string.values.iter().any(|value| match value {
|
||||||
Expr::FormattedValue(ast::ExprFormattedValue { range, .. }) => {
|
Expr::FormattedValue(ast::ExprFormattedValue { range, .. }) => {
|
||||||
let string_content = locator.slice(*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,
|
_ => false,
|
||||||
}) {
|
}) {
|
||||||
|
|
|
@ -63,6 +63,11 @@ result_f = (
|
||||||
),
|
),
|
||||||
2
|
2
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# https://github.com/astral-sh/ruff/issues/6841
|
||||||
|
x = f'''a{""}b'''
|
||||||
|
y = f'''c{1}d"""e'''
|
||||||
|
z = f'''a{""}b''' f'''c{1}d"""e'''
|
||||||
```
|
```
|
||||||
|
|
||||||
## Output
|
## Output
|
||||||
|
@ -124,6 +129,11 @@ result_f = (
|
||||||
),
|
),
|
||||||
2,
|
2,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# https://github.com/astral-sh/ruff/issues/6841
|
||||||
|
x = f"""a{""}b"""
|
||||||
|
y = f'''c{1}d"""e'''
|
||||||
|
z = f"""a{""}b""" f'''c{1}d"""e'''
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue