]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Support comments in dict literals.
authorDaniel Jasper <djasper@google.com>
Thu, 4 Sep 2014 14:58:30 +0000 (14:58 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 4 Sep 2014 14:58:30 +0000 (14:58 +0000)
Before:
  var stuff = {
    // comment for update
    update : false,
             // comment for update
    modules : false,
              // comment for update
    tasks : false
  };

After:
  var stuff = {
    // comment for update
    update : false,
    // comment for update
    modules : false,
    // comment for update
    tasks : false
  };

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

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTestJS.cpp

index 3b92c10fd8f8e6a4fae153155f87a15b3639cd44..6142e7dc56ba677b3e0aeaeda54dbc04d170adae 100644 (file)
@@ -1100,7 +1100,7 @@ public:
           ++OperatorIndex;
         }
 
-        next();
+        next(/*SkipPastLeadingComments=*/false);
       }
     }
   }
@@ -1113,7 +1113,9 @@ private:
       if (Current->Type == TT_ConditionalExpr)
         return prec::Conditional;
       else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon ||
-               Current->Type == TT_SelectorName)
+               Current->Type == TT_SelectorName ||
+               (Current->is(tok::comment) && Current->getNextNonComment() &&
+                Current->getNextNonComment()->Type == TT_SelectorName))
         return 0;
       else if (Current->Type == TT_RangeBasedForLoopColon)
         return prec::Comma;
@@ -1166,10 +1168,12 @@ private:
     addFakeParenthesis(Start, prec::Conditional);
   }
 
-  void next() {
+  void next(bool SkipPastLeadingComments = true) {
     if (Current)
       Current = Current->Next;
-    while (Current && Current->isTrailingComment())
+    while (Current &&
+           (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
+           Current->isTrailingComment())
       Current = Current->Next;
   }
 
index f8802b85d606ff424115bc49fa41de55832d27f1..4b0d1e77bd742ef7402d72746a2f5bbf8dcdb97c 100644 (file)
@@ -111,6 +111,14 @@ TEST_F(FormatTestJS, ContainerLiterals) {
                "        f();  //\n"
                "      }\n"
                "};");
+  verifyFormat("var stuff = {\n"
+               "  // comment for update\n"
+               "  update: false,\n"
+               "  // comment for modules\n"
+               "  modules: false,\n"
+               "  // comment for tasks\n"
+               "  tasks: false\n"
+               "};");
 }
 
 TEST_F(FormatTestJS, SpacesInContainerLiterals) {