diff --git a/scripts/formatter/forc-fmt-all.sh b/scripts/formatter/forc-fmt-all.sh new file mode 100755 index 00000000000..7e8fcdf9dcf --- /dev/null +++ b/scripts/formatter/forc-fmt-all.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# This script will format all sway projects in the current directory and all subdirectories. +# This is useful for testing the formatter itself to make sure it's not panicking on any valid +# sway projects and for checking that it's formatted output is correct. +forc_manifests=`find . -name Forc.toml` +let count=0 +let failed=0 +for f in $forc_manifests +do + dir="${f%/*}" + forc fmt -p $dir + if [ $? -ne 0 ] + then + echo "Formatting failed: $dir" + let failed=failed+1 + fi + let count=count+1 +done +echo "" +echo "Failed count: $failed" +echo "Total count: $count" diff --git a/swayfmt/src/items/item_abi.rs b/swayfmt/src/items/item_abi/mod.rs similarity index 99% rename from swayfmt/src/items/item_abi.rs rename to swayfmt/src/items/item_abi/mod.rs index b1433187d39..f678abe0cd9 100644 --- a/swayfmt/src/items/item_abi.rs +++ b/swayfmt/src/items/item_abi/mod.rs @@ -11,6 +11,9 @@ use std::fmt::Write; use sway_ast::{keywords::Token, ItemAbi}; use sway_types::{ast::Delimiter, Spanned}; +#[cfg(test)] +mod tests; + impl Format for ItemAbi { fn format( &self, diff --git a/swayfmt/src/items/item_abi/tests.rs b/swayfmt/src/items/item_abi/tests.rs new file mode 100644 index 00000000000..a0053aa01de --- /dev/null +++ b/swayfmt/src/items/item_abi/tests.rs @@ -0,0 +1,41 @@ +use forc_tracing::{println_green, println_red}; +use paste::paste; +use prettydiff::{basic::DiffOp, diff_lines}; +use test_macros::fmt_test_item; + +fmt_test_item!(abi_contains_constant +"abi A { + const ID: u32; +}", +intermediate_whitespace +"abi A { +const ID: u32; +}"); + +fmt_test_item!(abi_contains_functions +"abi A { + fn hi() -> bool; + fn hi2(hello: bool); + fn hi3(hello: bool) -> u64; +}", +intermediate_whitespace +"abi A { +fn hi() -> bool; + fn hi2(hello: bool); + fn hi3(hello: bool)-> u64; +}"); + +fmt_test_item!(abi_contains_comments +"abi A { + fn hi() -> bool; + /// Function 2 + fn hi2(hello: bool); + fn hi3(hello: bool) -> u64; // here too +}", +intermediate_whitespace +"abi A { +fn hi() -> bool; +/// Function 2 + fn hi2(hello: bool); + fn hi3(hello: bool)-> u64;// here too +}"); diff --git a/swayfmt/src/items/item_impl/tests.rs b/swayfmt/src/items/item_impl/tests.rs index 8901b0d6a16..1e950dcbc90 100644 --- a/swayfmt/src/items/item_impl/tests.rs +++ b/swayfmt/src/items/item_impl/tests.rs @@ -104,3 +104,13 @@ fn foo( ) { } }" ); + +fmt_test_item!(impl_contains_const +"impl ConstantId for Struct { + const ID: u32 = 5; +}", +intermediate_whitespace +"impl ConstantId for Struct { + const ID: u32=5; +}" +); diff --git a/swayfmt/src/items/item_trait/mod.rs b/swayfmt/src/items/item_trait/mod.rs index 20b90a3aee0..f171b23876c 100644 --- a/swayfmt/src/items/item_trait/mod.rs +++ b/swayfmt/src/items/item_trait/mod.rs @@ -83,13 +83,16 @@ impl Format for ItemTrait { sway_ast::ItemTraitItem::Const(const_decl, _) => { write!(formatted_code, "{}", formatter.indent_str()?,)?; const_decl.format(formatted_code, formatter)?; - writeln!(formatted_code, ";")?; } ItemTraitItem::Error(_, _) => {} } } } - formatted_code.pop(); // pop last ending newline + + if formatted_code.ends_with('\n') { + formatted_code.pop(); // pop last ending newline + } + Self::close_curly_brace(formatted_code, formatter)?; if let Some(trait_defs) = &self.trait_defs_opt { write!(formatted_code, " ")?; @@ -128,7 +131,7 @@ impl Format for ItemTraitItem { } ItemTraitItem::Const(const_decl, _) => { const_decl.format(formatted_code, formatter)?; - writeln!(formatted_code, ";")?; + writeln!(formatted_code)?; Ok(()) } ItemTraitItem::Error(_, _) => Ok(()), diff --git a/swayfmt/src/items/item_trait/tests.rs b/swayfmt/src/items/item_trait/tests.rs index e1daf791822..34711329a8f 100644 --- a/swayfmt/src/items/item_trait/tests.rs +++ b/swayfmt/src/items/item_trait/tests.rs @@ -67,6 +67,15 @@ intermediate_whitespace } " ); +fmt_test_item!(trait_contains_const +"trait ConstantId { + const ID: u32 = 1; +}", +intermediate_whitespace +"trait ConstantId { + const ID: u32 = 1; +}"); + fmt_test_item!( trait_normal_comment_two_fns "pub trait MyTrait { @@ -75,7 +84,6 @@ trait_normal_comment_two_fns // Before b fn b(self); }", - intermediate_whitespace " pub trait MyTrait { // Before A