Skip to content

Commit

Permalink
feat(prettier): finish should_hug_the_only_function_parameter (#1626)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryven authored Dec 5, 2023
1 parent 795db7c commit 2aa5f7d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
66 changes: 60 additions & 6 deletions crates/oxc_prettier/src/format/function_parameters.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,60 @@
use oxc_ast::{ast::*, AstKind};

use crate::{
comments::CommentFlags,
doc::{Doc, DocBuilder},
ss, Format, Prettier,
group, hardline, line, ss, Format, Prettier,
};

pub(super) fn should_hug_the_only_function_parameter(params: &FormalParameters<'_>) -> bool {
pub(super) fn should_hug_the_only_function_parameter(
p: &mut Prettier<'_>,
params: &FormalParameters<'_>,
) -> bool {
if params.parameters_count() != 1 {
return false;
}
true
let Some(parameter) = params.items.first() else { return false };

let all_comment_flags = CommentFlags::Trailing
| CommentFlags::Leading
| CommentFlags::Dangling
| CommentFlags::Block
| CommentFlags::Line
| CommentFlags::PrettierIgnore
| CommentFlags::First
| CommentFlags::Last;
if p.has_comment(parameter.span, all_comment_flags) {
return false;
}

match &parameter.pattern.kind {
BindingPatternKind::ObjectPattern(_) | BindingPatternKind::ArrayPattern(_) => true,
BindingPatternKind::BindingIdentifier(_) => {
let Some(ts_type_annotation) = &parameter.pattern.type_annotation else { return false };
matches!(
ts_type_annotation.type_annotation,
TSType::TSTypeLiteral(_) | TSType::TSMappedType(_)
)
}
BindingPatternKind::AssignmentPattern(assignment_pattern) => {
let left = &assignment_pattern.left.kind;
if matches!(left, BindingPatternKind::ObjectPattern(_)) {
return true;
}

if !matches!(left, BindingPatternKind::ArrayPattern(_)) {
return false;
}

let right = &assignment_pattern.right;
match right {
Expression::Identifier(_) => true,
Expression::ObjectExpression(obj_expr) => obj_expr.properties.len() == 0,
Expression::ArrayExpression(arr_expr) => arr_expr.elements.len() == 0,
_ => false,
}
}
}
}

pub(super) fn print_function_parameters<'a>(
Expand All @@ -26,8 +71,17 @@ pub(super) fn print_function_parameters<'a>(

for (i, param) in params.items.iter().enumerate() {
parts.push(param.format(p));
if i < params.items.len() - 1 {
parts.push(ss!(", "));
if i == params.items.len() - 1 {
break;
}
parts.push(ss!(","));
if should_hug_the_only_function_parameter(p, params) {
parts.push(ss!(" "));
} else if p.is_next_line_empty(param.span) {
parts.extend(hardline!());
parts.extend(hardline!());
} else {
parts.push(line!());
}
}

Expand All @@ -42,5 +96,5 @@ pub(super) fn print_function_parameters<'a>(
parts.push(ss!(")"));
}

Doc::Array(parts)
group!(p, Doc::Array(parts))
}
6 changes: 3 additions & 3 deletions crates/oxc_prettier/src/format/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub(super) fn print_object_properties<'a>(
parts.push(ss!("}"));

let parent_kind = p.parent_kind();
if (object.is_object_pattern() && should_hug_the_only_parameter(parent_kind))
if (object.is_object_pattern() && should_hug_the_only_parameter(p, parent_kind))
|| (!should_break
&& object.is_object_pattern()
&& matches!(
Expand All @@ -131,10 +131,10 @@ pub(super) fn print_object_properties<'a>(
content
}

fn should_hug_the_only_parameter(kind: AstKind<'_>) -> bool {
fn should_hug_the_only_parameter(p: &mut Prettier<'_>, kind: AstKind<'_>) -> bool {
match kind {
AstKind::FormalParameters(params) => {
super::function_parameters::should_hug_the_only_function_parameter(params)
super::function_parameters::should_hug_the_only_function_parameter(p, params)
}
_ => false,
}
Expand Down

0 comments on commit 2aa5f7d

Please sign in to comment.