Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fmt panic for const inside trait and abi #5009

Merged
merged 10 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions scripts/formatter/forc-fmt-all.sh
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions swayfmt/src/items/item_abi/tests.rs
Original file line number Diff line number Diff line change
@@ -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
}");
10 changes: 10 additions & 0 deletions swayfmt/src/items/item_impl/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}"
);
9 changes: 6 additions & 3 deletions swayfmt/src/items/item_trait/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
sdankel marked this conversation as resolved.
Show resolved Hide resolved
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, " ")?;
Expand Down Expand Up @@ -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(()),
Expand Down
10 changes: 9 additions & 1 deletion swayfmt/src/items/item_trait/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -75,7 +84,6 @@ trait_normal_comment_two_fns
// Before b
fn b(self);
}",

intermediate_whitespace
" pub trait MyTrait {
// Before A
Expand Down
Loading