r238544 - clang-format: [JS] Support ES6 computed property names.

Daniel Jasper djasper at google.com
Thu May 28 23:19:49 PDT 2015


Author: djasper
Date: Fri May 29 01:19:49 2015
New Revision: 238544

URL: http://llvm.org/viewvc/llvm-project?rev=238544&view=rev
Log:
clang-format: [JS] Support ES6 computed property names.

Before:
  var x = {
        [a]: 1,
    b: 2
  };

After:
  var x = {
    [a]: 1,
    b: 2
  };

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

Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=238544&r1=238543&r2=238544&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Fri May 29 01:19:49 2015
@@ -51,6 +51,7 @@ enum TokenType {
   TT_InlineASMBrace,
   TT_InlineASMColon,
   TT_JavaAnnotation,
+  TT_JsComputedPropertyName,
   TT_JsFatArrow,
   TT_JsTypeColon,
   TT_JsTypeOptionalQuestion,

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=238544&r1=238543&r2=238544&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Fri May 29 01:19:49 2015
@@ -275,6 +275,9 @@ private:
     if (Left->is(TT_Unknown)) {
       if (StartsObjCMethodExpr) {
         Left->Type = TT_ObjCMethodExpr;
+      } else if (Style.Language == FormatStyle::LK_JavaScript && Parent &&
+                 Parent->isOneOf(tok::l_brace, tok::comma)) {
+        Left->Type = TT_JsComputedPropertyName;
       } else if (Parent && Parent->isOneOf(tok::at, tok::equal, tok::comma)) {
         Left->Type = TT_ArrayInitializerLSquare;
       } else {

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=238544&r1=238543&r2=238544&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Fri May 29 01:19:49 2015
@@ -151,6 +151,11 @@ TEST_F(FormatTestJS, ContainerLiterals)
   // Arrow functions in object literals.
   verifyFormat("var x = {y: (a) => { return a; }};");
   verifyFormat("var x = {y: (a) => a};");
+
+  // Computed keys.
+  verifyFormat("var x = {\n"
+               "  [a]: 1,\n"
+               "};");
 }
 
 TEST_F(FormatTestJS, MethodsInObjectLiterals) {





More information about the cfe-commits mailing list