]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] support free-standing functions again.
authorDaniel Jasper <djasper@google.com>
Mon, 30 Jun 2014 13:24:54 +0000 (13:24 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 30 Jun 2014 13:24:54 +0000 (13:24 +0000)
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.

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

lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestJS.cpp

index 71ba893cf360f812e6fecee84764075441a4c473..20dd573162ccc39d5e21feee0bfb8da93340061b 100644 (file)
@@ -770,7 +770,10 @@ void UnwrappedLineParser::parseStructuralElement() {
       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;
       }
index 7f5507ee26a97ebe78cae21e14cab63753a6d030..f8802b85d606ff424115bc49fa41de55832d27f1 100644 (file)
@@ -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; };");