mirror of https://github.com/astral-sh/ruff
Consume all remaining placeholders at end of format spec parsing
This commit is contained in:
parent
0e3284244b
commit
5df9ba716f
|
|
@ -51,14 +51,13 @@ bad_string_format_character.py:15:1: PLE1300 Unsupported format character 'y'
|
|||
17 | "{:*^30s}".format("centered") # OK
|
||||
|
|
||||
|
||||
bad_string_format_character.py:20:1: PLE1300 Unsupported format character 'y'
|
||||
bad_string_format_character.py:19:1: PLE1300 Unsupported format character 'y'
|
||||
|
|
||||
17 | "{:*^30s}".format("centered") # OK
|
||||
18 | "{:{s}}".format("hello", s="s") # OK (nested replacement value not checked)
|
||||
19 |
|
||||
20 | "{:{s:y}}".format("hello", s="s") # [bad-format-character] (nested replacement format spec checked)
|
||||
19 | "{:{s:y}}".format("hello", s="s") # [bad-format-character] (nested replacement format spec checked)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLE1300
|
||||
21 |
|
||||
22 | ## f-strings
|
||||
20 | "{0:.{prec}g}".format(1.23, prec=15) # OK (cannot validate after nested replacement)
|
||||
|
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -334,8 +334,33 @@ fn parse_nested_placeholder<'a>(
|
|||
}
|
||||
}
|
||||
|
||||
///
|
||||
fn consume_remaining_placeholders<'a>(
|
||||
placeholders: &mut Vec<FormatPart>,
|
||||
text: &'a str,
|
||||
) -> Result<&'a str, FormatSpecError> {
|
||||
let mut chars = text.chars();
|
||||
let mut placeholder_count = placeholders.len();
|
||||
|
||||
while chars.clone().contains(&'{') {
|
||||
dbg!(&chars, placeholder_count);
|
||||
let text = parse_nested_placeholder(placeholders, chars.as_str())?;
|
||||
chars = text.chars();
|
||||
// If we did not parse a placeholder, consume a character
|
||||
if placeholder_count == placeholders.len() {
|
||||
chars.next();
|
||||
} else {
|
||||
placeholder_count = placeholders.len();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(chars.as_str())
|
||||
}
|
||||
|
||||
impl FormatSpec {
|
||||
pub fn parse(text: &str) -> Result<Self, FormatSpecError> {
|
||||
println!();
|
||||
dbg!(text);
|
||||
let mut replacements = vec![];
|
||||
let text = parse_nested_placeholder(&mut replacements, text)?;
|
||||
let (conversion, text) = FormatConversion::parse(text);
|
||||
|
|
@ -353,7 +378,7 @@ impl FormatSpec {
|
|||
let (grouping_option, text) = FormatGrouping::parse(text);
|
||||
let text = parse_nested_placeholder(&mut replacements, text)?;
|
||||
let (precision, text) = parse_precision(text)?;
|
||||
let text = parse_nested_placeholder(&mut replacements, text)?;
|
||||
let text = consume_remaining_placeholders(&mut replacements, text)?;
|
||||
|
||||
let (format_type, _text) = if text.is_empty() {
|
||||
(None, text)
|
||||
|
|
|
|||
|
|
@ -477,7 +477,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
|||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 239..243,
|
||||
range: 239..240,
|
||||
context_expr: Constant(
|
||||
ExprConstant {
|
||||
range: 239..240,
|
||||
|
|
@ -490,7 +490,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
|||
optional_vars: None,
|
||||
},
|
||||
WithItem {
|
||||
range: 239..243,
|
||||
range: 242..243,
|
||||
context_expr: Constant(
|
||||
ExprConstant {
|
||||
range: 242..243,
|
||||
|
|
|
|||
Loading…
Reference in New Issue