Skip to content

Commit

Permalink
Fix formatter bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Jan 17, 2024
1 parent b8457c5 commit 340b838
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Bug fixes

- Fixed a bug where long function headers with external implementations could
format incorrectly.
- The `@deprecated` attribute can now be used to annotate module constants.
This will cause a warning to be emitted when the constant is used.

Expand Down
6 changes: 3 additions & 3 deletions compiler-core/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,12 @@ impl<'comments> Formatter<'comments> {
let signature = match &function.return_annotation {
Some(anno) => signature.append(" -> ").append(self.type_ast(anno)),
None => signature,
};
}
.group();

let body = &function.body;
if body.len() == 1 && body.first().is_placeholder() {
return attributes.append(signature.group());
return attributes.append(signature);
}

let head = attributes.append(signature);
Expand All @@ -547,7 +548,6 @@ impl<'comments> Formatter<'comments> {

// Stick it all together
head.append(" {")
.group()
.append(line().append(body).nest(INDENT).group())
.append(line())
.append("}")
Expand Down
35 changes: 34 additions & 1 deletion compiler-core/src/format/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,21 +711,30 @@ fn statement_fn() {
}
"#
);
}

#[test]
fn statement_fn1() {
assert_format!(
r#"fn main(label_one one, label_two two, label_three three) {
Nil
}
"#
);
}

#[test]
fn statement_fn2() {
assert_format!(
r#"fn main(label_one one: One, label_two two: Two) {
Nil
}
"#
);
}

#[test]
fn statement_fn3() {
assert_format!(
r#"fn main(
label_one one: One,
Expand All @@ -744,14 +753,20 @@ fn statement_fn() {
}
"#
);
}

#[test]
fn statement_fn4() {
assert_format!(
r#"fn main(label _discarded) {
Nil
}
"#
);
}

#[test]
fn statement_fn5() {
// https://github.com/gleam-lang/gleam/issues/613
assert_format!(
r#"fn main() {
Expand All @@ -760,7 +775,10 @@ fn statement_fn() {
}
"#
);
}

#[test]
fn statement_fn6() {
//
// Module function return annotations
//
Expand All @@ -771,7 +789,10 @@ fn statement_fn() {
}
"#
);
}

#[test]
fn statement_fn7() {
assert_format!(
r#"fn main() -> Loooooooooooooooooooong(
Looooooooooooooong,
Expand All @@ -783,7 +804,10 @@ fn statement_fn() {
}
"#
);
}

#[test]
fn statement_fn8() {
assert_format!(
r#"fn main() -> Loooooooooooooooooooong(
Loooooooooooooooooooooooooooooooooooooooooong,
Expand All @@ -792,24 +816,33 @@ fn statement_fn() {
}
"#
);
}

#[test]
fn statement_fn9() {
assert_format!(
r#"fn main() -> program.Exit {
Nil
}
"#
);
}

#[test]
fn statement_fn10() {
assert_format!(
"fn order(
first: Set(member),
second: Set(member),
) -> #(Set(member), Set(member)) {
) -> #(Set(member), Set(member), a) {
Nil
}
"
);
}

#[test]
fn statement_fn11() {
assert_format!(
"///
pub fn try_map(
Expand Down
15 changes: 15 additions & 0 deletions compiler-core/src/format/tests/external_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,18 @@ fn improper_list_append(
"#
);
}

// Bug found by Hayleigh
#[test]
fn long_long_external() {
assert_format!(
r#"@external(javascript, "./client-component.ffi.mjs", "register")
pub fn register(
_app: App(WebComponent, Nil, model, msg),
_name: String,
) -> Result(Nil, Error) {
Error(NotABrowser)
}
"#
);
}
2 changes: 1 addition & 1 deletion compiler-core/src/format/tests/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fn only_last_argument_can_be_broken() {
fn function_that_is_a_little_over_the_limit() {
assert_format!(
r#"pub fn handle_request(
handler: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
handler: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
) -> Nil {
todo
}
Expand Down

0 comments on commit 340b838

Please sign in to comment.