]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Understand top-level function literals properly.
authorDaniel Jasper <djasper@google.com>
Tue, 20 May 2014 11:14:57 +0000 (11:14 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 20 May 2014 11:14:57 +0000 (11:14 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@209205 91177308-0d34-0410-b5e6-96231b3b80d8

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

index e2613fbc171c8ae82515015b24b1bbbe22ddd22f..5af743bba5a57ab341ef44cb58871fc2ba01080b 100644 (file)
@@ -763,6 +763,10 @@ void UnwrappedLineParser::parseStructuralElement() {
       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)) {
index f38bf895f21ce4af183ddbff75709f8f93ac382a..657f10874e022b0007aed5b4a89344c81fe0e7c8 100644 (file)
@@ -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) {