aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Format/FormatTestJS.cpp
diff options
context:
space:
mode:
authorMartin Probst <martin@probst.io>2020-01-24 16:13:51 +0100
committerMartin Probst <martin@probst.io>2020-01-29 13:23:54 +0100
commita324fcf1ae62d065b957e66a9d2f5c18b6259d27 (patch)
treee3b056cf0c22106a082cb52d0bdd4a2022e520e8 /clang/unittests/Format/FormatTestJS.cpp
parent3cf80822a9064e9ae137f486bc321671842f3fd0 (diff)
downloadllvm-a324fcf1ae62d065b957e66a9d2f5c18b6259d27.zip
llvm-a324fcf1ae62d065b957e66a9d2f5c18b6259d27.tar.gz
llvm-a324fcf1ae62d065b957e66a9d2f5c18b6259d27.tar.bz2
clang-format: insert trailing commas into containers.
Summary: This change adds an option to insert trailing commas into container literals. For example, in JavaScript: const x = [ a, b, ^~~~~ inserted if missing. ] This is implemented as a seperate post-processing pass after formatting (because formatting might change whether the container literal does or does not wrap). This keeps the code relatively simple and orthogonal, though it has the notable drawback that the newly inserted comma is not taken into account for formatting decisions (e.g. it might exceed the 80 char limit). To avoid exceeding the ColumnLimit, a comma is only inserted if it fits into the limit. Trailing comma insertion conceptually conflicts with argument bin-packing: inserting a comma disables bin-packing, so we cannot do both. clang-format rejects FormatStyle configurations that do both with this change. Reviewers: krasimir, MyDeveloperDay Subscribers: cfe-commits Tags: #clang
Diffstat (limited to 'clang/unittests/Format/FormatTestJS.cpp')
-rw-r--r--clang/unittests/Format/FormatTestJS.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 3c104b7..6efb866 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -878,6 +878,45 @@ TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) {
"]);");
}
+TEST_F(FormatTestJS, TrailingCommaInsertion) {
+ FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
+ Style.InsertTrailingCommas = FormatStyle::TCS_Wrapped;
+ // Insert comma in wrapped array.
+ verifyFormat("const x = [\n"
+ " 1, //\n"
+ " 2,\n"
+ "];",
+ "const x = [\n"
+ " 1, //\n"
+ " 2];",
+ Style);
+ // Insert comma in newly wrapped array.
+ Style.ColumnLimit = 30;
+ verifyFormat("const x = [\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ "];",
+ "const x = [aaaaaaaaaaaaaaaaaaaaaaaaa];", Style);
+ // Do not insert trailing commas if they'd exceed the colum limit
+ verifyFormat("const x = [\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ "];",
+ "const x = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa];", Style);
+ // Object literals.
+ verifyFormat("const x = {\n"
+ " a: aaaaaaaaaaaaaaaaa,\n"
+ "};",
+ "const x = {a: aaaaaaaaaaaaaaaaa};", Style);
+ verifyFormat("const x = {\n"
+ " a: aaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ "};",
+ "const x = {a: aaaaaaaaaaaaaaaaaaaaaaaaa};", Style);
+ // Object literal types.
+ verifyFormat("let x: {\n"
+ " a: aaaaaaaaaaaaaaaaaaaaa,\n"
+ "};",
+ "let x: {a: aaaaaaaaaaaaaaaaaaaaa};", Style);
+}
+
TEST_F(FormatTestJS, FunctionLiterals) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;