mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-15 08:00:19 +00:00
Fix handling of newlines in empty files (#7473)
This commit is contained in:
parent
b66bfa6570
commit
0346e781d4
12 changed files with 90 additions and 15 deletions
|
@ -1,5 +1,6 @@
|
|||
use ruff_formatter::write;
|
||||
use ruff_python_ast::ModModule;
|
||||
use ruff_python_trivia::lines_after;
|
||||
|
||||
use crate::comments::SourceComment;
|
||||
use crate::prelude::*;
|
||||
|
@ -11,16 +12,27 @@ pub struct FormatModModule;
|
|||
|
||||
impl FormatNodeRule<ModModule> for FormatModModule {
|
||||
fn fmt_fields(&self, item: &ModModule, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let ModModule { range: _, body } = item;
|
||||
let ModModule { range, body } = item;
|
||||
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
body.format().with_options(SuiteKind::TopLevel),
|
||||
// Trailing newline at the end of the file
|
||||
hard_line_break()
|
||||
]
|
||||
)
|
||||
if body.is_empty() {
|
||||
// Only preserve an empty line if the source contains an empty line too.
|
||||
if !f.context().comments().has_leading(item)
|
||||
&& lines_after(range.end(), f.context().source()) != 0
|
||||
{
|
||||
empty_line().fmt(f)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
body.format().with_options(SuiteKind::TopLevel),
|
||||
// Trailing newline at the end of the file
|
||||
hard_line_break()
|
||||
]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue