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

@ -8,7 +8,6 @@ use std::slice::{Iter, IterMut};
use itertools::Itertools;
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::{int, LiteralExpressionRef};
@ -842,6 +841,7 @@ pub struct ExprGeneratorExp {
pub range: TextRange,
pub elt: Box<Expr>,
pub generators: Vec<Comprehension>,
pub parenthesized: bool,
}
impl From<ExprGeneratorExp> for Expr {
@ -1796,6 +1796,9 @@ pub struct ExprTuple {
pub range: TextRange,
pub elts: Vec<Expr>,
pub ctx: ExprContext,
/// Whether the tuple is parenthesized in the source code.
pub parenthesized: bool,
}
impl From<ExprTuple> for Expr {
@ -1804,37 +1807,6 @@ impl From<ExprTuple> for Expr {
}
}
impl ExprTuple {
/// Return `true` if a tuple is parenthesized in the source code.
pub fn is_parenthesized(&self, source: &str) -> bool {
let Some(elt) = self.elts.first() else {
return true;
};
// Count the number of open parentheses between the start of the tuple and the first element.
let open_parentheses_count =
SimpleTokenizer::new(source, TextRange::new(self.start(), elt.start()))
.skip_trivia()
.filter(|token| token.kind() == SimpleTokenKind::LParen)
.count();
if open_parentheses_count == 0 {
return false;
}
// Count the number of parentheses between the end of the first element and its trailing comma.
let close_parentheses_count =
SimpleTokenizer::new(source, TextRange::new(elt.end(), self.end()))
.skip_trivia()
.take_while(|token| token.kind() != SimpleTokenKind::Comma)
.filter(|token| token.kind() == SimpleTokenKind::RParen)
.count();
// If the number of open parentheses is greater than the number of close parentheses, the tuple
// is parenthesized.
open_parentheses_count > close_parentheses_count
}
}
/// See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprSlice {
@ -3911,7 +3883,7 @@ mod tests {
assert_eq!(std::mem::size_of::<ExprDictComp>(), 48);
assert_eq!(std::mem::size_of::<ExprEllipsisLiteral>(), 8);
assert_eq!(std::mem::size_of::<ExprFString>(), 48);
assert_eq!(std::mem::size_of::<ExprGeneratorExp>(), 40);
assert_eq!(std::mem::size_of::<ExprGeneratorExp>(), 48);
assert_eq!(std::mem::size_of::<ExprIfExp>(), 32);
assert_eq!(std::mem::size_of::<ExprIpyEscapeCommand>(), 32);
assert_eq!(std::mem::size_of::<ExprLambda>(), 24);