]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] support closures in container literals.
authorDaniel Jasper <djasper@google.com>
Thu, 8 May 2014 09:25:39 +0000 (09:25 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 8 May 2014 09:25:39 +0000 (09:25 +0000)
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: ''}
    }
  };

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

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

index 5b6727bf9e430ce5fca2cd0fcaf544dd9a8587b9..940fcc8ae2f1e47da7988e9c94a779856d379547 100644 (file)
@@ -886,6 +886,22 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
   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::parseBracedList(bool ContinueOnSemicolons) {
   // 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();
index 8f0c5a3ef41e4957fcf7331cb7a280c87624a5a9..9eb56011629572ced56fe6ef4e95d061a71ef2a1 100644 (file)
@@ -100,6 +100,7 @@ private:
   void parseObjCProtocol();
   bool tryToParseLambda();
   bool tryToParseLambdaIntroducer();
+  void tryToParseJSFunction();
   void addUnwrappedLine();
   bool eof() const;
   void nextToken();
index 606303a68a921c461b988bad73b3fed61e1cd993..2dfd10faf1bdfe6ba07bc9da5b769009c5d228e7 100644 (file)
@@ -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) {