From bef2ce76c9d9e52c295fc1566f643e196d9082dd Mon Sep 17 00:00:00 2001 From: Ary Borenszweig <asterite@gmail.com> Date: Thu, 24 Oct 2024 12:51:35 -0300 Subject: [PATCH] fix: formatter didn't format `>>=` well --- tooling/nargo_fmt/src/formatter/statement.rs | 25 +++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tooling/nargo_fmt/src/formatter/statement.rs b/tooling/nargo_fmt/src/formatter/statement.rs index 036066e346f..28107294909 100644 --- a/tooling/nargo_fmt/src/formatter/statement.rs +++ b/tooling/nargo_fmt/src/formatter/statement.rs @@ -182,12 +182,15 @@ impl<'a, 'b> ChunkFormatter<'a, 'b> { if formatter.is_at(Token::Assign) { formatter.write_token(Token::Assign); } else { - while formatter.token != Token::Assign { - formatter.write_current_token(); - formatter.bump(); - formatter.skip_comments_and_whitespace(); - } - formatter.write_token(Token::Assign); + // This is something like `x += 1`, which is parsed as an + // Assign with an InfixExpression as its right-hand side: `x = x + 1`. + // There will always be two tokens here, like `+ =` or `> >=`. + formatter.write_current_token(); + formatter.bump(); + formatter.skip_comments_and_whitespace(); + formatter.write_current_token(); + formatter.bump(); + is_op_assign = true; } formatter.write_space(); @@ -435,6 +438,16 @@ mod tests { assert_format(src, expected); } + #[test] + fn format_shift_right_assign() { + let src = " fn foo() { x >>= 2 ; } "; + let expected = "fn foo() { + x >>= 2; +} +"; + assert_format(src, expected); + } + #[test] fn format_comptime_let_statement() { let src = " fn foo() { comptime let x = 1 ; } ";