From 1abb5f87a97bbea7b7774a8558386e8cf940597f Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Mon, 13 Jan 2025 16:54:42 -0300 Subject: [PATCH] fix(nargo_fmt): let doc comment could come after regular comment --- tooling/nargo_fmt/src/formatter/statement.rs | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tooling/nargo_fmt/src/formatter/statement.rs b/tooling/nargo_fmt/src/formatter/statement.rs index 1736197403c..9e3ea652ec6 100644 --- a/tooling/nargo_fmt/src/formatter/statement.rs +++ b/tooling/nargo_fmt/src/formatter/statement.rs @@ -23,10 +23,17 @@ impl<'a, 'b> ChunkFormatter<'a, 'b> { // Now write any leading comment respecting multiple newlines after them group.leading_comment(self.chunk(|formatter| { + // Doc comments for a let statement could come before a potential non-doc comment if formatter.token.kind() == TokenKind::OuterDocComment { formatter.format_outer_doc_comments(); } + formatter.skip_comments_and_whitespace_writing_multiple_lines_if_found(); + + // Or doc comments could come after a potential non-doc comment + if formatter.token.kind() == TokenKind::OuterDocComment { + formatter.format_outer_doc_comments(); + } })); ignore_next |= self.ignore_next; @@ -390,6 +397,21 @@ mod tests { assert_format(src, expected); } + #[test] + fn format_let_statement_with_unsafe_and_comment_before_it() { + let src = " fn foo() { + // Some comment + /// Safety: some doc + let x = unsafe { 1 } ; } "; + let expected = "fn foo() { + // Some comment + /// Safety: some doc + let x = unsafe { 1 }; +} +"; + assert_format(src, expected); + } + #[test] fn format_assign() { let src = " fn foo() { x = 2 ; } ";