mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
fix(prettier) use print_assignment for ObjectProperty (#1598)
This commit is contained in:
parent
da87b9b29e
commit
0134211b6f
3 changed files with 30 additions and 10 deletions
|
|
@ -1,8 +1,8 @@
|
|||
use oxc_ast::{
|
||||
ast::{
|
||||
AccessorProperty, AssignmentExpression, AssignmentTarget, AssignmentTargetPattern,
|
||||
AssignmentTargetProperty, BindingPatternKind, Expression, PropertyDefinition, Statement,
|
||||
VariableDeclarator,
|
||||
AssignmentTargetProperty, BindingPatternKind, Expression, ObjectProperty,
|
||||
PropertyDefinition, PropertyKind, Statement, VariableDeclarator,
|
||||
},
|
||||
AstKind,
|
||||
};
|
||||
|
|
@ -49,6 +49,7 @@ pub(super) enum AssignmentLikeNode<'a, 'b> {
|
|||
VariableDeclarator(&'b VariableDeclarator<'a>),
|
||||
PropertyDefinition(&'b PropertyDefinition<'a>),
|
||||
AccessorProperty(&'b AccessorProperty<'a>),
|
||||
ObjectProperty(&'b ObjectProperty<'a>),
|
||||
}
|
||||
|
||||
impl<'a, 'b> From<ClassMemberish<'a, 'b>> for AssignmentLikeNode<'a, 'b> {
|
||||
|
|
@ -209,7 +210,7 @@ fn choose_layout<'a>(
|
|||
| Expression::ClassExpression(_)
|
||||
))
|
||||
{
|
||||
return Layout::BreakAfterOperator;
|
||||
return Layout::NeverBreakAfterOperator;
|
||||
}
|
||||
|
||||
Layout::Fluid
|
||||
|
|
@ -279,6 +280,7 @@ fn is_arrow_function_variable_declarator(expr: &AssignmentLikeNode) -> bool {
|
|||
}
|
||||
AssignmentLikeNode::AssignmentExpression(_)
|
||||
| AssignmentLikeNode::PropertyDefinition(_)
|
||||
| AssignmentLikeNode::ObjectProperty(_)
|
||||
| AssignmentLikeNode::AccessorProperty(_) => false,
|
||||
}
|
||||
}
|
||||
|
|
@ -288,5 +290,11 @@ fn is_object_property_with_short_key<'a>(
|
|||
expr: &AssignmentLikeNode<'a, '_>,
|
||||
left_doc: &Doc<'a>,
|
||||
) -> bool {
|
||||
false
|
||||
let AssignmentLikeNode::ObjectProperty(object_prop) = expr else { return false };
|
||||
|
||||
if object_prop.method || object_prop.kind != PropertyKind::Init {
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1439,10 +1439,7 @@ impl<'a> Format<'a> for ObjectPropertyKind<'a> {
|
|||
impl<'a> Format<'a> for ObjectProperty<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
wrap!(p, self, ObjectProperty, {
|
||||
// Perf: Use same print function with BindingProperty
|
||||
if self.shorthand {
|
||||
self.key.format(p)
|
||||
} else {
|
||||
if self.method || self.kind == PropertyKind::Get || self.kind == PropertyKind::Set {
|
||||
let mut parts = p.vec();
|
||||
let mut method = self.method;
|
||||
match self.kind {
|
||||
|
|
@ -1471,8 +1468,22 @@ impl<'a> Format<'a> for ObjectProperty<'a> {
|
|||
parts.push(ss!(": "));
|
||||
parts.push(format!(p, self.value));
|
||||
}
|
||||
Doc::Group(Group::new(parts, false))
|
||||
return Doc::Group(Group::new(parts, false));
|
||||
}
|
||||
|
||||
if self.shorthand {
|
||||
return self.value.format(p);
|
||||
}
|
||||
|
||||
let left_doc = format!(p, self.key);
|
||||
|
||||
assignment::print_assignment(
|
||||
p,
|
||||
assignment::AssignmentLikeNode::ObjectProperty(self),
|
||||
left_doc,
|
||||
ss!(":"),
|
||||
Some(&self.value),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
Compatibility: 211/561 (37.61%)
|
||||
Compatibility: 210/561 (37.43%)
|
||||
|
||||
# Failed
|
||||
|
||||
|
|
@ -59,6 +59,7 @@ Compatibility: 211/561 (37.61%)
|
|||
* assignment-comments/call2.js
|
||||
* assignment-comments/function.js
|
||||
* assignment-comments/identifier.js
|
||||
* assignment-comments/number.js
|
||||
* assignment-comments/string.js
|
||||
|
||||
### async
|
||||
|
|
|
|||
Loading…
Reference in a new issue