r209205 - clang-format: [JS] Understand top-level function literals properly.

Daniel Jasper djasper at google.com
Tue May 20 04:14:58 PDT 2014


Author: djasper
Date: Tue May 20 06:14:57 2014
New Revision: 209205

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

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=209205&r1=209204&r2=209205&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Tue May 20 06:14:57 2014
@@ -763,6 +763,10 @@ void UnwrappedLineParser::parseStructura
       return;
     case tok::identifier: {
       StringRef Text = FormatTok->TokenText;
+      if (Style.Language == FormatStyle::LK_JavaScript && Text == "function") {
+        tryToParseJSFunction();
+        break;
+      }
       nextToken();
       if (Line->Tokens.size() == 1) {
         if (FormatTok->Tok.is(tok::colon)) {

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=209205&r1=209204&r2=209205&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Tue May 20 06:14:57 2014
@@ -31,7 +31,9 @@ protected:
     return Result;
   }
 
-  static std::string format(llvm::StringRef Code, const FormatStyle &Style) {
+  static std::string format(
+      llvm::StringRef Code,
+      const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
     return format(Code, 0, Code.size(), Style);
   }
 
@@ -116,6 +118,8 @@ TEST_F(FormatTestJS, Closures) {
                "    style: {direction: ''}\n"
                "  }\n"
                "};");
+  EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };",
+            format("abc=xyz?function(){return 1;}:function(){return -1;};"));
 }
 
 TEST_F(FormatTestJS, ReturnStatements) {





More information about the cfe-commits mailing list