Fix dangling module comments (#7456)

This commit is contained in:
Micha Reiser 2023-09-17 16:56:41 +02:00 committed by GitHub
parent 959338d39d
commit 26ae0a6e8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 32 deletions

View file

@ -1,10 +1,10 @@
use ruff_formatter::prelude::hard_line_break;
use ruff_formatter::{Buffer, FormatResult};
use ruff_formatter::write;
use ruff_python_ast::ModModule;
use crate::comments::{trailing_comments, SourceComment};
use crate::comments::SourceComment;
use crate::prelude::*;
use crate::statement::suite::SuiteKind;
use crate::{write, AsFormat, FormatNodeRule, PyFormatter};
use crate::FormatNodeRule;
#[derive(Default)]
pub struct FormatModModule;
@ -12,13 +12,11 @@ pub struct FormatModModule;
impl FormatNodeRule<ModModule> for FormatModModule {
fn fmt_fields(&self, item: &ModModule, f: &mut PyFormatter) -> FormatResult<()> {
let ModModule { range: _, body } = item;
let comments = f.context().comments().clone();
write!(
f,
[
body.format().with_options(SuiteKind::TopLevel),
trailing_comments(comments.dangling(item)),
// Trailing newline at the end of the file
hard_line_break()
]