clang-format: [JS] Try not to break in container literals.
authorDaniel Jasper <djasper@google.com>
Thu, 27 Nov 2014 15:24:48 +0000 (15:24 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 27 Nov 2014 15:24:48 +0000 (15:24 +0000)
Before:
  var obj = {
    fooooooooo:
        function(x) { return x.zIsTooLongForOneLineWithTheDeclarationLine(); }
  };

After:
  var obj = {
    fooooooooo: function(x) {
      return x.zIsTooLongForOneLineWithTheDeclarationLine();
    }
  };

llvm-svn: 222892

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

index d6c823e..718a019 100644 (file)
@@ -1548,6 +1548,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
     return 0;
   if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
     return Line.MightBeFunctionDecl ? 50 : 500;
+  if (Left.is(tok::colon) && Left.is(TT_DictLiteral))
+    return 100;
 
   if (Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
     return 100;
index 6c8fd29..d678564 100644 (file)
@@ -131,6 +131,11 @@ TEST_F(FormatTestJS, ContainerLiterals) {
                "      //\n"
                "      a\n"
                "};");
+  verifyFormat("var obj = {\n"
+               "  fooooooooo: function(x) {\n"
+               "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
+               "  }\n"
+               "};");
 }
 
 TEST_F(FormatTestJS, SpacesInContainerLiterals) {