From b4e90a723a53389f5d57760414fbd7c7dd226639 Mon Sep 17 00:00:00 2001 From: Cameron Date: Fri, 1 Dec 2023 12:52:26 +0000 Subject: [PATCH] feat(prettier): implement `has_comment`, improve blank lines when printing arrays (#1601) --- crates/oxc_prettier/src/comments/mod.rs | 10 +++++++ crates/oxc_prettier/src/comments/print.rs | 34 +++++++++++++++++++++-- crates/oxc_prettier/src/format/array.rs | 8 +++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/crates/oxc_prettier/src/comments/mod.rs b/crates/oxc_prettier/src/comments/mod.rs index 47358c1d4..9f303d6a3 100644 --- a/crates/oxc_prettier/src/comments/mod.rs +++ b/crates/oxc_prettier/src/comments/mod.rs @@ -26,6 +26,16 @@ impl Comment { self.has_line_suffix = yes; self } + + pub fn matches_flags(self, flags: CommentFlags) -> bool { + if flags.contains(CommentFlags::Block) && !self.is_block { + return false; + } + if flags.contains(CommentFlags::Line) && self.is_block { + return false; + } + true + } } #[derive(Default)] diff --git a/crates/oxc_prettier/src/comments/print.rs b/crates/oxc_prettier/src/comments/print.rs index dea8917e8..1c674e645 100644 --- a/crates/oxc_prettier/src/comments/print.rs +++ b/crates/oxc_prettier/src/comments/print.rs @@ -31,8 +31,38 @@ impl<'a> Prettier<'a> { doc } - #[allow(unused)] - pub(crate) fn has_comment(_span: Span, _flags: CommentFlags) -> bool { + pub(crate) fn has_comment(&mut self, range: Span, flags: CommentFlags) -> bool { + let mut peekable_trivias = self.trivias.clone(); + + while let Some(&(start, end, kind)) = peekable_trivias.peek() { + let mut should_break = true; + let comment = Comment::new(start, end, kind); + + if range.end < comment.start + && self.source_text[range.end as usize..comment.start as usize] + .chars() + .all(|c| c == ' ') + { + if flags.contains(CommentFlags::Trailing) && comment.matches_flags(flags) { + return true; + } + + should_break = false; + } + + if comment.end <= range.end { + if flags.contains(CommentFlags::Dangling) && comment.matches_flags(flags) { + return true; + } + should_break = false; + } + + if should_break { + break; + } + peekable_trivias.next(); + } + false } diff --git a/crates/oxc_prettier/src/format/array.rs b/crates/oxc_prettier/src/format/array.rs index 4d677f51f..fd7013875 100644 --- a/crates/oxc_prettier/src/format/array.rs +++ b/crates/oxc_prettier/src/format/array.rs @@ -4,7 +4,7 @@ use oxc_syntax::operator::UnaryOperator; use crate::{ array, - comments::DanglingCommentsPrintOptions, + comments::{CommentFlags, DanglingCommentsPrintOptions}, doc::{Doc, DocBuilder, Fill, Group}, group, hardline, if_break, indent, line, softline, ss, Prettier, }; @@ -217,6 +217,12 @@ where space_parts.extend(hardline!()); space_parts.extend(hardline!()); parts.push(Doc::Array(space_parts)); + } else if array.elements.get(i + 1).is_some_and(|next| { + p.has_comment(next.span(), CommentFlags::Leading | CommentFlags::Line) + }) { + let mut space_parts = p.vec(); + space_parts.extend(hardline!()); + parts.push(Doc::Array(space_parts)); } else { parts.push(line!()); }