Skip to content

Commit

Permalink
feat(prettier): print empty array (#1407)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryven authored Nov 18, 2023
1 parent 24c2580 commit 97fee26
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
28 changes: 25 additions & 3 deletions crates/oxc_prettier/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use oxc_span::Span;

use crate::{
doc::{Doc, Separator},
hardline, Prettier,
hardline, indent, Prettier,
};

#[derive(Clone, Copy)]
Expand All @@ -29,6 +29,18 @@ pub enum CommentFlags {
Last,
}

#[derive(Default)]
pub struct DanglingCommentsPrintOptions {
ident: bool,
}

impl DanglingCommentsPrintOptions {
pub(crate) fn with_ident(mut self, ident: bool) -> Self {
self.ident = ident;
self
}
}

impl<'a> Prettier<'a> {
#[allow(unused)]
pub(crate) fn has_comment(_span: Span, _flags: CommentFlags) -> bool {
Expand Down Expand Up @@ -57,7 +69,11 @@ impl<'a> Prettier<'a> {
}

#[must_use]
pub(crate) fn print_dangling_comments(&mut self, range: Span) -> Option<Doc<'a>> {
pub(crate) fn print_dangling_comments(
&mut self,
range: Span,
dangling_options: Option<DanglingCommentsPrintOptions>,
) -> Option<Doc<'a>> {
let mut parts = vec![];
while let Some((start, end, kind)) = self.trivias.peek().copied() {
// Comment within the span
Expand All @@ -68,7 +84,13 @@ impl<'a> Prettier<'a> {
break;
}
}
(!parts.is_empty()).then(|| Doc::Array(self.join(Separator::Hardline, parts)))
(!parts.is_empty()).then(|| Doc::Array(self.join(Separator::Hardline, parts))).map(|doc| {
if dangling_options.is_some_and(|options| options.ident) {
indent!(self, hardline!(), doc)
} else {
doc
}
})
}

#[must_use]
Expand Down
23 changes: 21 additions & 2 deletions crates/oxc_prettier/src/format/array.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#[allow(clippy::wildcard_imports)]
use oxc_ast::ast::*;
use oxc_span::Span;

use crate::{doc::Doc, group, if_break, ss, Prettier};
use crate::{
comment::DanglingCommentsPrintOptions, doc::Doc, group, if_break, softline, ss, Prettier,
};
use oxc_allocator::Vec;

use super::Format;
Expand All @@ -24,11 +27,19 @@ impl<'a, 'b> Array<'a, 'b> {
Self::ArrayAssignmentTarget(array) => array.elements.len(),
}
}
fn span(&self) -> Span {
match self {
Self::ArrayExpression(array) => array.span,
Self::TSTupleType(tuple) => tuple.span,
Self::ArrayPattern(array) => array.span,
Self::ArrayAssignmentTarget(array) => array.span,
}
}
}

pub(super) fn print_array<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) -> Doc<'a> {
if array.len() == 0 {
return ss!("[]");
return print_empty_array_elements(p, array);
}

let mut parts = p.vec();
Expand All @@ -46,6 +57,14 @@ pub(super) fn print_array<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) -> Do
Doc::Group(parts)
}

fn print_empty_array_elements<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) -> Doc<'a> {
let dangling_options = DanglingCommentsPrintOptions::default().with_ident(true);
p.print_dangling_comments(array.span(), Some(dangling_options)).map_or_else(
|| ss!("[]"),
|dangling_comments| group![p, ss!("["), dangling_comments, softline!(), ss!("]")],
)
}

fn print_elements<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) -> Vec<'a, Doc<'a>> {
let mut parts = p.vec();
match array {
Expand Down

0 comments on commit 97fee26

Please sign in to comment.