Author: mprobst
Date: Mon May 29 02:50:52 2017
New Revision: 304135

URL: http://llvm.org/viewvc/llvm-project?rev=304135&view=rev
Log:
clang-format: [JS] fix indenting bound functions.

Summary:
The previous fix to force build style wrapping if the previous token is a 
closing parenthesis broke a peculiar pattern where users parenthesize the 
function declaration in a bind call:
    fn((function() { ... }).bind(this));

This restores the previous behaviour by reverting that change, but narrowing 
the special case for unindenting closing parentheses to those followed by 
semicolons and opening braces, i.e. immediate calls and function declarations.

Reviewers: djasper

Subscribers: cfe-commits, klimek

Differential Revision: https://reviews.llvm.org/D33640

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=304135&r1=304134&r2=304135&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Mon May 29 02:50:52 2017
@@ -215,7 +215,7 @@ bool ContinuationIndenter::mustBreak(con
       //     ...
       //   }.bind(...));
       // FIXME: We should find a more generic solution to this problem.
-      !(State.Column <= NewLineColumn && Previous.isNot(tok::r_paren) &&
+      !(State.Column <= NewLineColumn &&
         Style.Language == FormatStyle::LK_JavaScript))
     return true;
 
@@ -689,7 +689,18 @@ unsigned ContinuationIndenter::getNewLin
       return State.Stack[State.Stack.size() - 2].LastSpace;
     return State.FirstIndent;
   }
-  if (Current.is(tok::r_paren) && State.Stack.size() > 1)
+  // Indent a closing parenthesis at the previous level if followed by a semi 
or
+  // opening brace. This allows indentations such as:
+  //     foo(
+  //       a,
+  //     );
+  //     function foo(
+  //       a,
+  //     ) {
+  //       code(); //
+  //     }
+  if (Current.is(tok::r_paren) && State.Stack.size() > 1 &&
+      (!Current.Next || Current.Next->isOneOf(tok::semi, tok::l_brace)))
     return State.Stack[State.Stack.size() - 2].LastSpace;
   if (NextNonComment->is(TT_TemplateString) && NextNonComment->closesScope())
     return State.Stack[State.Stack.size() - 2].LastSpace;

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=304135&r1=304134&r2=304135&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Mon May 29 02:50:52 2017
@@ -717,6 +717,11 @@ TEST_F(FormatTestJS, FunctionLiterals) {
                "  bar();\n"
                "}.bind(this));");
 
+  verifyFormat("SomeFunction((function() {\n"
+               "               foo();\n"
+               "               bar();\n"
+               "             }).bind(this));");
+
   // FIXME: This is bad, we should be wrapping before "function() {".
   verifyFormat("someFunction(function() {\n"
                "  doSomething();  // break\n"


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to