Add parenthesized flag to ExprTuple and ExprGenerator (#9614)

This commit is contained in:
Micha Reiser 2024-02-26 16:35:20 +01:00 committed by GitHub
parent ab4bd71755
commit 77c5561646
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
65 changed files with 391 additions and 139 deletions

View file

@ -14,7 +14,6 @@ use ruff_text_size::Ranged;
use crate::builders::parenthesize_if_expands;
use crate::comments::{leading_comments, trailing_comments, LeadingDanglingTrailingComments};
use crate::context::{NodeLevel, WithNodeLevel};
use crate::expression::expr_generator_exp::is_generator_parenthesized;
use crate::expression::parentheses::{
is_expression_parenthesized, optional_parentheses, parenthesized, NeedsParentheses,
OptionalParentheses, Parentheses, Parenthesize,
@ -661,15 +660,16 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
return;
}
Expr::Tuple(tuple) if tuple.is_parenthesized(self.context.source()) => {
Expr::Tuple(ast::ExprTuple {
parenthesized: true,
..
}) => {
self.any_parenthesized_expressions = true;
// The values are always parenthesized, don't visit.
return;
}
Expr::GeneratorExp(generator)
if is_generator_parenthesized(generator, self.context.source()) =>
{
Expr::GeneratorExp(generator) if generator.parenthesized => {
self.any_parenthesized_expressions = true;
// The values are always parenthesized, don't visit.
return;
@ -1035,11 +1035,7 @@ pub(crate) fn has_own_parentheses(
Some(OwnParentheses::NonEmpty)
}
Expr::GeneratorExp(generator)
if is_generator_parenthesized(generator, context.source()) =>
{
Some(OwnParentheses::NonEmpty)
}
Expr::GeneratorExp(generator) if generator.parenthesized => Some(OwnParentheses::NonEmpty),
// These expressions must contain _some_ child or trivia token in order to be non-empty.
Expr::List(ast::ExprList { elts, .. }) | Expr::Set(ast::ExprSet { elts, .. }) => {
@ -1050,7 +1046,12 @@ pub(crate) fn has_own_parentheses(
}
}
Expr::Tuple(tuple) if tuple.is_parenthesized(context.source()) => {
Expr::Tuple(
tuple @ ast::ExprTuple {
parenthesized: true,
..
},
) => {
if !tuple.elts.is_empty() || context.comments().has_dangling(AnyNodeRef::from(expr)) {
Some(OwnParentheses::NonEmpty)
} else {