From 467df23e65e1b6bfc9e55c35650a5b666026453a Mon Sep 17 00:00:00 2001 From: konstin Date: Thu, 8 Jun 2023 16:29:39 +0200 Subject: [PATCH] Implement StmtReturn (#4960) * Implement StmtPass This implements StmtPass as `pass`. The snapshot diff is small because pass mainly occurs in bodies and function (#4951) and if/for bodies. * Implement StmtReturn This implements StmtReturn as `return` or `return {value}`. The snapshot diff is small because return occurs in functions (#4951) --- ...ests__black_test__power_op_spacing_py.snap | 4 ++-- .../src/statement/stmt_return.rs | 20 ++++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__power_op_spacing_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__power_op_spacing_py.snap index bc44ba7b21..c3eb10be98 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__power_op_spacing_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__power_op_spacing_py.snap @@ -178,7 +178,7 @@ return np.divide( -return np.divide( - where=view.sum_of_weights_of_weight_long**2 > view.sum_of_weights_squared, # type: ignore -) -+NOT_YET_IMPLEMENTED_StmtReturn ++return NOT_IMPLEMENTED_call() ``` ## Ruff Output @@ -234,7 +234,7 @@ q = [i for i in []] # WE SHOULD DEFINITELY NOT EAT THESE COMMENTS (https://github.com/psf/black/issues/2873) NOT_YET_IMPLEMENTED_StmtIf -NOT_YET_IMPLEMENTED_StmtReturn +return NOT_IMPLEMENTED_call() ``` ## Black Output diff --git a/crates/ruff_python_formatter/src/statement/stmt_return.rs b/crates/ruff_python_formatter/src/statement/stmt_return.rs index 02da2a73d0..db9b28b0d4 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_return.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_return.rs @@ -1,5 +1,7 @@ -use crate::{not_yet_implemented, FormatNodeRule, PyFormatter}; -use ruff_formatter::{write, Buffer, FormatResult}; +use crate::expression::parentheses::Parenthesize; +use crate::{AsFormat, FormatNodeRule, PyFormatter}; +use ruff_formatter::prelude::{space, text}; +use ruff_formatter::{write, Buffer, Format, FormatResult}; use rustpython_parser::ast::StmtReturn; #[derive(Default)] @@ -7,6 +9,18 @@ pub struct FormatStmtReturn; impl FormatNodeRule for FormatStmtReturn { fn fmt_fields(&self, item: &StmtReturn, f: &mut PyFormatter) -> FormatResult<()> { - write!(f, [not_yet_implemented(item)]) + let StmtReturn { range: _, value } = item; + if let Some(value) = value { + write!( + f, + [ + text("return"), + space(), + value.format().with_options(Parenthesize::IfBreaks) + ] + ) + } else { + text("return").fmt(f) + } } }