r210887 - clang-format: [JS] Understand named function literals.

Daniel Jasper djasper at google.com
Fri Jun 13 00:02:05 PDT 2014


Author: djasper
Date: Fri Jun 13 02:02:04 2014
New Revision: 210887

URL: http://llvm.org/viewvc/llvm-project?rev=210887&view=rev
Log:
clang-format: [JS] Understand named function literals.

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

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

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=210887&r1=210886&r2=210887&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Fri Jun 13 02:02:04 2014
@@ -907,6 +907,11 @@ bool UnwrappedLineParser::tryToParseLamb
 
 void UnwrappedLineParser::tryToParseJSFunction() {
   nextToken();
+
+  // Consume function name.
+  if (FormatTok->is(tok::identifier))
+      nextToken();
+
   if (FormatTok->isNot(tok::l_paren))
     return;
   nextToken();

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=210887&r1=210886&r2=210887&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Fri Jun 13 02:02:04 2014
@@ -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) {





More information about the cfe-commits mailing list