diff --git a/src/generation/generate.rs b/src/generation/generate.rs index b12063546..b3c2adc7d 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -7602,6 +7602,14 @@ where items.push_signal(Signal::SpaceIfNotTrailing); } items.push_condition(conditions::indent_if_start_of_line(generated_node)); + let trailing_break_items = { + let mut break_items = PrintItems::new(); + if matches!(trailing_commas, TrailingCommas::Always) { + break_items.push_sc(sc!(",")); + } + break_items.push_signal(Signal::NewLine); + break_items + }; items.push_condition(if_true_or( "isDifferentLineAndStartLineIndentation", Rc::new(move |context| { @@ -7611,7 +7619,7 @@ where let is_different_start_line_indentation = start_lsil != context.writer_info.line_start_indent_level; Some(is_different_line && is_different_start_line_indentation) }), - Signal::NewLine.into(), + trailing_break_items, if space_around { Signal::SpaceIfNotTrailing.into() } else { PrintItems::new() }, )); } else { diff --git a/tests/specs/general/Arguments_TrailingCommas_Always.txt b/tests/specs/general/Arguments_TrailingCommas_Always.txt new file mode 100644 index 000000000..01269745b --- /dev/null +++ b/tests/specs/general/Arguments_TrailingCommas_Always.txt @@ -0,0 +1,73 @@ +~~ lineWidth: 60, trailingCommas: always ~~ +== should add trailing comma to single-arg call == +foo(x); + +[expect] +foo(x,); + +== should add trailing comma to multi-arg call == +foo(a, b, c); + +[expect] +foo(a, b, c,); + +== should add trailing comma in multi-line call == +foo( + aLongerName, + anotherLongerName +); + +[expect] +foo( + aLongerName, + anotherLongerName, +); + +== should add trailing comma after arrow expression body when call wraps (issue #696) == +[1, 2, 3].map(value => + value == "a quite long paragraph with many words") + +[expect] +[1, 2, 3,].map(value => + value == "a quite long paragraph with many words", +); + +== should add trailing comma after block-bodied arrow == +foo( + (v) => { + return v; + } +) + +[expect] +foo( + (v,) => { + return v; + }, +); + +== should not add trailing comma after rest argument in call when never-trailing applies == +foo(...args); + +[expect] +foo(...args,); + +== should not add trailing comma in dynamic import == +import( + "myreallylongdynamicallyloadedmodulename" +); + +[expect] +import( + "myreallylongdynamicallyloadedmodulename" +); + +== should add trailing comma when last arg is arrow whose body wraps == +a.b().c({ d: 1 }, () => + doSomethingThatDoesNotFitOnTheSameLine()) + +[expect] +a.b().c( + { d: 1, }, + () => doSomethingThatDoesNotFitOnTheSameLine(), +); diff --git a/tests/specs/issues/issue0696.txt b/tests/specs/issues/issue0696.txt new file mode 100644 index 000000000..b26b43db9 --- /dev/null +++ b/tests/specs/issues/issue0696.txt @@ -0,0 +1,34 @@ +~~ lineWidth: 60, trailingCommas: always ~~ +== should add trailing comma after arrow expression body when call breaks across lines (issue #696) == +const test = ["a","b","c"].map(value => + value == "a quite long paragraph with many words") + +[expect] +const test = ["a", "b", "c",].map(value => + value == "a quite long paragraph with many words", +); + +== should add trailing comma after nested expression body that wraps == +longCallExpression(value => + someVeryLongCondition && anotherCondition) + +[expect] +longCallExpression(value => + someVeryLongCondition && anotherCondition, +); + +== should not add trailing comma when arrow body stays on the same line == +foo(v => v); + +[expect] +foo(v => v); + +== should keep dynamic import without trailing comma == +import( + "myreallylongdynamicallyloadedmodulename" +); + +[expect] +import( + "myreallylongdynamicallyloadedmodulename" +);