clang-format: [JS] Don't put top-level dict literals on a single line.
authorDaniel Jasper <djasper@google.com>
Thu, 4 Dec 2014 16:07:17 +0000 (16:07 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 4 Dec 2014 16:07:17 +0000 (16:07 +0000)
These are often used for enums which apparently are easier to read if
formatted with one element per line.

llvm-svn: 223367

clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTestJS.cpp

index 9f692437cad4c67f4532d92e7e4117bc84adf28a..ede60eede3c24eaed2523cf573f78a758da64113 100644 (file)
@@ -1891,6 +1891,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
     if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
         Left.Previous->is(tok::char_constant))
       return true;
+    if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) &&
+        Left.NestingLevel == 0)
+      return true;
   } else if (Style.Language == FormatStyle::LK_Java) {
     if (Left.is(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
         Line.Last->is(tok::l_brace))
index 0d29c178f8b0e026a3b0f724efdc62cae7618973..23654a88483c82e5212a73cd746f1005cab50abd 100644 (file)
@@ -94,7 +94,10 @@ TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
 
 TEST_F(FormatTestJS, ES6DestructuringAssignment) {
   verifyFormat("var [a, b, c] = [1, 2, 3];");
-  verifyFormat("var {a, b} = {a: 1, b: 2};");
+  verifyFormat("var {a, b} = {\n"
+               "  a: 1,\n"
+               "  b: 2\n"
+               "};");
 }
 
 TEST_F(FormatTestJS, ContainerLiterals) {
@@ -140,16 +143,16 @@ TEST_F(FormatTestJS, ContainerLiterals) {
 
 TEST_F(FormatTestJS, SpacesInContainerLiterals) {
   verifyFormat("var arr = [1, 2, 3];");
-  verifyFormat("var obj = {a: 1, b: 2, c: 3};");
+  verifyFormat("f({a: 1, b: 2, c: 3});");
 
   verifyFormat("var object_literal_with_long_name = {\n"
                "  a: 'aaaaaaaaaaaaaaaaaa',\n"
                "  b: 'bbbbbbbbbbbbbbbbbb'\n"
                "};");
 
-  verifyFormat("var obj = {a: 1, b: 2, c: 3};",
+  verifyFormat("f({a: 1, b: 2, c: 3});",
                getChromiumStyle(FormatStyle::LK_JavaScript));
-  verifyFormat("someVariable = {'a': [{}]};");
+  verifyFormat("f({'a': [{}]});");
 }
 
 TEST_F(FormatTestJS, SingleQuoteStrings) {
@@ -238,12 +241,12 @@ TEST_F(FormatTestJS, FunctionLiterals) {
                "  };\n"
                "}");
 
-  verifyFormat("var x = {a: function() { return 1; }};",
-               getGoogleJSStyleWithColumns(38));
-  verifyFormat("var x = {\n"
+  verifyFormat("f({a: function() { return 1; }});",
+               getGoogleJSStyleWithColumns(33));
+  verifyFormat("f({\n"
                "  a: function() { return 1; }\n"
-               "};",
-               getGoogleJSStyleWithColumns(37));
+               "});",
+               getGoogleJSStyleWithColumns(32));
 
   verifyFormat("return {\n"
                "  a: function SomeFunction() {\n"