mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 01:51:30 +00:00
Fix normalize
arguments when fstring_formatting
is disabled (#13910)
This commit is contained in:
parent
7272f83868
commit
113ce840a6
4 changed files with 12 additions and 12 deletions
|
@ -3,9 +3,7 @@ use ruff_python_ast::{AnyStringFlags, FString, StringFlags};
|
||||||
use ruff_source_file::Locator;
|
use ruff_source_file::Locator;
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::preview::{
|
use crate::preview::is_f_string_formatting_enabled;
|
||||||
is_f_string_formatting_enabled, is_f_string_implicit_concatenated_string_literal_quotes_enabled,
|
|
||||||
};
|
|
||||||
use crate::string::{Quoting, StringNormalizer, StringQuotes};
|
use crate::string::{Quoting, StringNormalizer, StringQuotes};
|
||||||
|
|
||||||
use super::f_string_element::FormatFStringElement;
|
use super::f_string_element::FormatFStringElement;
|
||||||
|
@ -33,8 +31,7 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
|
||||||
|
|
||||||
// If the preview style is enabled, make the decision on what quotes to use locally for each
|
// If the preview style is enabled, make the decision on what quotes to use locally for each
|
||||||
// f-string instead of globally for the entire f-string expression.
|
// f-string instead of globally for the entire f-string expression.
|
||||||
let quoting =
|
let quoting = if is_f_string_formatting_enabled(f.context()) {
|
||||||
if is_f_string_implicit_concatenated_string_literal_quotes_enabled(f.context()) {
|
|
||||||
Quoting::CanChange
|
Quoting::CanChange
|
||||||
} else {
|
} else {
|
||||||
self.quoting
|
self.quoting
|
||||||
|
|
|
@ -16,6 +16,8 @@ pub(crate) const fn is_hug_parens_with_braces_and_square_brackets_enabled(
|
||||||
|
|
||||||
/// Returns `true` if the [`f-string formatting`](https://github.com/astral-sh/ruff/issues/7594) preview style is enabled.
|
/// Returns `true` if the [`f-string formatting`](https://github.com/astral-sh/ruff/issues/7594) preview style is enabled.
|
||||||
/// WARNING: This preview style depends on `is_f_string_implicit_concatenated_string_literal_quotes_enabled`.
|
/// WARNING: This preview style depends on `is_f_string_implicit_concatenated_string_literal_quotes_enabled`.
|
||||||
|
/// TODO: Remove `Quoting` when promoting this preview style and convert `FormatStringPart` etc. regular `FormatWithRule` implementations.
|
||||||
|
/// TODO: Remove `format_f_string` from `normalize_string` when promoting this preview style.
|
||||||
pub(crate) fn is_f_string_formatting_enabled(context: &PyFormatContext) -> bool {
|
pub(crate) fn is_f_string_formatting_enabled(context: &PyFormatContext) -> bool {
|
||||||
context.is_preview()
|
context.is_preview()
|
||||||
}
|
}
|
||||||
|
|
|
@ -371,8 +371,9 @@ impl Format<PyFormatContext<'_>> for FormatLiteralContent {
|
||||||
0,
|
0,
|
||||||
self.flags,
|
self.flags,
|
||||||
self.flags.is_f_string() && !self.is_fstring,
|
self.flags.is_f_string() && !self.is_fstring,
|
||||||
true,
|
// TODO: Remove the argument from `normalize_string` when promoting the `is_f_string_formatting_enabled` preview style.
|
||||||
false,
|
self.flags.is_f_string() && !is_f_string_formatting_enabled(f.context()),
|
||||||
|
is_f_string_formatting_enabled(f.context()),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Trim the start and end of the string if it's the first or last part of a docstring.
|
// Trim the start and end of the string if it's the first or last part of a docstring.
|
||||||
|
|
|
@ -626,7 +626,7 @@ pub(crate) fn normalize_string(
|
||||||
let mut formatted_value_nesting = 0u32;
|
let mut formatted_value_nesting = 0u32;
|
||||||
|
|
||||||
while let Some((index, c)) = chars.next() {
|
while let Some((index, c)) = chars.next() {
|
||||||
if matches!(c, '{' | '}') && is_fstring {
|
if matches!(c, '{' | '}') {
|
||||||
if escape_braces {
|
if escape_braces {
|
||||||
// Escape `{` and `}` when converting a regular string literal to an f-string literal.
|
// Escape `{` and `}` when converting a regular string literal to an f-string literal.
|
||||||
output.push_str(&input[last_index..=index]);
|
output.push_str(&input[last_index..=index]);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue