refactor(prettier) Remove flat_contents from IfBreak (#1324)

This commit is contained in:
Cameron 2023-11-15 02:13:30 +00:00 committed by GitHub
parent 71b3f48af1
commit 9c170b89ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 19 deletions

View file

@ -31,19 +31,13 @@ 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(IfBreak<'a>),
}
#[derive(Debug)]
pub struct IfBreak<'a> {
pub break_contents: Vec<'a, Doc<'a>>,
pub flat_contents: Vec<'a, Doc<'a>>,
IfBreak(Vec<'a, Doc<'a>>),
}
impl<'a> Doc<'a> {
#[must_use]
pub fn if_break(break_contents: Vec<'a, Doc<'a>>, flat_contents: Vec<'a, Doc<'a>>) -> Self {
Doc::IfBreak(IfBreak { break_contents, flat_contents })
pub fn if_break(break_contents: Vec<'a, Doc<'a>>) -> Self {
Doc::IfBreak(break_contents)
}
}

View file

@ -780,7 +780,7 @@ impl<'a> Format<'a> for ArrayExpression<'a> {
let mut if_break_comma = p.vec();
if_break_comma.push(Doc::Str(","));
parts_inner.push(Doc::if_break(if_break_comma, p.vec()));
parts_inner.push(Doc::if_break(if_break_comma));
parts.push(group!(p, Doc::Indent(parts_inner)));

View file

@ -12,7 +12,7 @@ impl<'a> Command<'a> {
}
}
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum Mode {
Break,
Flat,

View file

@ -7,10 +7,7 @@ mod command;
use std::collections::VecDeque;
use crate::{
doc::{Doc, IfBreak},
PrettierOptions,
};
use crate::{doc::Doc, PrettierOptions};
use self::command::{Command, Indent, Mode};
@ -125,10 +122,17 @@ impl<'a> Printer<'a> {
self.pos = self.indent(indent.length);
}
fn handle_if_break(&mut self, if_break: IfBreak<'a>, indent: Indent, mode: Mode) {
let IfBreak { break_contents, .. } = if_break;
self.cmds
.extend(break_contents.into_iter().rev().map(|doc| Command::new(indent, mode, doc)));
fn handle_if_break(
&mut self,
if_break: oxc_allocator::Vec<'a, Doc<'a>>,
indent: Indent,
mode: Mode,
) {
if mode == Mode::Break {
self.cmds.extend(
if_break.into_iter().rev().map(|doc| Command::new(indent, Mode::Break, doc)),
);
}
}
#[allow(clippy::cast_possible_wrap)]