Skip to content

Commit

Permalink
Fix handling of empty code blocks (FuelLabs#3915)
Browse files Browse the repository at this point in the history
  • Loading branch information
eureka-cpu authored Jan 28, 2023
1 parent d5c9326 commit 8075e95
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 18 deletions.
27 changes: 27 additions & 0 deletions swayfmt/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,33 @@ fn foo() {
r4: bool
}
}
"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
assert_eq!(correct_sway_code, formatted_sway_code);
assert!(test_stability(formatted_sway_code, formatter));
}
#[test]
fn test_empty_blocks() {
let sway_code_to_format = r#"contract;
fn contents() {
let i = { };
match i {
}
if true { }
}
fn empty() {}
"#;
let correct_sway_code = r#"contract;
fn contents() {
let i = {};
match i {}
if true {}
}
fn empty() {}
"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
Expand Down
14 changes: 10 additions & 4 deletions swayfmt/src/utils/language/expr/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,16 @@ fn format_then_block(
formatted_code: &mut FormattedCode,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
IfExpr::open_curly_brace(formatted_code, formatter)?;
if_expr.then_block.get().format(formatted_code, formatter)?;
if if_expr.else_opt.is_none() {
IfExpr::close_curly_brace(formatted_code, formatter)?;
if !if_expr.then_block.get().statements.is_empty()
|| if_expr.then_block.get().final_expr_opt.is_some()
{
IfExpr::open_curly_brace(formatted_code, formatter)?;
if_expr.then_block.get().format(formatted_code, formatter)?;
if if_expr.else_opt.is_none() {
IfExpr::close_curly_brace(formatted_code, formatter)?;
}
} else {
write!(formatted_code, " {{}}")?;
}

Ok(())
Expand Down
38 changes: 24 additions & 14 deletions swayfmt/src/utils/language/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,15 @@ impl Format for Expr {
Self::close_parenthesis(formatted_code, formatter)?;
}
Self::Block(code_block) => {
CodeBlockContents::open_curly_brace(formatted_code, formatter)?;
code_block.get().format(formatted_code, formatter)?;
CodeBlockContents::close_curly_brace(formatted_code, formatter)?;
if !code_block.get().statements.is_empty()
|| code_block.get().final_expr_opt.is_some()
{
CodeBlockContents::open_curly_brace(formatted_code, formatter)?;
code_block.get().format(formatted_code, formatter)?;
CodeBlockContents::close_curly_brace(formatted_code, formatter)?;
} else {
write!(formatted_code, "{{}}")?;
}
}
Self::Array(array_descriptor) => {
formatter.with_shape(
Expand Down Expand Up @@ -156,18 +162,22 @@ impl Format for Expr {
write!(formatted_code, "{} ", match_token.span().as_str())?;
value.format(formatted_code, formatter)?;
write!(formatted_code, " ")?;
MatchBranch::open_curly_brace(formatted_code, formatter)?;
let branches = branches.get();
for match_branch in branches.iter() {
write!(
formatted_code,
"{}",
formatter.shape.indent.to_string(&formatter.config)?
)?;
match_branch.format(formatted_code, formatter)?;
writeln!(formatted_code)?;
if !branches.get().is_empty() {
MatchBranch::open_curly_brace(formatted_code, formatter)?;
let branches = branches.get();
for match_branch in branches.iter() {
write!(
formatted_code,
"{}",
formatter.shape.indent.to_string(&formatter.config)?
)?;
match_branch.format(formatted_code, formatter)?;
writeln!(formatted_code)?;
}
MatchBranch::close_curly_brace(formatted_code, formatter)?;
} else {
write!(formatted_code, "{{}}")?;
}
MatchBranch::close_curly_brace(formatted_code, formatter)?;
}
Self::While {
while_token,
Expand Down

0 comments on commit 8075e95

Please sign in to comment.