mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(prettier): split ArrayExpression to single file (#1359)
This commit is contained in:
parent
e65ec46705
commit
3267437128
2 changed files with 85 additions and 28 deletions
72
crates/oxc_prettier/src/format/array.rs
Normal file
72
crates/oxc_prettier/src/format/array.rs
Normal file
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue