mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-21 20:15:11 +00:00
Remove Stmt::TryStar
(#6566)
## Summary Instead, we set an `is_star` flag on `Stmt::Try`. This is similar to the pattern we've migrated towards for `Stmt::For` (removing `Stmt::AsyncFor`) and friends. While these are significant differences for an interpreter, we tend to handle these cases identically or nearly identically. ## Test Plan `cargo test`
This commit is contained in:
parent
09c8b17661
commit
96d310fbab
33 changed files with 70 additions and 489 deletions
|
@ -196,12 +196,6 @@ fn is_first_statement_in_body(statement: AnyNodeRef, has_body: AnyNodeRef) -> bo
|
|||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
})
|
||||
| AnyNodeRef::StmtTryStar(ast::StmtTryStar {
|
||||
body,
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
}) => {
|
||||
are_same_optional(statement, body.first())
|
||||
|| are_same_optional(statement, orelse.first())
|
||||
|
@ -1418,13 +1412,6 @@ fn last_child_in_body(node: AnyNodeRef) -> Option<AnyNodeRef> {
|
|||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
})
|
||||
| AnyNodeRef::StmtTryStar(ast::StmtTryStar {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
}) => {
|
||||
if finalbody.is_empty() {
|
||||
if orelse.is_empty() {
|
||||
|
@ -1461,12 +1448,6 @@ fn is_first_statement_in_alternate_body(statement: AnyNodeRef, has_body: AnyNode
|
|||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
})
|
||||
| AnyNodeRef::StmtTryStar(ast::StmtTryStar {
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
}) => {
|
||||
are_same_optional(statement, handlers.first())
|
||||
|| are_same_optional(statement, orelse.first())
|
||||
|
|
|
@ -582,42 +582,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtTry {
|
|||
}
|
||||
}
|
||||
|
||||
impl FormatRule<ast::StmtTryStar, PyFormatContext<'_>>
|
||||
for crate::statement::stmt_try_star::FormatStmtTryStar
|
||||
{
|
||||
#[inline]
|
||||
fn fmt(&self, node: &ast::StmtTryStar, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
FormatNodeRule::<ast::StmtTryStar>::fmt(self, node, f)
|
||||
}
|
||||
}
|
||||
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::StmtTryStar {
|
||||
type Format<'a> = FormatRefWithRule<
|
||||
'a,
|
||||
ast::StmtTryStar,
|
||||
crate::statement::stmt_try_star::FormatStmtTryStar,
|
||||
PyFormatContext<'ast>,
|
||||
>;
|
||||
fn format(&self) -> Self::Format<'_> {
|
||||
FormatRefWithRule::new(
|
||||
self,
|
||||
crate::statement::stmt_try_star::FormatStmtTryStar::default(),
|
||||
)
|
||||
}
|
||||
}
|
||||
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtTryStar {
|
||||
type Format = FormatOwnedWithRule<
|
||||
ast::StmtTryStar,
|
||||
crate::statement::stmt_try_star::FormatStmtTryStar,
|
||||
PyFormatContext<'ast>,
|
||||
>;
|
||||
fn into_format(self) -> Self::Format {
|
||||
FormatOwnedWithRule::new(
|
||||
self,
|
||||
crate::statement::stmt_try_star::FormatStmtTryStar::default(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl FormatRule<ast::StmtAssert, PyFormatContext<'_>>
|
||||
for crate::statement::stmt_assert::FormatStmtAssert
|
||||
{
|
||||
|
|
|
@ -24,7 +24,6 @@ pub(crate) mod stmt_pass;
|
|||
pub(crate) mod stmt_raise;
|
||||
pub(crate) mod stmt_return;
|
||||
pub(crate) mod stmt_try;
|
||||
pub(crate) mod stmt_try_star;
|
||||
pub(crate) mod stmt_type_alias;
|
||||
pub(crate) mod stmt_while;
|
||||
pub(crate) mod stmt_with;
|
||||
|
@ -50,7 +49,6 @@ impl FormatRule<Stmt, PyFormatContext<'_>> for FormatStmt {
|
|||
Stmt::Match(x) => x.format().fmt(f),
|
||||
Stmt::Raise(x) => x.format().fmt(f),
|
||||
Stmt::Try(x) => x.format().fmt(f),
|
||||
Stmt::TryStar(x) => x.format().fmt(f),
|
||||
Stmt::Assert(x) => x.format().fmt(f),
|
||||
Stmt::Import(x) => x.format().fmt(f),
|
||||
Stmt::ImportFrom(x) => x.format().fmt(f),
|
||||
|
|
|
@ -1,86 +1,13 @@
|
|||
use crate::comments;
|
||||
use crate::comments::SourceComment;
|
||||
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
||||
use crate::other::except_handler_except_handler::ExceptHandlerKind;
|
||||
use crate::prelude::*;
|
||||
use crate::statement::FormatRefWithRule;
|
||||
use crate::statement::Stmt;
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::FormatRuleWithOptions;
|
||||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
use ruff_python_ast::node::AnyNodeRef;
|
||||
use ruff_python_ast::{ExceptHandler, Ranged, StmtTry, StmtTryStar, Suite};
|
||||
use ruff_text_size::TextRange;
|
||||
use ruff_python_ast::{ExceptHandler, Ranged, StmtTry, Suite};
|
||||
|
||||
pub(super) enum AnyStatementTry<'a> {
|
||||
Try(&'a StmtTry),
|
||||
TryStar(&'a StmtTryStar),
|
||||
}
|
||||
impl<'a> AnyStatementTry<'a> {
|
||||
const fn except_handler_kind(&self) -> ExceptHandlerKind {
|
||||
match self {
|
||||
AnyStatementTry::Try(_) => ExceptHandlerKind::Regular,
|
||||
AnyStatementTry::TryStar(_) => ExceptHandlerKind::Starred,
|
||||
}
|
||||
}
|
||||
|
||||
fn body(&self) -> &Suite {
|
||||
match self {
|
||||
AnyStatementTry::Try(try_) => &try_.body,
|
||||
AnyStatementTry::TryStar(try_) => &try_.body,
|
||||
}
|
||||
}
|
||||
|
||||
fn handlers(&self) -> &[ExceptHandler] {
|
||||
match self {
|
||||
AnyStatementTry::Try(try_) => try_.handlers.as_slice(),
|
||||
AnyStatementTry::TryStar(try_) => try_.handlers.as_slice(),
|
||||
}
|
||||
}
|
||||
fn orelse(&self) -> &Suite {
|
||||
match self {
|
||||
AnyStatementTry::Try(try_) => &try_.orelse,
|
||||
AnyStatementTry::TryStar(try_) => &try_.orelse,
|
||||
}
|
||||
}
|
||||
|
||||
fn finalbody(&self) -> &Suite {
|
||||
match self {
|
||||
AnyStatementTry::Try(try_) => &try_.finalbody,
|
||||
AnyStatementTry::TryStar(try_) => &try_.finalbody,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ranged for AnyStatementTry<'_> {
|
||||
fn range(&self) -> TextRange {
|
||||
match self {
|
||||
AnyStatementTry::Try(with) => with.range(),
|
||||
AnyStatementTry::TryStar(with) => with.range(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a StmtTry> for AnyStatementTry<'a> {
|
||||
fn from(value: &'a StmtTry) -> Self {
|
||||
AnyStatementTry::Try(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a StmtTryStar> for AnyStatementTry<'a> {
|
||||
fn from(value: &'a StmtTryStar) -> Self {
|
||||
AnyStatementTry::TryStar(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&AnyStatementTry<'a>> for AnyNodeRef<'a> {
|
||||
fn from(value: &AnyStatementTry<'a>) -> Self {
|
||||
match value {
|
||||
AnyStatementTry::Try(with) => AnyNodeRef::StmtTry(with),
|
||||
AnyStatementTry::TryStar(with) => AnyNodeRef::StmtTryStar(with),
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::comments;
|
||||
use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment};
|
||||
use crate::other::except_handler_except_handler::ExceptHandlerKind;
|
||||
use crate::prelude::*;
|
||||
use crate::statement::{FormatRefWithRule, Stmt};
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatStmtTry;
|
||||
|
@ -102,9 +29,10 @@ impl FormatRuleWithOptions<ExceptHandler, PyFormatContext<'_>> for FormatExceptH
|
|||
impl FormatRule<ExceptHandler, PyFormatContext<'_>> for FormatExceptHandler {
|
||||
fn fmt(&self, item: &ExceptHandler, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
match item {
|
||||
ExceptHandler::ExceptHandler(x) => {
|
||||
x.format().with_options(self.except_handler_kind).fmt(f)
|
||||
}
|
||||
ExceptHandler::ExceptHandler(except_handler) => except_handler
|
||||
.format()
|
||||
.with_options(self.except_handler_kind)
|
||||
.fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -121,14 +49,20 @@ impl<'ast> AsFormat<PyFormatContext<'ast>> for ExceptHandler {
|
|||
FormatRefWithRule::new(self, FormatExceptHandler::default())
|
||||
}
|
||||
}
|
||||
impl Format<PyFormatContext<'_>> for AnyStatementTry<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
|
||||
impl FormatNodeRule<StmtTry> for FormatStmtTry {
|
||||
fn fmt_fields(&self, item: &StmtTry, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
is_star,
|
||||
range: _,
|
||||
} = item;
|
||||
|
||||
let comments_info = f.context().comments().clone();
|
||||
let mut dangling_comments = comments_info.dangling_comments(self);
|
||||
let body = self.body();
|
||||
let handlers = self.handlers();
|
||||
let orelse = self.orelse();
|
||||
let finalbody = self.finalbody();
|
||||
let mut dangling_comments = comments_info.dangling_comments(item);
|
||||
|
||||
(_, dangling_comments) = format_case("try", body, None, dangling_comments, f)?;
|
||||
let mut previous_node = body.last();
|
||||
|
@ -139,7 +73,11 @@ impl Format<PyFormatContext<'_>> for AnyStatementTry<'_> {
|
|||
f,
|
||||
[
|
||||
leading_alternate_branch_comments(handler_comments, previous_node),
|
||||
&handler.format().with_options(self.except_handler_kind()),
|
||||
&handler.format().with_options(if *is_star {
|
||||
ExceptHandlerKind::Starred
|
||||
} else {
|
||||
ExceptHandlerKind::Regular
|
||||
}),
|
||||
]
|
||||
)?;
|
||||
previous_node = match handler {
|
||||
|
@ -154,12 +92,6 @@ impl Format<PyFormatContext<'_>> for AnyStatementTry<'_> {
|
|||
|
||||
write!(f, [comments::dangling_comments(dangling_comments)])
|
||||
}
|
||||
}
|
||||
|
||||
impl FormatNodeRule<StmtTry> for FormatStmtTry {
|
||||
fn fmt_fields(&self, item: &StmtTry, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
AnyStatementTry::from(item).fmt(f)
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtTry, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
// dangling comments are formatted as part of AnyStatementTry::fmt
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
use crate::statement::stmt_try::AnyStatementTry;
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::Format;
|
||||
use ruff_formatter::FormatResult;
|
||||
use ruff_python_ast::StmtTryStar;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatStmtTryStar;
|
||||
|
||||
impl FormatNodeRule<StmtTryStar> for FormatStmtTryStar {
|
||||
fn fmt_fields(&self, item: &StmtTryStar, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
AnyStatementTry::from(item).fmt(f)
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtTryStar, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
// dangling comments are formatted as part of AnyStatementTry::fmt
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue