Skip to content

Commit

Permalink
feat(prettier): split ArrayExpression to single file (oxc-project#1359
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mysteryven authored Nov 16, 2023
1 parent e65ec46 commit 3267437
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 28 deletions.
72 changes: 72 additions & 0 deletions crates/oxc_prettier/src/format/array.rs
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 13 additions & 28 deletions crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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))
}
}

Expand Down

0 comments on commit 3267437

Please sign in to comment.