]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Format embedded function literals more efficently.
authorDaniel Jasper <djasper@google.com>
Fri, 5 Sep 2014 09:27:38 +0000 (09:27 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 5 Sep 2014 09:27:38 +0000 (09:27 +0000)
Before:
  return {
    a: a,
    link:
        function() {
          f();  //
        },
    link:
        function() {
          f();  //
        }
  };

After:
  return {
    a: a,
    link: function() {
      f();  //
    },
    link: function() {
      f();  //
    }
  };

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

lib/Format/ContinuationIndenter.cpp
unittests/Format/FormatTestJS.cpp

index 828e7507626892fbce390b4d72cd2bfe280502a3..afd20b22e3aab1bbc54bf2b5e79c60eab01573ec 100644 (file)
@@ -643,7 +643,8 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
       State.Stack[State.Stack.size() - 2].JSFunctionInlined = false;
     }
     if (Current.TokenText == "function")
-      State.Stack.back().JSFunctionInlined = !Newline;
+      State.Stack.back().JSFunctionInlined =
+          !Newline && Previous && Previous->Type != TT_DictLiteral;
   }
 
   moveStatePastFakeLParens(State, Newline);
index bd931aaf0a3cb45bc13859a2a0744852b518fe4c..b161699d94351572f15196eeadb1e094c7f14a4e 100644 (file)
@@ -110,14 +110,12 @@ TEST_F(FormatTestJS, ContainerLiterals) {
                "};");
   verifyFormat("return {\n"
                "  a: a,\n"
-               "  link:\n"
-               "      function() {\n"
-               "        f();  //\n"
-               "      },\n"
-               "  link:\n"
-               "      function() {\n"
-               "        f();  //\n"
-               "      }\n"
+               "  link: function() {\n"
+               "    f();  //\n"
+               "  },\n"
+               "  link: function() {\n"
+               "    f();  //\n"
+               "  }\n"
                "};");
   verifyFormat("var stuff = {\n"
                "  // comment for update\n"