From 96ee0b86a7f25115902787986185c03acd3a2afb Mon Sep 17 00:00:00 2001 From: AMIR <31338382+amiremohamadi@users.noreply.github.com> Date: Thu, 9 Jan 2025 01:47:37 +0330 Subject: [PATCH] fix: formatting for loop (#6807) ## Description Fix #6804 ## Checklist - [x] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [ ] I have requested a review from the relevant team or maintainers. Co-authored-by: IGI-111 --- swayfmt/src/utils/language/expr/mod.rs | 2 +- swayfmt/src/utils/language/expr/tests.rs | 10 ++++++ swayfmt/tests/mod.rs | 40 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/swayfmt/src/utils/language/expr/mod.rs b/swayfmt/src/utils/language/expr/mod.rs index 9fd5005a24a..95a53c01c30 100644 --- a/swayfmt/src/utils/language/expr/mod.rs +++ b/swayfmt/src/utils/language/expr/mod.rs @@ -279,7 +279,7 @@ impl Format for Expr { |formatter| -> Result<(), FormatterError> { write!(formatted_code, "{} ", ForToken::AS_STR)?; value_pattern.format(formatted_code, formatter)?; - write!(formatted_code, "{} ", InToken::AS_STR)?; + write!(formatted_code, " {} ", InToken::AS_STR)?; iterator.format(formatted_code, formatter)?; IfExpr::open_curly_brace(formatted_code, formatter)?; block.get().format(formatted_code, formatter)?; diff --git a/swayfmt/src/utils/language/expr/tests.rs b/swayfmt/src/utils/language/expr/tests.rs index ef84d8b0c61..6472940e346 100644 --- a/swayfmt/src/utils/language/expr/tests.rs +++ b/swayfmt/src/utils/language/expr/tests.rs @@ -264,3 +264,13 @@ intermediate_whitespace "{ let i = 42; }"); + +fmt_test_expr!(basic_for_loop +"for iter in [0, 1, 7, 8, 15] { + let i = 42; +}", +intermediate_whitespace +"for iter in [0, 1, 7, 8, 15]{ +let i = 42; +}" +); diff --git a/swayfmt/tests/mod.rs b/swayfmt/tests/mod.rs index c3369d150c3..75f46632b23 100644 --- a/swayfmt/tests/mod.rs +++ b/swayfmt/tests/mod.rs @@ -3449,3 +3449,43 @@ fn tuple_field_access() { "#}, ); } + +#[test] +fn contract_for_loop() { + check( + indoc! {r#" + contract; + + abi MyContract { + fn test_function() -> bool; + } + + impl MyContract for Contract { + fn test_function() -> bool { + let mut my_vec: Vec = Vec::new(); + for iter in my_vec.iter() { + + } + + true + } + } + "#}, + indoc! {r#" + contract; + + abi MyContract { + fn test_function() -> bool; + } + + impl MyContract for Contract { + fn test_function() -> bool { + let mut my_vec: Vec = Vec::new(); + for iter in my_vec.iter() { } + + true + } + } + "#}, + ); +}