From 7e99e52bbc14b020898321592ff2956cce22c84b Mon Sep 17 00:00:00 2001 From: Boshen Date: Sun, 11 Feb 2024 23:01:19 +0800 Subject: [PATCH] refactor(prettier): function parameters (#2392) --- crates/oxc_prettier/src/format/array.rs | 73 +++++++++++-------- crates/oxc_prettier/src/format/function.rs | 13 ++-- .../src/format/function_parameters.rs | 21 ++++-- 3 files changed, 65 insertions(+), 42 deletions(-) diff --git a/crates/oxc_prettier/src/format/array.rs b/crates/oxc_prettier/src/format/array.rs index 9e4329a0f..95afa6db9 100644 --- a/crates/oxc_prettier/src/format/array.rs +++ b/crates/oxc_prettier/src/format/array.rs @@ -6,7 +6,7 @@ use crate::{ array, comments::{CommentFlags, DanglingCommentsPrintOptions}, doc::{Doc, DocBuilder, Fill, Group}, - group, hardline, if_break, indent, line, softline, ss, Prettier, + group, hardline, if_break, line, softline, ss, Prettier, }; use super::Format; @@ -99,24 +99,36 @@ pub fn print_array<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) -> Doc<'a> { }; let mut parts = p.vec(); - let elements = if should_use_concise_formatting { - print_array_elements_concisely(p, array, trailing_comma_fn) - } else { - let trailing_comma = trailing_comma_fn(p); - array!(p, print_elements(p, array), trailing_comma) - }; - let parts_inner = if let Some(dangling_comments) = p.print_dangling_comments(array.span(), None) - { - indent!(p, softline!(), elements, dangling_comments) - } else { - indent!(p, softline!(), elements) - }; - parts.push(ss!("[")); - parts.push(parts_inner); - parts.push(softline!()); - parts.push(ss!("]")); - let should_break = should_break(array); - Doc::Group(Group::new(parts).with_break(should_break).with_id(id)) + + parts.push(Doc::Group( + Group::new({ + let mut group = p.vec(); + group.push(ss!("[")); + group.push({ + Doc::Indent({ + let mut indent_parts = p.vec(); + indent_parts.push(softline!()); + indent_parts.push(if should_use_concise_formatting { + print_array_elements_concisely(p, array, trailing_comma_fn) + } else { + let trailing_comma = trailing_comma_fn(p); + array!(p, print_array_elements(p, array), trailing_comma) + }); + if let Some(dangling_comments) = p.print_dangling_comments(array.span(), None) { + indent_parts.push(dangling_comments); + }; + indent_parts + }) + }); + group.push(softline!()); + group.push(ss!("]")); + group + }) + .with_break(should_break(array)) + .with_id(id), + )); + + Doc::Array(parts) } fn print_empty_array_elements<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) -> Doc<'a> { @@ -127,7 +139,7 @@ fn print_empty_array_elements<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) - ) } -fn print_elements<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) -> Doc<'a> { +fn print_array_elements<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) -> Doc<'a> { let mut parts = p.vec(); match array { Array::ArrayExpression(array) => { @@ -154,21 +166,20 @@ fn print_elements<'a>(p: &mut Prettier<'a>, array: &Array<'a, '_>) -> Doc<'a> { } } Array::ArrayPattern(array_pat) => { + let len = array_pat.elements.len(); + let has_rest = array_pat.rest.is_some(); for (i, element) in array_pat.elements.iter().enumerate() { - if i > 0 && i < array_pat.elements.len() { - parts.push(ss!(",")); - parts.push(line!()); - } - if let Some(binding_pat) = element { - parts.push(binding_pat.format(p)); + parts.push(group!(p, binding_pat.format(p))); + } + if i == len - 1 && !has_rest { + break; } - } - - if let Some(rest) = &array_pat.rest { parts.push(ss!(",")); parts.push(line!()); - parts.push(rest.format(p)); + } + if let Some(rest) = &array_pat.rest { + parts.push(group!(p, rest.format(p))); } } Array::ArrayAssignmentTarget(array_pat) => { @@ -235,7 +246,7 @@ where } _ => { // TODO: implement - array!(p, print_elements(p, array), trailing_comma_fn(p)); + array!(p, print_array_elements(p, array), trailing_comma_fn(p)); } } diff --git a/crates/oxc_prettier/src/format/function.rs b/crates/oxc_prettier/src/format/function.rs index 8edd5befa..fd26c6f41 100644 --- a/crates/oxc_prettier/src/format/function.rs +++ b/crates/oxc_prettier/src/format/function.rs @@ -33,13 +33,14 @@ pub(super) fn print_function<'a>( if let Some(id) = &func.id { parts.push(p.str(id.name.as_str())); } - let params_doc = if should_group_function_parameters(func) { - group!(p, func.params.format(p)) - } else { - func.params.format(p) - }; // Prettier has `returnTypeDoc` to group together, write this for keep same with prettier. - parts.push(group!(p, params_doc)); + parts.push(group!(p, { + if should_group_function_parameters(func) { + group!(p, func.params.format(p)) + } else { + func.params.format(p) + } + })); if let Some(body) = &func.body { parts.push(space!()); parts.push(body.format(p)); diff --git a/crates/oxc_prettier/src/format/function_parameters.rs b/crates/oxc_prettier/src/format/function_parameters.rs index 4bb6b4d33..ee5857e69 100644 --- a/crates/oxc_prettier/src/format/function_parameters.rs +++ b/crates/oxc_prettier/src/format/function_parameters.rs @@ -69,14 +69,18 @@ pub(super) fn print_function_parameters<'a>( parts.push(ss!("(")); } + let should_hug_the_only_function_parameter = should_hug_the_only_function_parameter(p, params); + let mut printed = p.vec(); + let len = params.items.len(); + let has_rest = params.rest.is_some(); for (i, param) in params.items.iter().enumerate() { printed.push(param.format(p)); - if i == params.items.len() - 1 && params.rest.is_none() { + if i == len - 1 && !has_rest { break; } printed.push(ss!(",")); - if should_hug_the_only_function_parameter(p, params) { + if should_hug_the_only_function_parameter { printed.push(space!()); } else if p.is_next_line_empty(param.span) { printed.extend(hardline!()); @@ -85,11 +89,18 @@ pub(super) fn print_function_parameters<'a>( printed.push(line!()); } } - if let Some(rest) = ¶ms.rest { printed.push(rest.format(p)); } + if should_hug_the_only_function_parameter { + let mut array = p.vec(); + array.push(ss!("(")); + array.extend(printed); + array.push(ss!(")")); + return Doc::Array(array); + } + let mut indented = p.vec(); indented.push(softline!()); indented.extend(printed); @@ -103,9 +114,9 @@ pub(super) fn print_function_parameters<'a>( } if p.args.expand_first_arg { - Doc::Group(Group::new(parts)) - } else { Doc::Array(parts) + } else { + Doc::Group(Group::new(parts)) } }