Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fmt): eof newline #2690

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,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 @@ -3478,6 +3483,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 @@ -3499,6 +3508,8 @@ mod tests {
}
}

assert_eof(expected_source);

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

Expand All @@ -3515,6 +3526,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 @@ -3528,6 +3540,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