r208292 - clang-format: [JS] support closures in container literals.

Daniel Jasper djasper at google.com
Thu May 8 02:25:39 PDT 2014


Author: djasper
Date: Thu May  8 04:25:39 2014
New Revision: 208292

URL: http://llvm.org/viewvc/llvm-project?rev=208292&view=rev
Log:
clang-format: [JS] support closures in container literals.

Before:
  return {body: {setAttribute: function(key, val) {this[key] = val;
  }
  , getAttribute : function(key) { return this[key]; }
  , style : {
  direction:
    ''
  }
  }
  }
  ;

After:
  return {
    body: {
      setAttribute: function(key, val) { this[key] = val; },
      getAttribute: function(key) { return this[key]; },
      style: {direction: ''}
    }
  };

Modified:
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/lib/Format/UnwrappedLineParser.h
    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=208292&r1=208291&r2=208292&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Thu May  8 04:25:39 2014
@@ -886,6 +886,22 @@ bool UnwrappedLineParser::tryToParseLamb
   return false;
 }
 
+void UnwrappedLineParser::tryToParseJSFunction() {
+  nextToken();
+  if (FormatTok->isNot(tok::l_paren))
+    return;
+  nextToken();
+  while (FormatTok->isNot(tok::l_brace)) {
+    // Err on the side of caution in order to avoid consuming the full file in
+    // case of incomplete code.
+    if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
+                            tok::comment))
+      return;
+    nextToken();
+  }
+  parseChildBlock();
+}
+
 bool UnwrappedLineParser::tryToParseBracedList() {
   if (FormatTok->BlockKind == BK_Unknown)
     calculateBraceTypes();
@@ -903,9 +919,11 @@ bool UnwrappedLineParser::parseBracedLis
   // FIXME: Once we have an expression parser in the UnwrappedLineParser,
   // replace this by using parseAssigmentExpression() inside.
   do {
-    // FIXME: When we start to support lambdas, we'll want to parse them away
-    // here, otherwise our bail-out scenarios below break. The better solution
-    // might be to just implement a more or less complete expression parser.
+    if (Style.Language == FormatStyle::LK_JavaScript &&
+        FormatTok->TokenText == "function") {
+      tryToParseJSFunction();
+      continue;
+    }
     switch (FormatTok->Tok.getKind()) {
     case tok::caret:
       nextToken();

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=208292&r1=208291&r2=208292&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Thu May  8 04:25:39 2014
@@ -100,6 +100,7 @@ private:
   void parseObjCProtocol();
   bool tryToParseLambda();
   bool tryToParseLambdaIntroducer();
+  void tryToParseJSFunction();
   void addUnwrappedLine();
   bool eof() const;
   void nextToken();

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=208292&r1=208291&r2=208292&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Thu May  8 04:25:39 2014
@@ -101,6 +101,13 @@ TEST_F(FormatTestJS, GoogScopes) {
 TEST_F(FormatTestJS, Closures) {
   verifyFormat("doFoo(function() { return 1; });");
   verifyFormat("var func = function() { return 1; };");
+  verifyFormat("return {\n"
+               "  body: {\n"
+               "    setAttribute: function(key, val) { this[key] = val; },\n"
+               "    getAttribute: function(key) { return this[key]; },\n"
+               "    style: {direction: ''}\n"
+               "  }\n"
+               "};");
 }
 
 TEST_F(FormatTestJS, ReturnStatements) {





More information about the cfe-commits mailing list