r232320 - clang-format: [JS] more precisely detect enums.

Daniel Jasper djasper at google.com
Sun Mar 15 06:55:54 PDT 2015


Author: djasper
Date: Sun Mar 15 08:55:54 2015
New Revision: 232320

URL: http://llvm.org/viewvc/llvm-project?rev=232320&view=rev
Log:
clang-format: [JS] more precisely detect enums.

The current enum detection is overly aggressive. As NestingLevel only
applies per line (?) it classifies many if not most object literals as
enum declarations and adds superfluous line breaks into them. This
change narrows the heuristic by requiring an assignment just before the
open brace and requiring the line to start with an identifier.

Patch by Martin Probst. Thank you.

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=232320&r1=232319&r2=232320&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Sun Mar 15 08:55:54 2015
@@ -1952,7 +1952,13 @@ 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.NestingLevel == 0 && Left.Previous &&
+        Left.Previous->is(tok::equal) &&
+        Line.First->isOneOf(tok::identifier, Keywords.kw_import,
+                            tok::kw_export) &&
+        // kw_var is a pseudo-token that's a tok::identifier, so matches above.
+        !Line.First->is(Keywords.kw_var))
+      // Enum style object literal.
       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=232320&r1=232319&r2=232320&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Sun Mar 15 08:55:54 2015
@@ -94,10 +94,7 @@ TEST_F(FormatTestJS, LiteralOperatorsCan
 
 TEST_F(FormatTestJS, ES6DestructuringAssignment) {
   verifyFormat("var [a, b, c] = [1, 2, 3];");
-  verifyFormat("var {a, b} = {\n"
-               "  a: 1,\n"
-               "  b: 2\n"
-               "};");
+  verifyFormat("var {a, b} = {a: 1, b: 2};");
 }
 
 TEST_F(FormatTestJS, ContainerLiterals) {
@@ -139,6 +136,12 @@ TEST_F(FormatTestJS, ContainerLiterals)
                "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
                "  }\n"
                "};");
+  // Simple object literal, as opposed to enum style below.
+  verifyFormat("var obj = {a: 123};");
+  // Enum style top level assignment.
+  verifyFormat("X = {\n  a: 123\n};");
+  verifyFormat("X.Y = {\n  a: 123\n};");
+  verifyFormat("x = foo && {a: 123};");
 }
 
 TEST_F(FormatTestJS, SpacesInContainerLiterals) {
@@ -545,7 +548,7 @@ TEST_F(FormatTestJS, Modules) {
                getGoogleJSStyleWithColumns(20));
   verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
   verifyFormat("import * as lib from 'some/module.js';");
-  verifyFormat("var x = {\n  import: 1\n};\nx.import = 2;");
+  verifyFormat("var x = {import: 1};\nx.import = 2;");
 
   verifyFormat("export function fn() {\n"
                "  return 'fn';\n"





More information about the cfe-commits mailing list