diff --git a/cli/src/format.rs b/cli/src/format.rs index d34f261ae1..9fb48b7d39 100644 --- a/cli/src/format.rs +++ b/cli/src/format.rs @@ -45,7 +45,7 @@ pub fn format(files: std::vec::Vec) { e ); })); - + let ast_normalized = ast.remove_spaces(&arena); let reparsed_ast_normalized = reparsed_ast.remove_spaces(&arena); diff --git a/compiler/fmt/tests/test_fmt.rs b/compiler/fmt/tests/test_fmt.rs index 189fcb35b0..806c0e79f6 100644 --- a/compiler/fmt/tests/test_fmt.rs +++ b/compiler/fmt/tests/test_fmt.rs @@ -14,23 +14,32 @@ mod test_fmt { use roc_parse::parser::{Parser, State}; use roc_test_utils::assert_multiline_str_eq; - fn expr_formats_to(input: &str, expected: &str) { + fn _expect_format_helper(input: &str, expected: &str) { let arena = Bump::new(); - let input = input.trim_end(); - let expected = expected.trim_end(); - match roc_parse::test_helpers::parse_expr_with(&arena, input.trim()) { Ok(actual) => { let mut buf = String::new_in(&arena); actual.format_with_options(&mut buf, Parens::NotNeeded, Newlines::Yes, 0); - assert_multiline_str_eq!(expected, buf.as_str()) + assert_multiline_str_eq!(expected, buf.as_str()); } Err(error) => panic!("Unexpected parse failure when parsing this for formatting:\n\n{}\n\nParse error was:\n\n{:?}\n\n", input, error) }; } + fn expr_formats_to(input: &str, expected: &str) { + let input = input.trim_end(); + let expected = expected.trim_end(); + + // First check that input formats to the expected version + _expect_format_helper(input, expected); + + // Parse the expected result format it, asserting that it doesn't change + // It's important that formatting be stable / idempotent + _expect_format_helper(expected, expected); + } + fn expr_formats_same(input: &str) { expr_formats_to(input, input); } @@ -88,6 +97,32 @@ mod test_fmt { )); } + #[test] + #[ignore] + fn def_with_comment_on_same_line() { + // TODO(joshuawarner32): make trailing comments format stabily + // This test currently fails because the comment ends up as SpaceBefore for the following `a` + // This works fine when formatted _once_ - but if you format again, the formatter wants to + // insert a newline between `a = "Hello"` and the comment, further muddying the waters. + // Clearly the formatter shouldn't be allowed to migrate a comment around like that. + expr_formats_to( + indoc!( + r#" + a = "Hello" # This variable is for greeting + + a + "# + ), + indoc!( + r#" + a = "Hello" + # This variable is for greeting + a + "# + ), + ); + } + #[test] fn def_with_comment_and_extra_space() { expr_formats_to( @@ -1834,7 +1869,7 @@ mod test_fmt { } #[test] - fn when_with_alternatives() { + fn when_with_alternatives_1() { expr_formats_same(indoc!( r#" when b is @@ -1847,6 +1882,10 @@ mod test_fmt { 5 "# )); + } + + #[test] + fn when_with_alternatives_2() { expr_formats_same(indoc!( r#" when b is @@ -1856,6 +1895,10 @@ mod test_fmt { 1 "# )); + } + + #[test] + fn when_with_alternatives_3() { expr_formats_to( indoc!( r#" @@ -1873,6 +1916,13 @@ mod test_fmt { "# ), ); + } + + #[test] + #[ignore] + // This test is actually invalid, since the result of the formatter doesn't parse. + // TODO(joshuawarner32): fix either the formatter or parser to allow this + fn when_with_alternatives_4() { expr_formats_to( indoc!( r#"