r218595 - clang-format: [JS] Improve formatting of function literals in chains

Daniel Jasper djasper at google.com
Mon Sep 29 00:54:54 PDT 2014


Author: djasper
Date: Mon Sep 29 02:54:54 2014
New Revision: 218595

URL: http://llvm.org/viewvc/llvm-project?rev=218595&view=rev
Log:
clang-format: [JS] Improve formatting of function literals in chains

Before:
  getSomeLongPromise(.....)
      .then(
           function(value) {
             body();
             body();
           })
      .thenCatch(function(error) {
    body();
    body();
  });

After:
  getSomeLongPromise(.....)
      .then(function(value) {
        body();
        body();
      })
      .thenCatch(function(error) {
        body();
        body();
      });

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

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=218595&r1=218594&r2=218595&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Mon Sep 29 02:54:54 2014
@@ -656,7 +656,10 @@ unsigned ContinuationIndenter::moveState
     }
     if (Current.TokenText == "function")
       State.Stack.back().JSFunctionInlined =
-          !Newline && Previous && Previous->Type != TT_DictLiteral;
+          !Newline && Previous && Previous->Type != TT_DictLiteral &&
+          // If the unnamed function is the only parameter to another function,
+          // we can likely inline it and come up with a good format.
+          (Previous->isNot(tok::l_paren) || Previous->ParameterCount > 1);
   }
 
   moveStatePastFakeLParens(State, Newline);

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=218595&r1=218594&r2=218595&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Mon Sep 29 02:54:54 2014
@@ -249,6 +249,19 @@ TEST_F(FormatTestJS, MultipleFunctionLit
                "               doFoo();\n"
                "               doBaz();\n"
                "             });\n");
+
+  verifyFormat("getSomeLongPromise()\n"
+               "    .then(function(value) { body(); })\n"
+               "    .thenCatch(function(error) { body(); });");
+  verifyFormat("getSomeLongPromise()\n"
+               "    .then(function(value) {\n"
+               "      body();\n"
+               "      body();\n"
+               "    })\n"
+               "    .thenCatch(function(error) {\n"
+               "      body();\n"
+               "      body();\n"
+               "    });");
 }
 
 TEST_F(FormatTestJS, ReturnStatements) {





More information about the cfe-commits mailing list