r218689 - clang-format: [JS] Support AllowShortFunctionsOnASingleLine.

Daniel Jasper djasper at google.com
Tue Sep 30 10:57:06 PDT 2014


Author: djasper
Date: Tue Sep 30 12:57:06 2014
New Revision: 218689

URL: http://llvm.org/viewvc/llvm-project?rev=218689&view=rev
Log:
clang-format: [JS] Support AllowShortFunctionsOnASingleLine.

Specifically, this also counts for stuff like (with style "inline"):
  var x = function() {
    return 1;
  };

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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=218689&r1=218688&r2=218689&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Sep 30 12:57:06 2014
@@ -414,6 +414,7 @@ FormatStyle getGoogleStyle(FormatStyle::
     GoogleStyle.BreakBeforeTernaryOperators = false;
     GoogleStyle.MaxEmptyLinesToKeep = 3;
     GoogleStyle.SpacesInContainerLiterals = false;
+    GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
   } else if (Language == FormatStyle::LK_Proto) {
     GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
     GoogleStyle.SpacesInContainerLiterals = false;

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=218689&r1=218688&r2=218689&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue Sep 30 12:57:06 2014
@@ -1734,6 +1734,13 @@ bool TokenAnnotator::mustBreakBefore(con
              Style.Language == FormatStyle::LK_Proto) {
     // Don't enums onto single lines in protocol buffers.
     return true;
+  } else if (Style.Language == FormatStyle::LK_JavaScript &&
+             Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
+             !Left.Children.empty()) {
+    // Support AllowShortFunctionsOnASingleLine for JavaScript.
+    return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
+           (Left.NestingLevel == 0 && Line.Level == 0 &&
+            Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline);
   } else if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
     return Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
            Style.BreakBeforeBraces == FormatStyle::BS_GNU;

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=218689&r1=218688&r2=218689&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Tue Sep 30 12:57:06 2014
@@ -172,7 +172,9 @@ TEST_F(FormatTestJS, FormatsFreestanding
 TEST_F(FormatTestJS, FunctionLiterals) {
   verifyFormat("doFoo(function() {});");
   verifyFormat("doFoo(function() { return 1; });");
-  verifyFormat("var func = function() { return 1; };");
+  verifyFormat("var func = function() {\n"
+               "  return 1;\n"
+               "};");
   verifyFormat("return {\n"
                "  body: {\n"
                "    setAttribute: function(key, val) { this[key] = val; },\n"
@@ -180,7 +182,14 @@ TEST_F(FormatTestJS, FunctionLiterals) {
                "    style: {direction: ''}\n"
                "  }\n"
                "};");
-  EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };",
+  // FIXME: The formatting here probably isn't ideal.
+  EXPECT_EQ("abc = xyz ?\n"
+            "          function() {\n"
+            "            return 1;\n"
+            "          } :\n"
+            "          function() {\n"
+            "  return -1;\n"
+            "};",
             format("abc=xyz?function(){return 1;}:function(){return -1;};"));
 
   verifyFormat("var closure = goog.bind(\n"
@@ -218,6 +227,57 @@ TEST_F(FormatTestJS, FunctionLiterals) {
                "};");
 }
 
+TEST_F(FormatTestJS, InliningFunctionLiterals) {
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
+  Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
+  verifyFormat("var func = function() {\n"
+               "  return 1;\n"
+               "};",
+               Style);
+  verifyFormat("var func = doSomething(function() { return 1; });", Style);
+  verifyFormat("var outer = function() {\n"
+               "  var inner = function() { return 1; }\n"
+               "};",
+               Style);
+  verifyFormat("function outer1(a, b) {\n"
+               "  function inner1(a, b) { return a; }\n"
+               "}",
+               Style);
+
+  Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
+  verifyFormat("var func = function() { return 1; };", Style);
+  verifyFormat("var func = doSomething(function() { return 1; });", Style);
+  verifyFormat(
+      "var outer = function() { var inner = function() { return 1; } };",
+      Style);
+  verifyFormat("function outer1(a, b) {\n"
+               "  function inner1(a, b) { return a; }\n"
+               "}",
+               Style);
+
+  Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
+  verifyFormat("var func = function() {\n"
+               "  return 1;\n"
+               "};",
+               Style);
+  verifyFormat("var func = doSomething(function() {\n"
+               "  return 1;\n"
+               "});",
+               Style);
+  verifyFormat("var outer = function() {\n"
+               "  var inner = function() {\n"
+               "    return 1;\n"
+               "  }\n"
+               "};",
+               Style);
+  verifyFormat("function outer1(a, b) {\n"
+               "  function inner1(a, b) {\n"
+               "    return a;\n"
+               "  }\n"
+               "}",
+               Style);
+}
+
 TEST_F(FormatTestJS, MultipleFunctionLiterals) {
   verifyFormat("promise.then(\n"
                "    function success() {\n"
@@ -265,7 +325,9 @@ TEST_F(FormatTestJS, MultipleFunctionLit
 }
 
 TEST_F(FormatTestJS, ReturnStatements) {
-  verifyFormat("function() { return [hello, world]; }");
+  verifyFormat("function() {\n"
+               "  return [hello, world];\n"
+               "}");
 }
 
 TEST_F(FormatTestJS, ClosureStyleComments) {





More information about the cfe-commits mailing list