r212038 - clang-format: [JS] support free-standing functions again.

Daniel Jasper djasper at google.com
Mon Jun 30 06:24:55 PDT 2014


Author: djasper
Date: Mon Jun 30 08:24:54 2014
New Revision: 212038

URL: http://llvm.org/viewvc/llvm-project?rev=212038&view=rev
Log:
clang-format: [JS] support free-standing functions again.

This worked initially but was broken by r210887.

Before:
  function outer1(a, b) {
    function inner1(a, b) { return a; } inner1(a, b);
  } function outer2(a, b) { function inner2(a, b) { return a; } inner2(a, b); }

After:
  function outer1(a, b) {
    function inner1(a, b) { return a; }
    inner1(a, b);
  }
  function outer2(a, b) {
    function inner2(a, b) { return a; }
    inner2(a, b);
  }

Thanks to Adam Strzelecki for working on this.

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

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=212038&r1=212037&r2=212038&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon Jun 30 08:24:54 2014
@@ -770,7 +770,10 @@ void UnwrappedLineParser::parseStructura
       return;
     case tok::identifier: {
       StringRef Text = FormatTok->TokenText;
-      if (Style.Language == FormatStyle::LK_JavaScript && Text == "function") {
+      // Parse function literal unless 'function' is the first token in a line
+      // in which case this should be treated as a free-standing function.
+      if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
+          Line->Tokens.size() > 0) {
         tryToParseJSFunction();
         break;
       }

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=212038&r1=212037&r2=212038&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Mon Jun 30 08:24:54 2014
@@ -138,6 +138,17 @@ TEST_F(FormatTestJS, GoogScopes) {
                "});  // goog.scope");
 }
 
+TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
+  verifyFormat("function outer1(a, b) {\n"
+               "  function inner1(a, b) { return a; }\n"
+               "  inner1(a, b);\n"
+               "}\n"
+               "function outer2(a, b) {\n"
+               "  function inner2(a, b) { return a; }\n"
+               "  inner2(a, b);\n"
+               "}");
+}
+
 TEST_F(FormatTestJS, FunctionLiterals) {
   verifyFormat("doFoo(function() { return 1; });");
   verifyFormat("var func = function() { return 1; };");





More information about the cfe-commits mailing list