Skip to content

Commit

Permalink
✨ add check if its missing a newline at EOF (#28)
Browse files Browse the repository at this point in the history
I resolved my git and did properly the #27 PR 😄
  • Loading branch information
AucaCoyan committed Jun 15, 2023
1 parent 7770e3a commit 478471d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ pub fn format_inner(contents: &[u8], _config: &Config) -> Vec<u8> {
// cleanup
start = span.end + 1;
}
// just before writing, append a new line to the file.
out = insert_newline(out);
// just before writing, check if a new line is needed.
out = add_newline_at_end_of_file(out);

// timer = timer.done_formatting()
out
Expand All @@ -156,6 +156,14 @@ fn insert_newline(mut bytes: Vec<u8>) -> Vec<u8> {
bytes
}

/// Checks if it missing a new line. If true, adds it.
fn add_newline_at_end_of_file(out: Vec<u8>) -> Vec<u8> {
match out.last() {
Some(&b'\n') => out,
_ => insert_newline(out),
}
}

/// Given a slice of bytes, strip all spaces, new lines and tabs found within
///
/// Because you don't know how the incoming code is formatted,
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ myfunc(one)
run_test(input, expected);
}

#[test]
fn add_new_line() {
let input = "null";
let expected = "null\n";
run_test(input, expected);
}

#[test]
fn remove_additional_lines() {
let input = "let one = 1\n\n\n";
let expected = "let one = 1\n";
run_test(input, expected);
}

#[test]
fn remove_leading_whitespace() {
let input = " 0";
Expand Down

0 comments on commit 478471d

Please sign in to comment.