mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Preserve triple quotes and prefixes for strings (#15818)
## Summary This is a follow-up to #15726, #15778, and #15794 to preserve the triple quote and prefix flags in plain strings, bytestrings, and f-strings. I also added a `StringLiteralFlags::without_triple_quotes` method to avoid passing along triple quotes in rules like SIM905 where it might not make sense, as discussed [here](https://github.com/astral-sh/ruff/pull/15726#discussion_r1930532426). ## Test Plan Existing tests, plus many new cases in the `generator::tests::quote` test that should cover all combinations of quotes and prefixes, at least for simple string bodies. Closes #7799 when combined with #15694, #15726, #15778, and #15794. --------- Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
9a33924a65
commit
b5e5271adf
16 changed files with 318 additions and 141 deletions
|
@ -97,3 +97,12 @@ b"TesT.WwW.ExamplE.CoM".split(b".")
|
|||
"hello\nworld".splitlines()
|
||||
"hello\nworld".splitlines(keepends=True)
|
||||
"hello\nworld".splitlines(keepends=False)
|
||||
|
||||
|
||||
# another positive demonstrating quote preservation
|
||||
"""
|
||||
"itemA"
|
||||
'itemB'
|
||||
'''itemC'''
|
||||
"'itemD'"
|
||||
""".split()
|
||||
|
|
|
@ -108,7 +108,9 @@ fn check_string_or_bytes(
|
|||
|
||||
let mut diagnostic = Diagnostic::new(UnnecessaryEscapedQuote, range);
|
||||
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
|
||||
flags.format_string_contents(&unescape_string(contents, opposite_quote_char)),
|
||||
flags
|
||||
.display_contents(&unescape_string(contents, opposite_quote_char))
|
||||
.to_string(),
|
||||
range,
|
||||
)));
|
||||
Some(diagnostic)
|
||||
|
|
|
@ -3,8 +3,8 @@ use std::cmp::Ordering;
|
|||
use ruff_diagnostics::{Applicability, Diagnostic, Edit, Fix, FixAvailability, Violation};
|
||||
use ruff_macros::{derive_message_formats, ViolationMetadata};
|
||||
use ruff_python_ast::{
|
||||
Expr, ExprCall, ExprContext, ExprList, ExprUnaryOp, StringLiteral, StringLiteralFlags,
|
||||
StringLiteralValue, UnaryOp,
|
||||
str::TripleQuotes, Expr, ExprCall, ExprContext, ExprList, ExprUnaryOp, StringLiteral,
|
||||
StringLiteralFlags, StringLiteralValue, UnaryOp,
|
||||
};
|
||||
use ruff_text_size::{Ranged, TextRange};
|
||||
|
||||
|
@ -123,7 +123,17 @@ fn construct_replacement(elts: &[&str], flags: StringLiteralFlags) -> Expr {
|
|||
Expr::from(StringLiteral {
|
||||
value: Box::from(*elt),
|
||||
range: TextRange::default(),
|
||||
flags,
|
||||
// intentionally omit the triple quote flag, if set, to avoid strange
|
||||
// replacements like
|
||||
//
|
||||
// ```python
|
||||
// """
|
||||
// itemA
|
||||
// itemB
|
||||
// itemC
|
||||
// """.split() # -> ["""itemA""", """itemB""", """itemC"""]
|
||||
// ```
|
||||
flags: flags.with_triple_quotes(TripleQuotes::No),
|
||||
})
|
||||
})
|
||||
.collect(),
|
||||
|
|
|
@ -842,4 +842,29 @@ SIM905.py:72:1: SIM905 [*] Consider using a list literal instead of `str.split`
|
|||
72 |+["a,b,c"] # ["a,b,c"]
|
||||
73 73 |
|
||||
74 74 | # negatives
|
||||
75 75 |
|
||||
75 75 |
|
||||
|
||||
SIM905.py:103:1: SIM905 [*] Consider using a list literal instead of `str.split`
|
||||
|
|
||||
102 | # another positive demonstrating quote preservation
|
||||
103 | / """
|
||||
104 | | "itemA"
|
||||
105 | | 'itemB'
|
||||
106 | | '''itemC'''
|
||||
107 | | "'itemD'"
|
||||
108 | | """.split()
|
||||
| |___________^ SIM905
|
||||
|
|
||||
= help: Replace with list literal
|
||||
|
||||
ℹ Safe fix
|
||||
100 100 |
|
||||
101 101 |
|
||||
102 102 | # another positive demonstrating quote preservation
|
||||
103 |-"""
|
||||
104 |-"itemA"
|
||||
105 |-'itemB'
|
||||
106 |-'''itemC'''
|
||||
107 |-"'itemD'"
|
||||
108 |-""".split()
|
||||
103 |+['"itemA"', "'itemB'", "'''itemC'''", "\"'itemD'\""]
|
||||
|
|
|
@ -405,7 +405,9 @@ pub(crate) fn printf_string_formatting(
|
|||
// Convert the `%`-format string to a `.format` string.
|
||||
format_strings.push((
|
||||
string_literal.range(),
|
||||
flags.format_string_contents(&percent_to_format(&format_string)),
|
||||
flags
|
||||
.display_contents(&percent_to_format(&format_string))
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue