Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
));
}

Expand Down Expand Up @@ -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;
};
Expand All @@ -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)
}
Expand Down
56 changes: 56 additions & 0 deletions tests/specs/issues/issue0107.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}));
16 changes: 16 additions & 0 deletions tests/specs/issues/issue0107_02.txt
Original file line number Diff line number Diff line change
@@ -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);
})
);
Loading