r239462 - clang-format: [JS] Only special case top level object literal

Daniel Jasper djasper at google.com
Wed Jun 10 02:21:11 PDT 2015


Author: djasper
Date: Wed Jun 10 04:21:09 2015
New Revision: 239462

URL: http://llvm.org/viewvc/llvm-project?rev=239462&view=rev
Log:
clang-format: [JS] Only special case top level object literal
assignments as enums.

Top level object literals are treated as enums, and their k/v pairs are put on
separate lines:

  X.Y = {
    A: 1,
    B: 2
  };

However assignments within blocks should not be affected:

  function x() {
    y = {a:1, b:2};
  }

This change fixes the second case. Patch by Martin Probst.

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=239462&r1=239461&r2=239462&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Jun 10 04:21:09 2015
@@ -2088,13 +2088,14 @@ bool TokenAnnotator::mustBreakBefore(con
         Left.Previous->is(tok::char_constant))
       return true;
     if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) &&
-        Left.NestingLevel == 0 && Left.Previous &&
+        Line.Level == 0 && Left.Previous &&
         Left.Previous->is(tok::equal) &&
         Line.First->isOneOf(tok::identifier, Keywords.kw_import,
-                            tok::kw_export) &&
+                            tok::kw_export, tok::kw_const) &&
         // kw_var is a pseudo-token that's a tok::identifier, so matches above.
         !Line.First->is(Keywords.kw_var))
-      // Enum style object literal.
+      // Object literals on the top level of a file are treated as "enum-style".
+      // Each key/value pair is put on a separate line, instead of bin-packing.
       return true;
   } else if (Style.Language == FormatStyle::LK_Java) {
     if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=239462&r1=239461&r2=239462&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Wed Jun 10 04:21:09 2015
@@ -146,6 +146,10 @@ TEST_F(FormatTestJS, ContainerLiterals)
   // Enum style top level assignment.
   verifyFormat("X = {\n  a: 123\n};");
   verifyFormat("X.Y = {\n  a: 123\n};");
+  // But only on the top level, otherwise its a plain object literal assignment.
+  verifyFormat("function x() {\n"
+               "  y = {z: 1};\n"
+               "}");
   verifyFormat("x = foo && {a: 123};");
 
   // Arrow functions in object literals.





More information about the cfe-commits mailing list