]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Making arrow function wrapping more consistent.
authorDaniel Jasper <djasper@google.com>
Mon, 1 Jun 2015 09:56:32 +0000 (09:56 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 1 Jun 2015 09:56:32 +0000 (09:56 +0000)
Before:
  someFunction(() =>
               {
                 doSomething();  // break
               })
      .doSomethingElse(  // break
          );

After:
  someFunction(() => {
    doSomething();  // break
  })
      .doSomethingElse(  // break
          );

This is still bad, but at least it is consistent with what we do for other
function literals. Added corresponding tests.

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

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

index 4e8f5af263d2f59a15beebbc06f78d811b1b560a..39b2793feef9b64d11a2aea6ab61ddf61fec9d70 100644 (file)
@@ -481,11 +481,9 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
   bool NestedBlockSpecialCase =
       Current.is(tok::r_brace) && State.Stack.size() > 1 &&
       State.Stack[State.Stack.size() - 2].NestedBlockInlined;
-  if (!NestedBlockSpecialCase) {
-    for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
+  if (!NestedBlockSpecialCase)
+    for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i)
       State.Stack[i].BreakBeforeParameter = true;
-    }
-  }
 
   if (PreviousNonComment &&
       !PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
@@ -689,11 +687,9 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
   //   }, a, b, c);
   if (Current.isNot(tok::comment) && Previous && Previous->is(tok::l_brace) &&
       State.Stack.size() > 1) {
-    if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline) {
-      for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
+    if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline)
+      for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i)
         State.Stack[i].NoLineBreak = true;
-      }
-    }
     State.Stack[State.Stack.size() - 2].NestedBlockInlined = false;
   }
   if (Previous && (Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) ||
index ad40d99ad82c2df3391e8d8ddf8a49d1ff28f403..501865e9ad880d19e2ee5c682b2f034b266d200c 100644 (file)
@@ -749,8 +749,8 @@ private:
     // recovered from an error (e.g. failure to find the matching >).
     if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro,
                                TT_FunctionLBrace, TT_ImplicitStringLiteral,
-                               TT_InlineASMBrace, TT_RegexLiteral,
-                               TT_TrailingReturnArrow))
+                               TT_InlineASMBrace, TT_JsFatArrow,
+                               TT_RegexLiteral, TT_TrailingReturnArrow))
       CurrentToken->Type = TT_Unknown;
     CurrentToken->Role.reset();
     CurrentToken->MatchingParen = nullptr;
index 29b8aa1ff13c7b59eca56ac3e09167c3a6becdbb..20c10b634bf4af013166b0dd2aba6f5d111f5384 100644 (file)
@@ -337,6 +337,14 @@ TEST_F(FormatTestJS, FunctionLiterals) {
                "               doSomething();\n"
                "               doSomething();\n"
                "             }, this));");
+
+  // FIXME: This is bad, we should be wrapping before "function() {".
+  verifyFormat("someFunction(function() {\n"
+               "  doSomething();  // break\n"
+               "})\n"
+               "    .doSomethingElse(\n"
+               "        // break\n"
+               "        );");
 }
 
 TEST_F(FormatTestJS, InliningFunctionLiterals) {
@@ -455,7 +463,14 @@ TEST_F(FormatTestJS, ArrowFunctions) {
                "  return a;\n"
                "};");
   verifyFormat("var x = (a) => a;");
-  verifyFormat("var x = (a) => a;");
+
+  // FIXME: This is bad, we should be wrapping before "() => {".
+  verifyFormat("someFunction(() => {\n"
+               "  doSomething();  // break\n"
+               "})\n"
+               "    .doSomethingElse(\n"
+               "        // break\n"
+               "        );");
 }
 
 TEST_F(FormatTestJS, ReturnStatements) {