Rename JoinedStr to FString in the AST (#6379)

## Summary

Per the proposal in https://github.com/astral-sh/ruff/discussions/6183,
this PR renames the `JoinedStr` node to `FString`.
This commit is contained in:
Charlie Marsh 2023-08-07 13:33:17 -04:00 committed by GitHub
parent 999d88e773
commit 3f0eea6d87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
56 changed files with 166 additions and 166 deletions

View file

@ -5,18 +5,18 @@ use crate::prelude::*;
use crate::{FormatNodeRule, PyFormatter};
use ruff_formatter::FormatResult;
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::ExprJoinedStr;
use ruff_python_ast::ExprFString;
#[derive(Default)]
pub struct FormatExprJoinedStr;
pub struct FormatExprFString;
impl FormatNodeRule<ExprJoinedStr> for FormatExprJoinedStr {
fn fmt_fields(&self, item: &ExprJoinedStr, f: &mut PyFormatter) -> FormatResult<()> {
FormatString::new(&AnyString::JoinedStr(item)).fmt(f)
impl FormatNodeRule<ExprFString> for FormatExprFString {
fn fmt_fields(&self, item: &ExprFString, f: &mut PyFormatter) -> FormatResult<()> {
FormatString::new(&AnyString::FString(item)).fmt(f)
}
}
impl NeedsParentheses for ExprJoinedStr {
impl NeedsParentheses for ExprFString {
fn needs_parentheses(
&self,
_parent: AnyNodeRef,

View file

@ -27,10 +27,10 @@ pub(crate) mod expr_compare;
pub(crate) mod expr_constant;
pub(crate) mod expr_dict;
pub(crate) mod expr_dict_comp;
pub(crate) mod expr_f_string;
pub(crate) mod expr_formatted_value;
pub(crate) mod expr_generator_exp;
pub(crate) mod expr_if_exp;
pub(crate) mod expr_joined_str;
pub(crate) mod expr_lambda;
pub(crate) mod expr_line_magic;
pub(crate) mod expr_list;
@ -93,7 +93,7 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
Expr::Compare(expr) => expr.format().with_options(Some(parentheses)).fmt(f),
Expr::Call(expr) => expr.format().fmt(f),
Expr::FormattedValue(expr) => expr.format().fmt(f),
Expr::JoinedStr(expr) => expr.format().fmt(f),
Expr::FString(expr) => expr.format().fmt(f),
Expr::Constant(expr) => expr.format().fmt(f),
Expr::Attribute(expr) => expr.format().fmt(f),
Expr::Subscript(expr) => expr.format().fmt(f),
@ -231,7 +231,7 @@ impl NeedsParentheses for Expr {
Expr::Compare(expr) => expr.needs_parentheses(parent, context),
Expr::Call(expr) => expr.needs_parentheses(parent, context),
Expr::FormattedValue(expr) => expr.needs_parentheses(parent, context),
Expr::JoinedStr(expr) => expr.needs_parentheses(parent, context),
Expr::FString(expr) => expr.needs_parentheses(parent, context),
Expr::Constant(expr) => expr.needs_parentheses(parent, context),
Expr::Attribute(expr) => expr.needs_parentheses(parent, context),
Expr::Subscript(expr) => expr.needs_parentheses(parent, context),
@ -429,7 +429,7 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
| Expr::Yield(_)
| Expr::YieldFrom(_)
| Expr::FormattedValue(_)
| Expr::JoinedStr(_)
| Expr::FString(_)
| Expr::Constant(_)
| Expr::Starred(_)
| Expr::Name(_)

View file

@ -2,7 +2,7 @@ use std::borrow::Cow;
use bitflags::bitflags;
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{self as ast, ExprConstant, ExprJoinedStr, Ranged};
use ruff_python_ast::{self as ast, ExprConstant, ExprFString, Ranged};
use ruff_python_parser::lexer::{lex_starts_at, LexicalError, LexicalErrorType};
use ruff_python_parser::{Mode, Tok};
use ruff_source_file::Locator;
@ -27,15 +27,15 @@ enum Quoting {
pub(super) enum AnyString<'a> {
Constant(&'a ExprConstant),
JoinedStr(&'a ExprJoinedStr),
FString(&'a ExprFString),
}
impl<'a> AnyString<'a> {
fn quoting(&self, locator: &Locator) -> Quoting {
match self {
Self::Constant(_) => Quoting::CanChange,
Self::JoinedStr(joined_str) => {
if joined_str.values.iter().any(|value| match value {
Self::FString(f_string) => {
if f_string.values.iter().any(|value| match value {
Expr::FormattedValue(ast::ExprFormattedValue { range, .. }) => {
let string_content = locator.slice(*range);
string_content.contains(['"', '\''])
@ -55,7 +55,7 @@ impl Ranged for AnyString<'_> {
fn range(&self) -> TextRange {
match self {
Self::Constant(expr) => expr.range(),
Self::JoinedStr(expr) => expr.range(),
Self::FString(expr) => expr.range(),
}
}
}
@ -64,7 +64,7 @@ impl<'a> From<&AnyString<'a>> for AnyNodeRef<'a> {
fn from(value: &AnyString<'a>) -> Self {
match value {
AnyString::Constant(expr) => AnyNodeRef::ExprConstant(expr),
AnyString::JoinedStr(expr) => AnyNodeRef::ExprJoinedStr(expr),
AnyString::FString(expr) => AnyNodeRef::ExprFString(expr),
}
}
}

View file

@ -1606,38 +1606,38 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ExprFormattedValue {
}
}
impl FormatRule<ast::ExprJoinedStr, PyFormatContext<'_>>
for crate::expression::expr_joined_str::FormatExprJoinedStr
impl FormatRule<ast::ExprFString, PyFormatContext<'_>>
for crate::expression::expr_f_string::FormatExprFString
{
#[inline]
fn fmt(&self, node: &ast::ExprJoinedStr, f: &mut PyFormatter) -> FormatResult<()> {
FormatNodeRule::<ast::ExprJoinedStr>::fmt(self, node, f)
fn fmt(&self, node: &ast::ExprFString, f: &mut PyFormatter) -> FormatResult<()> {
FormatNodeRule::<ast::ExprFString>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::ExprJoinedStr {
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::ExprFString {
type Format<'a> = FormatRefWithRule<
'a,
ast::ExprJoinedStr,
crate::expression::expr_joined_str::FormatExprJoinedStr,
ast::ExprFString,
crate::expression::expr_f_string::FormatExprFString,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::expression::expr_joined_str::FormatExprJoinedStr::default(),
crate::expression::expr_f_string::FormatExprFString::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ExprJoinedStr {
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ExprFString {
type Format = FormatOwnedWithRule<
ast::ExprJoinedStr,
crate::expression::expr_joined_str::FormatExprJoinedStr,
ast::ExprFString,
crate::expression::expr_f_string::FormatExprFString,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::expression::expr_joined_str::FormatExprJoinedStr::default(),
crate::expression::expr_f_string::FormatExprFString::default(),
)
}
}