]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Support ES6 destructuring assignments.
authorDaniel Jasper <djasper@google.com>
Mon, 19 May 2014 07:37:07 +0000 (07:37 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 19 May 2014 07:37:07 +0000 (07:37 +0000)
Before:
  var[a, b, c] = [1, 2, 3];

After:
  var [a, b, c] = [1, 2, 3];

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

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

index d6a54db74e80206e82cc947b1be029ed9b78eba4..47ecd6a4dc3ce53c1f1620a42bb179634be3007b 100644 (file)
@@ -1381,6 +1381,9 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
     if (Right.is(tok::l_paren) &&
         (Left.TokenText == "returns" || Left.TokenText == "option"))
       return true;
+  } else if (Style.Language == FormatStyle::LK_JavaScript) {
+    if (Left.TokenText == "var")
+      return true;
   }
   if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
     return true;
index c10dc9675f4bb53fa837c81ccd9f9b41bfa93626..f38bf895f21ce4af183ddbff75709f8f93ac382a 100644 (file)
@@ -81,6 +81,11 @@ TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
   verifyFormat("var b = a.map((x) => x + 1);");
 }
 
+TEST_F(FormatTestJS, ES6DestructuringAssignment) {
+  verifyFormat("var [a, b, c] = [1, 2, 3];");
+  verifyFormat("var {a, b} = {a: 1, b: 2};");
+}
+
 TEST_F(FormatTestJS, SpacesInContainerLiterals) {
   verifyFormat("var arr = [1, 2, 3];");
   verifyFormat("var obj = {a: 1, b: 2, c: 3};");