From 2d3b5eb299b065d91e4ec64354c6a4dd9b96d3cc Mon Sep 17 00:00:00 2001 From: Boshen Date: Fri, 17 Nov 2023 19:38:25 +0800 Subject: [PATCH] feat(prettier): add comments before expressions (#1375) --- crates/oxc_prettier/src/comment.rs | 12 ++++++++---- crates/oxc_prettier/src/format/mod.rs | 3 +++ crates/oxc_prettier/src/lib.rs | 9 +++------ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/crates/oxc_prettier/src/comment.rs b/crates/oxc_prettier/src/comment.rs index a5a14ea85..ab42411da 100644 --- a/crates/oxc_prettier/src/comment.rs +++ b/crates/oxc_prettier/src/comment.rs @@ -35,12 +35,16 @@ impl<'a> Prettier<'a> { false } + #[must_use] pub(crate) fn print_leading_comments(&mut self, range: Span) -> Option> { - let mut parts = vec![]; + let mut parts = self.vec(); while let Some((start, end, kind)) = self.trivias.peek().copied() { // Comment before the span if end <= range.start { parts.push(self.print_comment(start, end, kind)); + if kind.is_multi_line() { + parts.push(hardline!()); + } self.trivias.next(); } else { break; @@ -49,11 +53,10 @@ impl<'a> Prettier<'a> { if parts.is_empty() { return None; } - let mut comments = self.join(Separator::Hardline, parts); - comments.push(hardline!()); - Some(Doc::Array(comments)) + Some(Doc::Array(parts)) } + #[must_use] pub(crate) fn print_dangling_comments(&mut self, range: Span) -> Option> { let mut parts = vec![]; while let Some((start, end, kind)) = self.trivias.peek().copied() { @@ -68,6 +71,7 @@ impl<'a> Prettier<'a> { (!parts.is_empty()).then(|| Doc::Array(self.join(Separator::Hardline, parts))) } + #[must_use] fn print_comment(&self, start: u32, end: u32, kind: CommentKind) -> Doc<'a> { let end_offset = if kind.is_multi_line() { 2 } else { 0 }; let comment = Span::new(start - 2, end + end_offset).source_text(self.source_text); diff --git a/crates/oxc_prettier/src/format/mod.rs b/crates/oxc_prettier/src/format/mod.rs index 4fe675c23..9b80ecbb1 100644 --- a/crates/oxc_prettier/src/format/mod.rs +++ b/crates/oxc_prettier/src/format/mod.rs @@ -91,6 +91,9 @@ impl<'a> Format<'a> for Statement<'a> { impl<'a> Format<'a> for ExpressionStatement<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { let mut parts = p.vec(); + if let Some(doc) = p.print_leading_comments(self.span) { + parts.push(doc); + } parts.push(self.expression.format(p)); if p.options.semi { parts.push(ss!(";")); diff --git a/crates/oxc_prettier/src/lib.rs b/crates/oxc_prettier/src/lib.rs index 8651ec690..57c0d1e2e 100644 --- a/crates/oxc_prettier/src/lib.rs +++ b/crates/oxc_prettier/src/lib.rs @@ -10,10 +10,7 @@ mod options; mod printer; mod util; -use std::{ - iter::{Peekable, Rev}, - vec, -}; +use std::{iter::Peekable, vec}; use doc::Doc; use oxc_allocator::Allocator; @@ -31,7 +28,7 @@ pub struct Prettier<'a> { options: PrettierOptions, /// A stack of comments that will be carefully placed in the right places. - trivias: Peekable>>, + trivias: Peekable>, } impl<'a> Prettier<'a> { @@ -41,7 +38,7 @@ impl<'a> Prettier<'a> { trivias: Trivias, options: PrettierOptions, ) -> Self { - let trivias = trivias.into_iter().rev().peekable(); + let trivias = trivias.into_iter().peekable(); Self { allocator, source_text, options, trivias } }