mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-02 01:42:25 +00:00
Pass parent to NeedsParentheses
(#5708)
This commit is contained in:
parent
30702c2977
commit
067b2a6ce6
55 changed files with 562 additions and 606 deletions
|
@ -2,6 +2,7 @@ use rustpython_parser::ast::StmtAssign;
|
|||
|
||||
use ruff_formatter::write;
|
||||
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::prelude::*;
|
||||
use crate::FormatNodeRule;
|
||||
|
@ -26,6 +27,13 @@ impl FormatNodeRule<StmtAssign> for FormatStmtAssign {
|
|||
write!(f, [target.format(), space(), text("="), space()])?;
|
||||
}
|
||||
|
||||
write!(f, [value.format().with_options(Parenthesize::IfBreaks)])
|
||||
write!(
|
||||
f,
|
||||
[maybe_parenthesize_expression(
|
||||
value,
|
||||
item,
|
||||
Parenthesize::IfBreaks
|
||||
)]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
use rustpython_parser::ast::StmtAsyncFunctionDef;
|
||||
|
||||
use ruff_python_ast::function::AnyFunctionDefinition;
|
||||
|
||||
use crate::prelude::*;
|
||||
use crate::FormatNodeRule;
|
||||
use ruff_python_ast::function::AnyFunctionDefinition;
|
||||
use rustpython_parser::ast::StmtAsyncFunctionDef;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatStmtAsyncFunctionDef;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::prelude::{space, text};
|
||||
|
@ -23,7 +24,7 @@ impl FormatNodeRule<StmtAugAssign> for FormatStmtAugAssign {
|
|||
op.format(),
|
||||
text("="),
|
||||
space(),
|
||||
value.format().with_options(Parenthesize::IfBreaks)
|
||||
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
|
||||
]
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use crate::comments::trailing_comments;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
|
||||
use crate::expression::parentheses::Parentheses;
|
||||
use crate::prelude::*;
|
||||
use crate::trivia::{SimpleTokenizer, TokenKind};
|
||||
use ruff_formatter::{format_args, write};
|
||||
|
@ -100,13 +101,13 @@ impl Format<PyFormatContext<'_>> for FormatInheritanceClause<'_> {
|
|||
.count();
|
||||
|
||||
// Ignore the first parentheses count
|
||||
let parenthesize = if left_paren_count > 1 {
|
||||
Parenthesize::Always
|
||||
let parentheses = if left_paren_count > 1 {
|
||||
Parentheses::Always
|
||||
} else {
|
||||
Parenthesize::Never
|
||||
Parentheses::Never
|
||||
};
|
||||
|
||||
joiner.entry(first, &first.format().with_options(parenthesize));
|
||||
joiner.entry(first, &first.format().with_options(parentheses));
|
||||
joiner.nodes(rest.iter());
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
use crate::builders::{parenthesize_if_expands, PyFormatterExtensions};
|
||||
use crate::comments::dangling_node_comments;
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::prelude::{block_indent, format_with, space, text};
|
||||
use ruff_formatter::{write, Buffer, Format, FormatResult};
|
||||
use rustpython_parser::ast::{Ranged, StmtDelete};
|
||||
|
@ -32,7 +33,14 @@ impl FormatNodeRule<StmtDelete> for FormatStmtDelete {
|
|||
)
|
||||
}
|
||||
[single] => {
|
||||
write!(f, [single.format().with_options(Parenthesize::IfBreaks)])
|
||||
write!(
|
||||
f,
|
||||
[maybe_parenthesize_expression(
|
||||
single,
|
||||
item,
|
||||
Parenthesize::IfBreaks
|
||||
)]
|
||||
)
|
||||
}
|
||||
targets => {
|
||||
let item = format_with(|f| {
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
use crate::expression::parentheses::{is_expression_parenthesized, Parenthesize};
|
||||
use crate::expression::string::StringLayout;
|
||||
use rustpython_parser::ast::StmtExpr;
|
||||
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::prelude::*;
|
||||
use crate::FormatNodeRule;
|
||||
use rustpython_parser::ast::StmtExpr;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatStmtExpr;
|
||||
|
@ -11,14 +12,6 @@ impl FormatNodeRule<StmtExpr> for FormatStmtExpr {
|
|||
fn fmt_fields(&self, item: &StmtExpr, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let StmtExpr { value, .. } = item;
|
||||
|
||||
if let Some(constant) = value.as_constant_expr() {
|
||||
if constant.value.is_str()
|
||||
&& !is_expression_parenthesized(value.as_ref().into(), f.context().source())
|
||||
{
|
||||
return constant.format().with_options(StringLayout::Flat).fmt(f);
|
||||
}
|
||||
}
|
||||
|
||||
value.format().with_options(Parenthesize::Optional).fmt(f)
|
||||
maybe_parenthesize_expression(value, item, Parenthesize::Optional).fmt(f)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
||||
use crate::expression::expr_tuple::TupleParentheses;
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::prelude::*;
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
|
@ -17,7 +18,7 @@ impl Format<PyFormatContext<'_>> for ExprTupleWithoutParentheses<'_> {
|
|||
.format()
|
||||
.with_options(TupleParentheses::StripInsideForLoop)
|
||||
.fmt(f),
|
||||
other => other.format().with_options(Parenthesize::IfBreaks).fmt(f),
|
||||
other => maybe_parenthesize_expression(other, self.0, Parenthesize::IfBreaks).fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +55,7 @@ impl FormatNodeRule<StmtFor> for FormatStmtFor {
|
|||
space(),
|
||||
text("in"),
|
||||
space(),
|
||||
iter.format().with_options(Parenthesize::IfBreaks),
|
||||
maybe_parenthesize_expression(iter, item, Parenthesize::IfBreaks),
|
||||
text(":"),
|
||||
trailing_comments(trailing_condition_comments),
|
||||
block_indent(&body.format())
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
use rustpython_parser::ast::{Ranged, StmtFunctionDef};
|
||||
|
||||
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule};
|
||||
use ruff_python_ast::function::AnyFunctionDefinition;
|
||||
|
||||
use crate::comments::{leading_comments, trailing_comments};
|
||||
use crate::context::NodeLevel;
|
||||
use crate::expression::parentheses::{optional_parentheses, Parenthesize};
|
||||
use crate::expression::parentheses::{optional_parentheses, Parentheses};
|
||||
use crate::prelude::*;
|
||||
use crate::trivia::{lines_after, skip_trailing_trivia};
|
||||
use crate::FormatNodeRule;
|
||||
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule};
|
||||
use ruff_python_ast::function::AnyFunctionDefinition;
|
||||
use rustpython_parser::ast::{Ranged, StmtFunctionDef};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatStmtFunctionDef;
|
||||
|
@ -98,7 +100,7 @@ impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFun
|
|||
text("->"),
|
||||
space(),
|
||||
optional_parentheses(
|
||||
&return_annotation.format().with_options(Parenthesize::Never)
|
||||
&return_annotation.format().with_options(Parentheses::Never)
|
||||
)
|
||||
]
|
||||
)?;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment};
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::prelude::*;
|
||||
use crate::FormatNodeRule;
|
||||
|
@ -48,7 +49,7 @@ impl FormatNodeRule<StmtIf> for FormatStmtIf {
|
|||
[
|
||||
text(current.keyword()),
|
||||
space(),
|
||||
test.format().with_options(Parenthesize::IfBreaks),
|
||||
maybe_parenthesize_expression(test, current_statement, Parenthesize::IfBreaks),
|
||||
text(":"),
|
||||
trailing_comments(if_trailing_comments),
|
||||
block_indent(&body.format())
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::prelude::{space, text};
|
||||
use ruff_formatter::{write, Buffer, Format, FormatResult};
|
||||
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use rustpython_parser::ast::StmtRaise;
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -21,7 +22,10 @@ impl FormatNodeRule<StmtRaise> for FormatStmtRaise {
|
|||
if let Some(value) = exc {
|
||||
write!(
|
||||
f,
|
||||
[space(), value.format().with_options(Parenthesize::Optional)]
|
||||
[
|
||||
space(),
|
||||
maybe_parenthesize_expression(value, item, Parenthesize::Optional)
|
||||
]
|
||||
)?;
|
||||
}
|
||||
|
||||
|
@ -32,7 +36,7 @@ impl FormatNodeRule<StmtRaise> for FormatStmtRaise {
|
|||
space(),
|
||||
text("from"),
|
||||
space(),
|
||||
value.format().with_options(Parenthesize::Optional)
|
||||
maybe_parenthesize_expression(value, item, Parenthesize::Optional)
|
||||
]
|
||||
)?;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::prelude::{space, text};
|
||||
use ruff_formatter::{write, Buffer, Format, FormatResult};
|
||||
use rustpython_parser::ast::StmtReturn;
|
||||
|
@ -16,7 +17,7 @@ impl FormatNodeRule<StmtReturn> for FormatStmtReturn {
|
|||
[
|
||||
text("return"),
|
||||
space(),
|
||||
value.format().with_options(Parenthesize::IfBreaks)
|
||||
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
|
||||
]
|
||||
)
|
||||
} else {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::prelude::*;
|
||||
use crate::FormatNodeRule;
|
||||
|
@ -33,7 +34,7 @@ impl FormatNodeRule<StmtWhile> for FormatStmtWhile {
|
|||
[
|
||||
text("while"),
|
||||
space(),
|
||||
test.format().with_options(Parenthesize::IfBreaks),
|
||||
maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks),
|
||||
text(":"),
|
||||
trailing_comments(trailing_condition_comments),
|
||||
block_indent(&body.format())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue