From 3267437128a86d865cf4935dd3b375a951bbefcb Mon Sep 17 00:00:00 2001 From: Wenzhe Wang Date: Fri, 17 Nov 2023 00:03:29 +0800 Subject: [PATCH] feat(prettier): split `ArrayExpression` to single file (#1359) --- crates/oxc_prettier/src/format/array.rs | 72 +++++++++++++++++++++++++ crates/oxc_prettier/src/format/mod.rs | 41 +++++--------- 2 files changed, 85 insertions(+), 28 deletions(-) create mode 100644 crates/oxc_prettier/src/format/array.rs diff --git a/crates/oxc_prettier/src/format/array.rs b/crates/oxc_prettier/src/format/array.rs new file mode 100644 index 0000000000000..bfdd2a01965a0 --- /dev/null +++ b/crates/oxc_prettier/src/format/array.rs @@ -0,0 +1,72 @@ +#[allow(clippy::wildcard_imports)] +use oxc_ast::ast::*; + +use crate::{doc::Doc, group, if_break, ss, Prettier}; +use oxc_allocator::Vec; + +use super::Format; + +pub enum Array<'a, 'b> { + ArrayExpression(&'b ArrayExpression<'a>), + #[allow(unused)] + TSTupleType(&'b TSTupleType<'a>), +} + +impl<'a, 'b> Array<'a, 'b> { + fn len(&self) -> usize { + match self { + Self::ArrayExpression(array) => array.elements.len(), + Self::TSTupleType(tuple) => tuple.element_types.len(), + } + } +} + +impl<'a> Prettier<'a> { + pub(super) fn print_array(&mut self, array: &Array<'a, '_>) -> Doc<'a> { + if array.len() == 0 { + return ss!("[]"); + } + + let mut parts = self.vec(); + parts.push(ss!("[")); + + let mut parts_inner = self.vec(); + parts_inner.push(Doc::Softline); + parts_inner.extend(print_elements(self, array)); + parts_inner.push(if_break!(self, ",")); + + parts.push(group!(self, Doc::Indent(parts_inner))); + parts.push(Doc::Softline); + parts.push(ss!("]")); + + Doc::Group(parts) + } +} + +fn print_elements<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) -> Vec<'a, Doc<'a>> { + let mut parts = p.vec(); + match array { + Array::ArrayExpression(array) => { + for (i, element) in array.elements.iter().enumerate() { + if i > 0 && i < array.elements.len() { + parts.push(ss!(",")); + parts.push(Doc::Line); + } + + parts.push(element.format(p)); + } + } + Array::TSTupleType(tuple) => { + for (i, element) in tuple.element_types.iter().enumerate() { + if i > 0 && i < tuple.element_types.len() { + parts.push(ss!(",")); + parts.push(Doc::Line); + } + + parts.push(element.format(p)); + } + } + } + + parts +} diff --git a/crates/oxc_prettier/src/format/mod.rs b/crates/oxc_prettier/src/format/mod.rs index 658272eebe9f6..cffc7d0bbb338 100644 --- a/crates/oxc_prettier/src/format/mod.rs +++ b/crates/oxc_prettier/src/format/mod.rs @@ -10,6 +10,7 @@ use oxc_allocator::{Box, Vec}; use oxc_ast::ast::*; use oxc_span::GetSpan; +mod array; mod arrow_function; mod binaryish; mod block; @@ -25,12 +26,15 @@ mod ternary; use crate::{ array, doc::{Doc, Separator}, - format, group, hardline, if_break, indent, softline, ss, string, + format, group, hardline, indent, softline, ss, string, util::is_next_line_empty, Prettier, }; -use self::binaryish::{BinaryishLeft, BinaryishOperator}; +use self::{ + array::Array, + binaryish::{BinaryishLeft, BinaryishOperator}, +}; pub trait Format<'a> { #[must_use] @@ -436,6 +440,12 @@ impl<'a> Format<'a> for TSTypeParameterDeclaration<'a> { } } +impl<'a> Format<'a> for TSTupleElement<'a> { + fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { + Doc::Line + } +} + impl<'a> Format<'a> for VariableDeclarator<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { let mut parts = p.vec(); @@ -811,32 +821,7 @@ impl<'a> Format<'a> for SpreadElement<'a> { impl<'a> Format<'a> for ArrayExpression<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - if self.elements.len() == 0 { - return ss!("[]"); - } - - let mut parts = p.vec(); - parts.push(ss!("[")); - - let mut parts_inner = p.vec(); - parts_inner.push(Doc::Softline); - - for (i, element) in self.elements.iter().enumerate() { - if i > 0 && i < self.elements.len() { - parts_inner.push(ss!(",")); - parts_inner.push(Doc::Line); - } - parts_inner.push(format!(p, element)); - } - - parts_inner.push(if_break!(p, ",")); - - parts.push(group!(p, Doc::Indent(parts_inner))); - - parts.push(Doc::Softline); - parts.push(ss!("]")); - - Doc::Group(parts) + p.print_array(&Array::ArrayExpression(self)) } }