mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 22:55:08 +00:00
Add support for reformatting byte strings (#3176)
This commit is contained in:
parent
f967f344fc
commit
1e7233a8eb
20 changed files with 90 additions and 59 deletions
|
@ -652,7 +652,7 @@ fn format_constant(
|
|||
write!(f, [text("False")])?;
|
||||
}
|
||||
}
|
||||
Constant::Str(_) => write!(f, [string_literal(expr)])?,
|
||||
Constant::Str(_) | Constant::Bytes(_) => write!(f, [string_literal(expr)])?,
|
||||
_ => write!(f, [literal(Range::from_located(expr))])?,
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
@ -37,6 +37,7 @@ impl Format<ASTFormatContext<'_>> for StringLiteralPart {
|
|||
}
|
||||
}
|
||||
|
||||
// Retain raw prefixes.
|
||||
let mut is_raw = false;
|
||||
if leading_quote.contains('r') {
|
||||
is_raw = true;
|
||||
|
@ -46,6 +47,11 @@ impl Format<ASTFormatContext<'_>> for StringLiteralPart {
|
|||
f.write_element(FormatElement::StaticText { text: "R" })?;
|
||||
}
|
||||
|
||||
// Normalize bytes literals to use b"...".
|
||||
if leading_quote.contains('b') || leading_quote.contains('B') {
|
||||
f.write_element(FormatElement::StaticText { text: "b" })?;
|
||||
}
|
||||
|
||||
if trailing_quote.len() == 1 {
|
||||
// Single-quoted string.
|
||||
if dquotes == 0 || squotes > 0 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue