forked from oxc-project/oxc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prettier): split
ArrayExpression
to single file (oxc-project#1359
- Loading branch information
1 parent
e65ec46
commit 3267437
Showing
2 changed files
with
85 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters