diff --git a/crates/oxc_prettier/src/format/class.rs b/crates/oxc_prettier/src/format/class.rs index 30baa1545..50cf93866 100644 --- a/crates/oxc_prettier/src/format/class.rs +++ b/crates/oxc_prettier/src/format/class.rs @@ -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) => { diff --git a/crates/oxc_prettier/src/format/function.rs b/crates/oxc_prettier/src/format/function.rs index a1f4bd480..f814a5685 100644 --- a/crates/oxc_prettier/src/format/function.rs +++ b/crates/oxc_prettier/src/format/function.rs @@ -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) } diff --git a/crates/oxc_prettier/src/format/function_parameters.rs b/crates/oxc_prettier/src/format/function_parameters.rs index a348aa15e..4f40a3bc1 100644 --- a/crates/oxc_prettier/src/format/function_parameters.rs +++ b/crates/oxc_prettier/src/format/function_parameters.rs @@ -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>, diff --git a/crates/oxc_prettier/src/format/mod.rs b/crates/oxc_prettier/src/format/mod.rs index e58130a7e..26e9ce749 100644 --- a/crates/oxc_prettier/src/format/mod.rs +++ b/crates/oxc_prettier/src/format/mod.rs @@ -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)] + }) } } diff --git a/crates/oxc_prettier/src/format/object.rs b/crates/oxc_prettier/src/format/object.rs index 84bbd5246..46da59e07 100644 --- a/crates/oxc_prettier/src/format/object.rs +++ b/crates/oxc_prettier/src/format/object.rs @@ -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, + } +} diff --git a/tasks/prettier_conformance/prettier.snap.md b/tasks/prettier_conformance/prettier.snap.md index d62bdd2b0..1d9b87bd2 100644 --- a/tasks/prettier_conformance/prettier.snap.md +++ b/tasks/prettier_conformance/prettier.snap.md @@ -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