]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Support AllowShortFunctionsOnASingleLine.
authorDaniel Jasper <djasper@google.com>
Tue, 30 Sep 2014 17:57:06 +0000 (17:57 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 30 Sep 2014 17:57:06 +0000 (17:57 +0000)
Specifically, this also counts for stuff like (with style "inline"):
  var x = function() {
    return 1;
  };

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@218689 91177308-0d34-0410-b5e6-96231b3b80d8

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

index e70bad9f3be2870002fbd2baa74698e819034d35..d07f68d022600872ad9f4150005d3e1404052cf3 100644 (file)
@@ -414,6 +414,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
     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;
index 68e16268dff75ce2b5abec6f5909eea23f2fdec1..43721ef95c0e4a9173a579c5faa15f5a4977808e 100644 (file)
@@ -1734,6 +1734,13 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
              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;
index 09d5f1feb2f2ced515d7a7f9bc748a42e7a95966..bc9bbad60173d449e2174d38181556ed68fd40f0 100644 (file)
@@ -172,7 +172,9 @@ TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
 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, MultipleFunctionLiterals) {
 }
 
 TEST_F(FormatTestJS, ReturnStatements) {
-  verifyFormat("function() { return [hello, world]; }");
+  verifyFormat("function() {\n"
+               "  return [hello, world];\n"
+               "}");
 }
 
 TEST_F(FormatTestJS, ClosureStyleComments) {