mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(prettier): use allocator for IfBreak's Box (#1339)
This commit is contained in:
parent
b443a0798f
commit
cf3318c609
4 changed files with 13 additions and 10 deletions
|
|
@ -3,7 +3,7 @@
|
|||
//! References:
|
||||
//! * <https://github.com/prettier/prettier/blob/main/commands.md>
|
||||
|
||||
use oxc_allocator::{String, Vec};
|
||||
use oxc_allocator::{Box, String, Vec};
|
||||
|
||||
use crate::Prettier;
|
||||
|
||||
|
|
@ -31,11 +31,9 @@ pub enum Doc<'a> {
|
|||
/// no matter if the expression fits on one line or not.
|
||||
Hardline,
|
||||
/// Print something if the current `group` or the current element of `fill` breaks and something else if it doesn't.
|
||||
IfBreak(Box<Doc<'a>>),
|
||||
IfBreak(Box<'a, Doc<'a>>),
|
||||
}
|
||||
|
||||
impl<'a> Doc<'a> {}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[allow(unused)]
|
||||
pub enum Separator {
|
||||
|
|
@ -55,6 +53,11 @@ impl<'a> Prettier<'a> {
|
|||
Doc::Str(String::from_str_in(s, self.allocator).into_bump_str())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn alloc(&self, doc: Doc<'a>) -> Box<'a, Doc<'a>> {
|
||||
Box(self.allocator.alloc(doc))
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) fn join(&self, separator: Separator, docs: std::vec::Vec<Doc<'a>>) -> Doc<'a> {
|
||||
let mut parts = self.vec();
|
||||
|
|
|
|||
|
|
@ -256,10 +256,10 @@ impl<'a> Format<'a> for ReturnStatement<'a> {
|
|||
parts.push(ss!(" "));
|
||||
parts.push(group![
|
||||
p,
|
||||
if_break!("("),
|
||||
if_break!(p, "("),
|
||||
indent!(p, softline!(), format!(p, argument)),
|
||||
softline!(),
|
||||
if_break!(")")
|
||||
if_break!(p, ")")
|
||||
]);
|
||||
}
|
||||
parts.push(p.str(";"));
|
||||
|
|
@ -792,7 +792,7 @@ impl<'a> Format<'a> for ArrayExpression<'a> {
|
|||
parts_inner.push(format!(p, element));
|
||||
}
|
||||
|
||||
parts_inner.push(if_break!(","));
|
||||
parts_inner.push(if_break!(p, ","));
|
||||
|
||||
parts.push(group!(p, Doc::Indent(parts_inner)));
|
||||
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ macro_rules! group {
|
|||
|
||||
#[macro_export]
|
||||
macro_rules! if_break {
|
||||
($s:expr) => {{
|
||||
Doc::IfBreak(std::boxed::Box::new(Doc::Str($s)))
|
||||
($p:ident, $s:expr) => {{
|
||||
Doc::IfBreak($p.alloc(Doc::Str($s)))
|
||||
}};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ impl<'a> Printer<'a> {
|
|||
Doc::Line => self.handle_line(indent, mode),
|
||||
Doc::Softline => self.handle_softline(indent, mode),
|
||||
Doc::Hardline => self.handle_hardline(indent),
|
||||
Doc::IfBreak(if_break) => self.handle_if_break(*if_break, indent, mode),
|
||||
Doc::IfBreak(doc) => self.handle_if_break(doc.unbox(), indent, mode),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue