mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-11 18:58:24 +03:00
Merge pull request #6823 from evanrelf/evan/1135
Format shebangs correctly
This commit is contained in:
commit
fc81e887bb
@ -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() {
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user