]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Understand named function literals.
authorDaniel Jasper <djasper@google.com>
Fri, 13 Jun 2014 07:02:04 +0000 (07:02 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 13 Jun 2014 07:02:04 +0000 (07:02 +0000)
Before:
  return {a: function SomeFunction(){// ...
                                     return 1;
  }
  }
  ;

After:
  return {
    a: function SomeFunction() {
      // ...
      return 1;
    }
  };

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

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

index 6a8156f92eed32170188b75564513add3b9ebdf6..71ba893cf360f812e6fecee84764075441a4c473 100644 (file)
@@ -907,6 +907,11 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
 
 void UnwrappedLineParser::tryToParseJSFunction() {
   nextToken();
+
+  // Consume function name.
+  if (FormatTok->is(tok::identifier))
+      nextToken();
+
   if (FormatTok->isNot(tok::l_paren))
     return;
   nextToken();
index 485ccd624e6e3366f8fee6c8aa9af58376ab171a..7f5507ee26a97ebe78cae21e14cab63753a6d030 100644 (file)
@@ -138,7 +138,7 @@ TEST_F(FormatTestJS, GoogScopes) {
                "});  // goog.scope");
 }
 
-TEST_F(FormatTestJS, Closures) {
+TEST_F(FormatTestJS, FunctionLiterals) {
   verifyFormat("doFoo(function() { return 1; });");
   verifyFormat("var func = function() { return 1; };");
   verifyFormat("return {\n"
@@ -177,6 +177,13 @@ TEST_F(FormatTestJS, Closures) {
                "  a: function() { return 1; }\n"
                "};",
                getGoogleJSStyleWithColumns(37));
+
+  verifyFormat("return {\n"
+               "  a: function SomeFunction() {\n"
+               "    // ...\n"
+               "    return 1;\n"
+               "  }\n"
+               "};");
 }
 
 TEST_F(FormatTestJS, MultipleFunctionLiterals) {