r217157 - clang-format: [JS] Support comments in dict literals.

Daniel Jasper djasper at google.com
Thu Sep 4 07:58:31 PDT 2014


Author: djasper
Date: Thu Sep  4 09:58:30 2014
New Revision: 217157

URL: http://llvm.org/viewvc/llvm-project?rev=217157&view=rev
Log:
clang-format: [JS] Support comments in dict literals.

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
  };

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTestJS.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=217157&r1=217156&r2=217157&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Sep  4 09:58:30 2014
@@ -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;
   }
 

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=217157&r1=217156&r2=217157&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Thu Sep  4 09:58:30 2014
@@ -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) {





More information about the cfe-commits mailing list