mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 13:05:06 +00:00
Add support for TryStar (#3089)
This commit is contained in:
parent
50ec6d3b0f
commit
cdc4e86158
30 changed files with 289 additions and 15 deletions
|
@ -234,6 +234,19 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a mut Stm
|
|||
visitor.visit_body(orelse);
|
||||
visitor.visit_body(finalbody);
|
||||
}
|
||||
StmtKind::TryStar {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
} => {
|
||||
visitor.visit_body(body);
|
||||
for excepthandler in handlers {
|
||||
visitor.visit_excepthandler(excepthandler);
|
||||
}
|
||||
visitor.visit_body(orelse);
|
||||
visitor.visit_body(finalbody);
|
||||
}
|
||||
StmtKind::Assert { test, msg } => {
|
||||
visitor.visit_expr(test);
|
||||
if let Some(expr) = msg {
|
||||
|
|
|
@ -249,6 +249,12 @@ pub enum StmtKind {
|
|||
orelse: Vec<Stmt>,
|
||||
finalbody: Vec<Stmt>,
|
||||
},
|
||||
TryStar {
|
||||
body: Vec<Stmt>,
|
||||
handlers: Vec<Excepthandler>,
|
||||
orelse: Vec<Stmt>,
|
||||
finalbody: Vec<Stmt>,
|
||||
},
|
||||
Assert {
|
||||
test: Box<Expr>,
|
||||
msg: Option<Box<Expr>>,
|
||||
|
@ -815,6 +821,23 @@ impl From<rustpython_parser::ast::Stmt> for Stmt {
|
|||
trivia: vec![],
|
||||
parentheses: Parenthesize::Never,
|
||||
},
|
||||
rustpython_parser::ast::StmtKind::TryStar {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
} => Stmt {
|
||||
location: stmt.location,
|
||||
end_location: stmt.end_location,
|
||||
node: StmtKind::TryStar {
|
||||
body: body.into_iter().map(Into::into).collect(),
|
||||
handlers: handlers.into_iter().map(Into::into).collect(),
|
||||
orelse: orelse.into_iter().map(Into::into).collect(),
|
||||
finalbody: finalbody.into_iter().map(Into::into).collect(),
|
||||
},
|
||||
trivia: vec![],
|
||||
parentheses: Parenthesize::Never,
|
||||
},
|
||||
rustpython_parser::ast::StmtKind::Import { names } => Stmt {
|
||||
location: stmt.location,
|
||||
end_location: stmt.end_location,
|
||||
|
|
|
@ -476,6 +476,28 @@ fn format_try(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn format_try_star(
|
||||
f: &mut Formatter<ASTFormatContext<'_>>,
|
||||
stmt: &Stmt,
|
||||
body: &[Stmt],
|
||||
handlers: &[Excepthandler],
|
||||
orelse: &[Stmt],
|
||||
finalbody: &[Stmt],
|
||||
) -> FormatResult<()> {
|
||||
write!(f, [text("try:"), block_indent(&block(body))])?;
|
||||
for handler in handlers {
|
||||
// TODO(charlie): Include `except*`.
|
||||
write!(f, [handler.format()])?;
|
||||
}
|
||||
if !orelse.is_empty() {
|
||||
write!(f, [text("else:"), block_indent(&block(orelse))])?;
|
||||
}
|
||||
if !finalbody.is_empty() {
|
||||
write!(f, [text("finally:"), block_indent(&block(finalbody))])?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn format_assert(
|
||||
f: &mut Formatter<ASTFormatContext<'_>>,
|
||||
stmt: &Stmt,
|
||||
|
@ -832,6 +854,12 @@ impl Format<ASTFormatContext<'_>> for FormatStmt<'_> {
|
|||
orelse,
|
||||
finalbody,
|
||||
} => format_try(f, self.item, body, handlers, orelse, finalbody),
|
||||
StmtKind::TryStar {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
} => format_try_star(f, self.item, body, handlers, orelse, finalbody),
|
||||
StmtKind::Assert { test, msg } => {
|
||||
format_assert(f, self.item, test, msg.as_ref().map(|expr| &**expr))
|
||||
}
|
||||
|
|
|
@ -226,6 +226,12 @@ impl<'a> Visitor<'a> for NewlineNormalizer {
|
|||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
}
|
||||
| StmtKind::TryStar {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
} => {
|
||||
self.depth = Depth::Nested;
|
||||
self.trailer = Trailer::CompoundStatement;
|
||||
|
|
|
@ -81,6 +81,7 @@ impl<'a> Visitor<'a> for ParenthesesNormalizer {
|
|||
StmtKind::Match { .. } => {}
|
||||
StmtKind::Raise { .. } => {}
|
||||
StmtKind::Try { .. } => {}
|
||||
StmtKind::TryStar { .. } => {}
|
||||
StmtKind::Assert { test, msg } => {
|
||||
use_inferred_parens(test);
|
||||
if let Some(msg) = msg {
|
||||
|
|
|
@ -411,6 +411,12 @@ fn sorted_child_nodes_inner<'a>(node: &Node<'a>, result: &mut Vec<Node<'a>>) {
|
|||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
}
|
||||
| StmtKind::TryStar {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
} => {
|
||||
for stmt in body {
|
||||
result.push(Node::Stmt(stmt));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue