Merge pull request #6823 from evanrelf/evan/1135

Format shebangs correctly
This commit is contained in:
Richard Feldman 2024-06-20 09:00:30 -04:00 committed by GitHub
commit fc81e887bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 64 additions and 0 deletions

View File

@ -149,6 +149,14 @@ pub fn fmt_comments_only<'a, 'buf, I>(
}
fn fmt_comment(buf: &mut Buf, comment: &str) {
// Format shebangs without whitespace. We look for " !" as well to fix incorrect formatting from
// the past.
if buf.is_empty() && (comment.starts_with('!') || comment.starts_with(" !")) {
buf.push('#');
buf.push_str(comment.trim());
return;
}
// The '#' in a comment should always be preceded by a newline or a space,
// unless it's the very beginning of the buffer.
if !buf.is_empty() && !buf.ends_with_space() && !buf.ends_with_newline() {

View File

@ -147,6 +147,62 @@ mod test_fmt {
));
}
#[test]
fn shebang_comment() {
// Correct shebangs are left alone
expr_formats_same(indoc!(
r#"
#!/usr/bin/env roc
x = 0
x
"#
));
// Incorrect shebang formatting from the past is fixed
expr_formats_to(
indoc!(
r#"
# !/usr/bin/env roc
x = 0
x
"#
),
indoc!(
r#"
#!/usr/bin/env roc
x = 0
x
"#
),
);
// Other whitespace from the user is left alone
expr_formats_to(
&format!(
indoc!(
r#"
# !/usr/bin/env roc{space}
x = 0
x
"#
),
space = " "
),
indoc!(
r#"
# !/usr/bin/env roc
x = 0
x
"#
),
);
}
#[test]
fn comment_with_trailing_space() {
expr_formats_to(