mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
fix(prettier): object pattern in function parameters (#1595)
This commit is contained in:
parent
9f38072002
commit
e3c54b92c9
6 changed files with 57 additions and 26 deletions
|
|
@ -1,4 +1,5 @@
|
|||
use oxc_ast::ast::*;
|
||||
use oxc_span::GetSpan;
|
||||
|
||||
use crate::{
|
||||
array,
|
||||
|
|
@ -42,7 +43,9 @@ pub(super) fn print_class_body<'a>(p: &mut Prettier<'a>, class_body: &ClassBody<
|
|||
if i < class_body.body.len() - 1 {
|
||||
parts_inner.extend(hardline!());
|
||||
|
||||
// TODO: if the next line is empty, add another hardline
|
||||
if p.is_next_line_empty(node.span()) {
|
||||
parts_inner.extend(hardline!());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -213,14 +216,6 @@ fn should_print_semicolon_after_class_property<'a>(
|
|||
}
|
||||
}
|
||||
|
||||
if !next_node.r#static() {
|
||||
if let ClassElement::PropertyDefinition(def) = next_node {
|
||||
if !def.declare {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match next_node {
|
||||
ClassElement::PropertyDefinition(property_definition) => property_definition.computed,
|
||||
ClassElement::TSAbstractPropertyDefinition(property_definition) => {
|
||||
|
|
|
|||
|
|
@ -41,8 +41,10 @@ pub(super) fn print_function<'a>(
|
|||
parts.push(ss!(" "));
|
||||
parts.push(body.format(p));
|
||||
}
|
||||
if p.options.semi && (func.is_ts_declare_function() || func.body.is_none()) {
|
||||
parts.push(p.str(";"));
|
||||
if func.is_ts_declare_function() || func.body.is_none() {
|
||||
if let Some(semi) = p.semi() {
|
||||
parts.push(semi);
|
||||
}
|
||||
}
|
||||
|
||||
Doc::Array(parts)
|
||||
|
|
@ -120,7 +122,9 @@ pub(super) fn print_return_or_throw_argument<'a>(
|
|||
);
|
||||
}
|
||||
|
||||
parts.push(p.str(";"));
|
||||
if let Some(semi) = p.semi() {
|
||||
parts.push(semi);
|
||||
}
|
||||
Doc::Array(parts)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,13 @@ use crate::{
|
|||
ss, Format, Prettier,
|
||||
};
|
||||
|
||||
pub(super) fn should_hug_the_only_function_parameter(params: &FormalParameters<'_>) -> bool {
|
||||
if params.parameters_count() != 1 {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub(super) fn print_function_parameters<'a>(
|
||||
p: &mut Prettier<'a>,
|
||||
params: &FormalParameters<'a>,
|
||||
|
|
|
|||
|
|
@ -623,6 +623,12 @@ impl<'a> Format<'a> for VariableDeclaration<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Format<'a> for VariableDeclarator<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
wrap!(p, self, VariableDeclarator, { assignment::print_variable_declarator(p, self) })
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Format<'a> for UsingDeclaration<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
line!()
|
||||
|
|
@ -956,12 +962,6 @@ impl<'a> Format<'a> for TSTupleElement<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Format<'a> for VariableDeclarator<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
assignment::print_variable_declarator(p, self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Format<'a> for Function<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
wrap!(p, self, Function, { function::print_function(p, self, None) })
|
||||
|
|
@ -986,7 +986,7 @@ impl<'a> Format<'a> for FormalParameters<'a> {
|
|||
|
||||
impl<'a> Format<'a> for FormalParameter<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
self.pattern.format(p)
|
||||
wrap!(p, self, FormalParameter, { self.pattern.format(p) })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2074,7 +2074,9 @@ impl<'a> Format<'a> for BindingPattern<'a> {
|
|||
|
||||
impl<'a> Format<'a> for ObjectPattern<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
object::print_object_properties(p, ObjectLike::Pattern(self))
|
||||
wrap!(p, self, ObjectPattern, {
|
||||
object::print_object_properties(p, ObjectLike::Pattern(self))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2096,13 +2098,15 @@ impl<'a> Format<'a> for RestElement<'a> {
|
|||
|
||||
impl<'a> Format<'a> for ArrayPattern<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
array::print_array(p, &Array::ArrayPattern(self))
|
||||
wrap!(p, self, ArrayPattern, { array::print_array(p, &Array::ArrayPattern(self)) })
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Format<'a> for AssignmentPattern<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
array![p, format!(p, self.left), ss!(" = "), format!(p, self.right)]
|
||||
wrap!(p, self, AssignmentPattern, {
|
||||
array![p, format!(p, self.left), ss!(" = "), format!(p, self.right)]
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
use oxc_ast::ast::{ObjectAssignmentTarget, ObjectExpression, ObjectPattern};
|
||||
use oxc_ast::{
|
||||
ast::{ObjectAssignmentTarget, ObjectExpression, ObjectPattern},
|
||||
AstKind,
|
||||
};
|
||||
use oxc_span::Span;
|
||||
|
||||
use crate::{
|
||||
|
|
@ -66,6 +69,8 @@ pub(super) fn print_object_properties<'a>(
|
|||
let left_brace = ss!("{");
|
||||
let right_brace = ss!("}");
|
||||
|
||||
let should_break = false;
|
||||
|
||||
let content = if object.is_empty() {
|
||||
group![p, left_brace, softline!(), right_brace]
|
||||
} else {
|
||||
|
|
@ -106,7 +111,15 @@ pub(super) fn print_object_properties<'a>(
|
|||
parts.push(if p.options.bracket_spacing { line!() } else { softline!() });
|
||||
parts.push(ss!("}"));
|
||||
|
||||
if object.is_object_pattern() {
|
||||
let parent_kind = p.parent_kind();
|
||||
if (object.is_object_pattern() && should_hug_the_only_parameter(parent_kind))
|
||||
|| (!should_break
|
||||
&& object.is_object_pattern()
|
||||
&& matches!(
|
||||
parent_kind,
|
||||
AstKind::AssignmentExpression(_) | AstKind::VariableDeclarator(_)
|
||||
))
|
||||
{
|
||||
Doc::Array(parts)
|
||||
} else {
|
||||
let should_break =
|
||||
|
|
@ -117,3 +130,12 @@ pub(super) fn print_object_properties<'a>(
|
|||
|
||||
content
|
||||
}
|
||||
|
||||
fn should_hug_the_only_parameter(kind: AstKind<'_>) -> bool {
|
||||
match kind {
|
||||
AstKind::FormalParameters(params) => {
|
||||
super::function_parameters::should_hug_the_only_function_parameter(params)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
Compatibility: 208/561 (37.08%)
|
||||
Compatibility: 209/561 (37.25%)
|
||||
|
||||
# Failed
|
||||
|
||||
|
|
@ -289,7 +289,6 @@ Compatibility: 208/561 (37.08%)
|
|||
* last-argument-expansion/empty-lines.js
|
||||
* last-argument-expansion/empty-object.js
|
||||
* last-argument-expansion/function-body-in-mode-break.js
|
||||
* last-argument-expansion/function-expression.js
|
||||
* last-argument-expansion/issue-10708.js
|
||||
* last-argument-expansion/issue-7518.js
|
||||
* last-argument-expansion/jsx.js
|
||||
|
|
|
|||
Loading…
Reference in a new issue