r222893 - clang-format: [JS] Contract fewer functions to a single line.

Daniel Jasper djasper at google.com
Thu Nov 27 07:37:42 PST 2014


Author: djasper
Date: Thu Nov 27 09:37:42 2014
New Revision: 222893

URL: http://llvm.org/viewvc/llvm-project?rev=222893&view=rev
Log:
clang-format: [JS] Contract fewer functions to a single line.

Before:
  var someVariable =
      function(x) { return x.zIsTooLongForOneLineWithTheDeclarationLine(); };

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

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTestJS.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=222893&r1=222892&r2=222893&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Nov 27 09:37:42 2014
@@ -1480,6 +1480,9 @@ unsigned TokenAnnotator::splitPenalty(co
       return 2;
     if (Left.is(tok::comma) && Left.NestingLevel == 0)
       return 3;
+  } else if (Style.Language == FormatStyle::LK_JavaScript) {
+    if (Right.is(Keywords.kw_function))
+      return 100;
   }
 
   if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next &&
@@ -1548,8 +1551,6 @@ unsigned TokenAnnotator::splitPenalty(co
     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;

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=222893&r1=222892&r2=222893&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Thu Nov 27 09:37:42 2014
@@ -232,6 +232,11 @@ TEST_F(FormatTestJS, FunctionLiterals) {
                "    };\n"
                "  }\n"
                "};");
+  verifyFormat("{\n"
+               "  var someVariable = function(x) {\n"
+               "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
+               "  };\n"
+               "}");
 
   verifyFormat("var x = {a: function() { return 1; }};",
                getGoogleJSStyleWithColumns(38));





More information about the cfe-commits mailing list