Skip to content

Commit

Permalink
enforce eof newline (#2690)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk committed Aug 10, 2022
1 parent 6e5626d commit 6c96bb8
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,11 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {
},
)?;

// EOF newline
if self.last_char().map_or(true, |char| char != '\n') {
writeln!(self.buf())?;
}

Ok(())
}

Expand Down Expand Up @@ -3509,6 +3514,10 @@ mod tests {
}
}

fn assert_eof(content: &str) {
assert!(content.ends_with("\n") && !content.ends_with("\n\n"));
}

fn test_formatter(
filename: &str,
config: FormatterConfig,
Expand All @@ -3530,6 +3539,8 @@ mod tests {
}
}

assert_eof(expected_source);

let source_parsed = parse(source).unwrap();
let expected_parsed = parse(expected_source).unwrap();

Expand All @@ -3546,6 +3557,7 @@ mod tests {

let mut source_formatted = String::new();
format(&mut source_formatted, source_parsed, config.clone()).unwrap();
assert_eof(&source_formatted);

// println!("{}", source_formatted);
let source_formatted = PrettyString(source_formatted);
Expand All @@ -3559,6 +3571,8 @@ mod tests {

let mut expected_formatted = String::new();
format(&mut expected_formatted, expected_parsed, config).unwrap();
assert_eof(&expected_formatted);

let expected_formatted = PrettyString(expected_formatted);

pretty_assertions::assert_eq!(
Expand Down

0 comments on commit 6c96bb8

Please sign in to comment.