]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Support ES6 computed property names.
authorDaniel Jasper <djasper@google.com>
Fri, 29 May 2015 06:19:49 +0000 (06:19 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 29 May 2015 06:19:49 +0000 (06:19 +0000)
Before:
  var x = {
        [a]: 1,
    b: 2
  };

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

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

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

index ec0fdf4aa813b04bf78d50c123a8d1b753de21c6..dd12969454e4424a3c951415ccc36c38778080a8 100644 (file)
@@ -51,6 +51,7 @@ enum TokenType {
   TT_InlineASMBrace,
   TT_InlineASMColon,
   TT_JavaAnnotation,
+  TT_JsComputedPropertyName,
   TT_JsFatArrow,
   TT_JsTypeColon,
   TT_JsTypeOptionalQuestion,
index 2d8dbdbad50814bac91fef6a7ce8cc6f26b65658..3b6628ecfbdd2ba22b85027b3de33e10e997ab51 100644 (file)
@@ -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 {
index a536926c73c9e8eba18af60bc1898653fe7183ca..d95ff9ba8341a57fe99d7d7382f7401a9718f71e 100644 (file)
@@ -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) {