mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
refactor(prettier): clean up object::print_object_properties (#1573)
This commit is contained in:
parent
72dd72b466
commit
c5b138f006
5 changed files with 101 additions and 51 deletions
|
|
@ -885,6 +885,16 @@ pub struct ObjectAssignmentTarget<'a> {
|
||||||
pub rest: Option<AssignmentTarget<'a>>,
|
pub rest: Option<AssignmentTarget<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> ObjectAssignmentTarget<'a> {
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.properties.is_empty() && self.rest.is_none()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.properties.len() + usize::from(self.rest.is_some())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Hash)]
|
#[derive(Debug, Hash)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize), serde(untagged))]
|
#[cfg_attr(feature = "serde", derive(Serialize), serde(untagged))]
|
||||||
pub enum AssignmentTargetMaybeDefault<'a> {
|
pub enum AssignmentTargetMaybeDefault<'a> {
|
||||||
|
|
@ -1441,6 +1451,10 @@ impl<'a> ObjectPattern<'a> {
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.properties.is_empty() && self.rest.is_none()
|
self.properties.is_empty() && self.rest.is_none()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.properties.len() + usize::from(self.rest.is_some())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Hash)]
|
#[derive(Debug, Hash)]
|
||||||
|
|
@ -1467,6 +1481,10 @@ impl<'a> ArrayPattern<'a> {
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.elements.is_empty() && self.rest.is_none()
|
self.elements.is_empty() && self.rest.is_none()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.elements.len() + usize::from(self.rest.is_some())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Hash)]
|
#[derive(Debug, Hash)]
|
||||||
|
|
|
||||||
|
|
@ -182,6 +182,7 @@ fn print_elements<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) -> Doc<'a> {
|
||||||
if let Some(rest) = &array_pat.rest {
|
if let Some(rest) = &array_pat.rest {
|
||||||
parts.push(ss!(","));
|
parts.push(ss!(","));
|
||||||
parts.push(line!());
|
parts.push(line!());
|
||||||
|
parts.push(ss!("..."));
|
||||||
parts.push(rest.format(p));
|
parts.push(rest.format(p));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,9 @@ pub(super) fn print_function_parameters<'a>(
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(rest) = ¶ms.rest {
|
if let Some(rest) = ¶ms.rest {
|
||||||
parts.push(ss!(", "));
|
if !params.items.is_empty() {
|
||||||
|
parts.push(ss!(", "));
|
||||||
|
}
|
||||||
parts.push(rest.format(p));
|
parts.push(rest.format(p));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1455,11 +1455,7 @@ impl<'a> Format<'a> for ArrayExpression<'a> {
|
||||||
impl<'a> Format<'a> for ObjectExpression<'a> {
|
impl<'a> Format<'a> for ObjectExpression<'a> {
|
||||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||||
wrap!(p, self, ObjectExpression, {
|
wrap!(p, self, ObjectExpression, {
|
||||||
object::print_object_properties(
|
object::print_object_properties(p, ObjectLike::Expression(self))
|
||||||
p,
|
|
||||||
&ObjectLike::ObjectExpression(self),
|
|
||||||
&self.properties,
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1698,11 +1694,7 @@ impl<'a> Format<'a> for AssignmentTargetMaybeDefault<'a> {
|
||||||
|
|
||||||
impl<'a> Format<'a> for ObjectAssignmentTarget<'a> {
|
impl<'a> Format<'a> for ObjectAssignmentTarget<'a> {
|
||||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||||
object::print_object_properties(
|
object::print_object_properties(p, ObjectLike::AssignmentTarget(self))
|
||||||
p,
|
|
||||||
&ObjectLike::ObjectAssignmentTarget(self),
|
|
||||||
&self.properties,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2074,7 +2066,7 @@ impl<'a> Format<'a> for BindingPattern<'a> {
|
||||||
|
|
||||||
impl<'a> Format<'a> for ObjectPattern<'a> {
|
impl<'a> Format<'a> for ObjectPattern<'a> {
|
||||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||||
object::print_object_properties(p, &ObjectLike::ObjectPattern(self), &self.properties)
|
object::print_object_properties(p, ObjectLike::Pattern(self))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
use oxc_allocator::Vec;
|
|
||||||
use oxc_ast::ast::{ObjectAssignmentTarget, ObjectExpression, ObjectPattern};
|
use oxc_ast::ast::{ObjectAssignmentTarget, ObjectExpression, ObjectPattern};
|
||||||
use oxc_span::{GetSpan, Span};
|
use oxc_span::Span;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
doc::{Doc, DocBuilder, Group},
|
doc::{Doc, DocBuilder, Group},
|
||||||
|
|
@ -9,62 +8,100 @@ use crate::{
|
||||||
|
|
||||||
use super::{misc, Format};
|
use super::{misc, Format};
|
||||||
|
|
||||||
#[allow(clippy::enum_variant_names)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum ObjectLike<'a, 'b> {
|
pub enum ObjectLike<'a, 'b> {
|
||||||
ObjectExpression(&'b ObjectExpression<'a>),
|
Expression(&'b ObjectExpression<'a>),
|
||||||
ObjectAssignmentTarget(&'b ObjectAssignmentTarget<'a>),
|
AssignmentTarget(&'b ObjectAssignmentTarget<'a>),
|
||||||
ObjectPattern(&'b ObjectPattern<'a>),
|
Pattern(&'b ObjectPattern<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectLike<'_, '_> {
|
impl<'a, 'b> ObjectLike<'a, 'b> {
|
||||||
fn is_object_pattern(&self) -> bool {
|
fn len(&self) -> usize {
|
||||||
matches!(self, ObjectLike::ObjectPattern(_))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ObjectLike<'_, '_> {
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
match self {
|
match self {
|
||||||
ObjectLike::ObjectExpression(object) => object.span,
|
ObjectLike::Expression(object) => object.properties.len(),
|
||||||
ObjectLike::ObjectAssignmentTarget(object) => object.span,
|
ObjectLike::AssignmentTarget(object) => object.properties.len(),
|
||||||
ObjectLike::ObjectPattern(object) => object.span,
|
ObjectLike::Pattern(object) => object.properties.len(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_empty(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
ObjectLike::Expression(object) => object.properties.is_empty(),
|
||||||
|
ObjectLike::AssignmentTarget(object) => object.is_empty(),
|
||||||
|
ObjectLike::Pattern(object) => object.is_empty(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_object_pattern(&self) -> bool {
|
||||||
|
matches!(self, ObjectLike::Pattern(_))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
match self {
|
||||||
|
ObjectLike::Expression(object) => object.span,
|
||||||
|
ObjectLike::AssignmentTarget(object) => object.span,
|
||||||
|
ObjectLike::Pattern(object) => object.span,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn iter(&'b self, p: &'b mut Prettier<'a>) -> Box<dyn Iterator<Item = Doc<'a>> + 'b> {
|
||||||
|
match self {
|
||||||
|
ObjectLike::Expression(object) => {
|
||||||
|
Box::new(object.properties.iter().map(|prop| prop.format(p)))
|
||||||
|
}
|
||||||
|
ObjectLike::AssignmentTarget(object) => {
|
||||||
|
Box::new(object.properties.iter().map(|prop| prop.format(p)))
|
||||||
|
}
|
||||||
|
ObjectLike::Pattern(object) => {
|
||||||
|
Box::new(object.properties.iter().map(|prop| prop.format(p)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn print_object_properties<'a, F: Format<'a> + GetSpan>(
|
pub(super) fn print_object_properties<'a>(
|
||||||
p: &mut Prettier<'a>,
|
p: &mut Prettier<'a>,
|
||||||
object: &ObjectLike<'a, '_>,
|
object: ObjectLike<'a, '_>,
|
||||||
properties: &Vec<'a, F>,
|
|
||||||
) -> Doc<'a> {
|
) -> Doc<'a> {
|
||||||
let left_brace = ss!("{");
|
let left_brace = ss!("{");
|
||||||
let right_brace = ss!("}");
|
let right_brace = ss!("}");
|
||||||
|
|
||||||
let content = if properties.is_empty() {
|
let content = if object.is_empty() {
|
||||||
group![p, left_brace, softline!(), right_brace]
|
group![p, left_brace, softline!(), right_brace]
|
||||||
} else {
|
} else {
|
||||||
let mut parts = p.vec();
|
let mut parts = p.vec();
|
||||||
parts.push(ss!("{"));
|
parts.push(ss!("{"));
|
||||||
|
parts.push(Doc::Indent({
|
||||||
let mut indent_parts = p.vec();
|
let len = object.len();
|
||||||
indent_parts.push(if p.options.bracket_spacing { line!() } else { softline!() });
|
let mut indent_parts = p.vec();
|
||||||
for (i, prop) in properties.iter().enumerate() {
|
indent_parts.push(if p.options.bracket_spacing { line!() } else { softline!() });
|
||||||
indent_parts.push(prop.format(p));
|
for (i, doc) in object.iter(p).enumerate() {
|
||||||
if i < properties.len() - 1 {
|
indent_parts.push(doc);
|
||||||
indent_parts.push(Doc::Str(","));
|
if i < len - 1 {
|
||||||
indent_parts.push(line!());
|
indent_parts.push(ss!(","));
|
||||||
|
indent_parts.push(line!());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
match object {
|
||||||
|
ObjectLike::Expression(object) => {}
|
||||||
parts.push(Doc::Indent(indent_parts));
|
ObjectLike::AssignmentTarget(object) => {
|
||||||
|
if let Some(rest) = &object.rest {
|
||||||
|
indent_parts.push(ss!("..."));
|
||||||
|
indent_parts.push(rest.format(p));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ObjectLike::Pattern(object) => {
|
||||||
|
if let Some(rest) = &object.rest {
|
||||||
|
indent_parts.push(ss!(","));
|
||||||
|
indent_parts.push(line!());
|
||||||
|
indent_parts.push(rest.format(p));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
indent_parts
|
||||||
|
}));
|
||||||
parts.push(if_break!(p, ",", "", None));
|
parts.push(if_break!(p, ",", "", None));
|
||||||
|
parts.push(if p.options.bracket_spacing { line!() } else { softline!() });
|
||||||
if p.options.bracket_spacing {
|
|
||||||
parts.push(line!());
|
|
||||||
} else {
|
|
||||||
parts.push(softline!());
|
|
||||||
}
|
|
||||||
|
|
||||||
parts.push(ss!("}"));
|
parts.push(ss!("}"));
|
||||||
|
|
||||||
if object.is_object_pattern() {
|
if object.is_object_pattern() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue