diff --git a/src/generation/generate.rs b/src/generation/generate.rs index 95a60227..0b029429 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -1986,6 +1986,13 @@ fn gen_arrow_func_expr<'a>(node: &'a ArrowExpr<'a>, context: &mut Context<'a>) - } else if should_not_newline_after_arrow(&node.body, context) { items.push_space(); } else { + // a call that hugs its last argument moves to the next line when it doesn't fit on the arrow's line, + // so it must be allowed to stay there when formatting again + let short_header_separator = if is_call_hugging_last_arg(&node.body, context) { + Signal::SpaceOrNewLine.into_print_items(context.arena) + } else { + " ".into_print_items(context.arena) + }; items.extend(gen_line_numbers_region_start(context.arena, &[end_body_ln])); items.push_condition(conditions::if_above_width_or( context.config.indent_width, @@ -1998,7 +2005,7 @@ fn gen_arrow_func_expr<'a>(node: &'a ArrowExpr<'a>, context: &mut Context<'a>) - Signal::SpaceOrNewLine.into_print_items(context.arena), ) .into_print_items(context.arena), - " ".into_print_items(context.arena), + short_header_separator, )); } @@ -2079,6 +2086,21 @@ fn gen_arrow_func_expr<'a>(node: &'a ArrowExpr<'a>, context: &mut Context<'a>) - /// Gets if the body is a call on the same line as the arrow with its last argument starting on that /// line and continuing on the next ones (ex. `a => b({\n c,\n})`), which keeps its line breaks. fn is_hugging_call_body<'a>(body: &ArrowFunctionBody<'a>, context: &Context<'a>) -> bool { + let ArrowFunctionBody::Expr(expr) = body else { + return false; + }; + let program = context.program; + let start_line = expr.start_line_fast(program); + let is_on_arrow_line = context + .token_finder + .get_previous_token(expr) + .is_some_and(|arrow_token| arrow_token.end_line_fast(program) == start_line); + is_on_arrow_line && is_call_hugging_last_arg(body, context) + } + + /// Gets if the body is a call with its last argument starting on the call's first line and continuing + /// on the next ones (ex. `b({\n c,\n})`). + fn is_call_hugging_last_arg<'a>(body: &ArrowFunctionBody<'a>, context: &Context<'a>) -> bool { let ArrowFunctionBody::Expr(expr) = body else { return false; }; @@ -2095,12 +2117,7 @@ fn gen_arrow_func_expr<'a>(node: &'a ArrowExpr<'a>, context: &mut Context<'a>) - }; let program = context.program; let start_line = expr.start_line_fast(program); - let is_on_arrow_line = context - .token_finder - .get_previous_token(expr) - .is_some_and(|arrow_token| arrow_token.end_line_fast(program) == start_line); - is_on_arrow_line - && last_arg.start_line_fast(program) == start_line + last_arg.start_line_fast(program) == start_line && last_arg.end_line_fast(program) > start_line && allows_inline_multi_line((*last_arg).into(), context, args.len() > 1) } diff --git a/tests/specs/issues/issue0107.txt b/tests/specs/issues/issue0107.txt index 05166b67..6c69b12f 100644 --- a/tests/specs/issues/issue0107.txt +++ b/tests/specs/issues/issue0107.txt @@ -101,3 +101,59 @@ foo( a: foo, }), ); + +== should format a test call body after multi-line arrow parameters the same way when formatted again == +values.forEach(( + value, +) => it("formats the value when this line is longer than the default line width of one hundred and twenty characters", () => { + test(value); +})); +values.forEach(( + value, +) => + it("formats the value when this line is longer than the default line width of one hundred and twenty characters", () => { + test(value); + }) +); + +[expect] +values.forEach(( + value, +) => + it("formats the value when this line is longer than the default line width of one hundred and twenty characters", () => { + test(value); + }) +); +values.forEach(( + value, +) => + it("formats the value when this line is longer than the default line width of one hundred and twenty characters", () => { + test(value); + }) +); + +== should keep a call that fits after multi-line arrow parameters on the arrow's line == +values.forEach(( + value, +) => foo({ + a: 1, +})); +values.forEach(( + value, +) => + foo({ + a: 1, + }) +); + +[expect] +values.forEach(( + value, +) => foo({ + a: 1, +})); +values.forEach(( + value, +) => foo({ + a: 1, +})); diff --git a/tests/specs/issues/issue0107_02.txt b/tests/specs/issues/issue0107_02.txt new file mode 100644 index 00000000..a6c15552 --- /dev/null +++ b/tests/specs/issues/issue0107_02.txt @@ -0,0 +1,16 @@ +~~ indentWidth: 2 ~~ +== should move a test call body after multi-line arrow parameters to the next line when it doesn't fit == +values.forEach(( + value, +) => it("formats the value when this line is longer than the default line width of one hundred and twenty characters", () => { + test(value); +})); + +[expect] +values.forEach(( + value, +) => + it("formats the value when this line is longer than the default line width of one hundred and twenty characters", () => { + test(value); + }) +);