Implement template strings (#17851)

This PR implements template strings (t-strings) in the parser and
formatter for Ruff.

Minimal changes necessary to compile were made in other parts of the code (e.g. ty, the linter, etc.). These will be covered properly in follow-up PRs.
This commit is contained in:
Dylan
2025-05-30 15:00:56 -05:00
committed by GitHub
parent ad024f9a09
commit 9bbf4987e8
261 changed files with 18023 additions and 1802 deletions

View File

@@ -1,8 +1,8 @@
use crate::{
Alias, Arguments, BoolOp, BytesLiteral, CmpOp, Comprehension, Decorator, ElifElseClause,
ExceptHandler, Expr, FString, FStringElement, Keyword, MatchCase, Mod, Operator, Parameter,
ParameterWithDefault, Parameters, Pattern, PatternArguments, PatternKeyword, Singleton, Stmt,
StringLiteral, TypeParam, TypeParams, UnaryOp, WithItem,
ExceptHandler, Expr, FString, InterpolatedStringElement, Keyword, MatchCase, Mod, Operator,
Parameter, ParameterWithDefault, Parameters, Pattern, PatternArguments, PatternKeyword,
Singleton, Stmt, StringLiteral, TString, TypeParam, TypeParams, UnaryOp, WithItem,
};
use crate::{AnyNodeRef, Identifier};
@@ -157,8 +157,16 @@ pub trait SourceOrderVisitor<'a> {
}
#[inline]
fn visit_f_string_element(&mut self, f_string_element: &'a FStringElement) {
walk_f_string_element(self, f_string_element);
fn visit_interpolated_string_element(
&mut self,
interpolated_string_element: &'a InterpolatedStringElement,
) {
walk_interpolated_string_element(self, interpolated_string_element);
}
#[inline]
fn visit_t_string(&mut self, t_string: &'a TString) {
walk_t_string(self, t_string);
}
#[inline]
@@ -272,6 +280,7 @@ where
Expr::Compare(expr) => expr.visit_source_order(visitor),
Expr::Call(expr) => expr.visit_source_order(visitor),
Expr::FString(expr) => expr.visit_source_order(visitor),
Expr::TString(expr) => expr.visit_source_order(visitor),
Expr::StringLiteral(expr) => expr.visit_source_order(visitor),
Expr::BytesLiteral(expr) => expr.visit_source_order(visitor),
Expr::NumberLiteral(expr) => expr.visit_source_order(visitor),
@@ -497,15 +506,17 @@ where
visitor.leave_node(node);
}
pub fn walk_f_string_element<'a, V: SourceOrderVisitor<'a> + ?Sized>(
pub fn walk_interpolated_string_element<'a, V: SourceOrderVisitor<'a> + ?Sized>(
visitor: &mut V,
f_string_element: &'a FStringElement,
f_string_element: &'a InterpolatedStringElement,
) {
let node = AnyNodeRef::from(f_string_element);
if visitor.enter_node(node).is_traverse() {
match f_string_element {
FStringElement::Expression(element) => element.visit_source_order(visitor),
FStringElement::Literal(element) => element.visit_source_order(visitor),
InterpolatedStringElement::Interpolation(element) => {
element.visit_source_order(visitor);
}
InterpolatedStringElement::Literal(element) => element.visit_source_order(visitor),
}
}
visitor.leave_node(node);
@@ -550,6 +561,18 @@ where
visitor.leave_node(node);
}
#[inline]
pub fn walk_t_string<'a, V>(visitor: &mut V, t_string: &'a TString)
where
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(t_string);
if visitor.enter_node(node).is_traverse() {
t_string.visit_source_order(visitor);
}
visitor.leave_node(node);
}
#[inline]
pub fn walk_string_literal<'a, V>(visitor: &mut V, string_literal: &'a StringLiteral)
where

View File

@@ -1,8 +1,8 @@
use crate::{
self as ast, Alias, Arguments, BoolOp, BytesLiteral, CmpOp, Comprehension, Decorator,
ElifElseClause, ExceptHandler, Expr, ExprContext, FString, FStringElement, Keyword, MatchCase,
Operator, Parameter, Parameters, Pattern, PatternArguments, PatternKeyword, Stmt,
StringLiteral, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple,
ElifElseClause, ExceptHandler, Expr, ExprContext, FString, InterpolatedStringElement, Keyword,
MatchCase, Operator, Parameter, Parameters, Pattern, PatternArguments, PatternKeyword, Stmt,
StringLiteral, TString, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple,
TypeParams, UnaryOp, WithItem,
};
@@ -86,8 +86,14 @@ pub trait Transformer {
fn visit_f_string(&self, f_string: &mut FString) {
walk_f_string(self, f_string);
}
fn visit_f_string_element(&self, f_string_element: &mut FStringElement) {
walk_f_string_element(self, f_string_element);
fn visit_interpolated_string_element(
&self,
interpolated_string_element: &mut InterpolatedStringElement,
) {
walk_interpolated_string_element(self, interpolated_string_element);
}
fn visit_t_string(&self, t_string: &mut TString) {
walk_t_string(self, t_string);
}
fn visit_string_literal(&self, string_literal: &mut StringLiteral) {
walk_string_literal(self, string_literal);
@@ -470,6 +476,21 @@ pub fn walk_expr<V: Transformer + ?Sized>(visitor: &V, expr: &mut Expr) {
}
}
}
Expr::TString(ast::ExprTString { value, .. }) => {
for t_string_part in value.iter_mut() {
match t_string_part {
ast::TStringPart::Literal(string_literal) => {
visitor.visit_string_literal(string_literal);
}
ast::TStringPart::FString(f_string) => {
visitor.visit_f_string(f_string);
}
ast::TStringPart::TString(t_string) => {
visitor.visit_t_string(t_string);
}
}
}
}
Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) => {
for string_literal in value.iter_mut() {
visitor.visit_string_literal(string_literal);
@@ -744,29 +765,35 @@ pub fn walk_pattern_keyword<V: Transformer + ?Sized>(
pub fn walk_f_string<V: Transformer + ?Sized>(visitor: &V, f_string: &mut FString) {
for element in &mut f_string.elements {
visitor.visit_f_string_element(element);
visitor.visit_interpolated_string_element(element);
}
}
pub fn walk_f_string_element<V: Transformer + ?Sized>(
pub fn walk_interpolated_string_element<V: Transformer + ?Sized>(
visitor: &V,
f_string_element: &mut FStringElement,
interpolated_string_element: &mut InterpolatedStringElement,
) {
if let ast::FStringElement::Expression(ast::FStringExpressionElement {
if let ast::InterpolatedStringElement::Interpolation(ast::InterpolatedElement {
expression,
format_spec,
..
}) = f_string_element
}) = interpolated_string_element
{
visitor.visit_expr(expression);
if let Some(format_spec) = format_spec {
for spec_element in &mut format_spec.elements {
visitor.visit_f_string_element(spec_element);
visitor.visit_interpolated_string_element(spec_element);
}
}
}
}
pub fn walk_t_string<V: Transformer + ?Sized>(visitor: &V, t_string: &mut TString) {
for element in &mut t_string.elements {
visitor.visit_interpolated_string_element(element);
}
}
pub fn walk_expr_context<V: Transformer + ?Sized>(_visitor: &V, _expr_context: &mut ExprContext) {}
pub fn walk_bool_op<V: Transformer + ?Sized>(_visitor: &V, _bool_op: &mut BoolOp) {}