mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(prettier): print empty array (#1407)
This commit is contained in:
parent
24c2580435
commit
97fee26e25
2 changed files with 46 additions and 5 deletions
|
|
@ -5,7 +5,7 @@ use oxc_span::Span;
|
|||
|
||||
use crate::{
|
||||
doc::{Doc, Separator},
|
||||
hardline, Prettier,
|
||||
hardline, indent, Prettier,
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
|
|
@ -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 {
|
||||
|
|
@ -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
|
||||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -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();
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue