Skip to content
Open
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
10 changes: 9 additions & 1 deletion src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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 {
Expand Down
73 changes: 73 additions & 0 deletions tests/specs/general/Arguments_TrailingCommas_Always.txt
Original file line number Diff line number Diff line change
@@ -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(),
);
34 changes: 34 additions & 0 deletions tests/specs/issues/issue0696.txt
Original file line number Diff line number Diff line change
@@ -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"
);