]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] handle conditionals in fields, default params.
authorMartin Probst <martin@probst.io>
Thu, 23 Jun 2016 21:51:49 +0000 (21:51 +0000)
committerMartin Probst <martin@probst.io>
Thu, 23 Jun 2016 21:51:49 +0000 (21:51 +0000)
Summary:

Reviewers: djasper

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D21658

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

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

index 5302f13340ff8320dfaf00252643cffdaeb3313e..4a90522e6e31cd8d2ba9e3575a6c3d409c3250d3 100644 (file)
@@ -639,7 +639,7 @@ private:
       }
       // Declarations cannot be conditional expressions, this can only be part
       // of a type declaration.
-      if (Line.MustBeDeclaration &&
+      if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
           Style.Language == FormatStyle::LK_JavaScript)
         break;
       parseConditional();
@@ -1009,7 +1009,7 @@ private:
       Current.Type = TT_UnaryOperator;
     } else if (Current.is(tok::question)) {
       if (Style.Language == FormatStyle::LK_JavaScript &&
-          Line.MustBeDeclaration) {
+          Line.MustBeDeclaration && !Contexts.back().IsExpression) {
         // In JavaScript, `interface X { foo?(): bar; }` is an optional method
         // on the interface, not a ternary expression.
         Current.Type = TT_JsTypeOptionalQuestion;
index bd5a505d59ee2c873f420582b9fea6e52ccbbd86..3b9667d5ca4458529dd2c6a358e21444c7a2f10c 100644 (file)
@@ -1253,7 +1253,6 @@ TEST_F(FormatTestJS, OptionalTypes) {
   verifyFormat("interface X {\n"
                "  y?(): z;\n"
                "}");
-  verifyFormat("x ? 1 : 2;");
   verifyFormat("constructor({aa}: {\n"
                "  aa?: string,\n"
                "  aaaaaaaa?: string,\n"
@@ -1350,5 +1349,14 @@ TEST_F(FormatTestJS, NonNullAssertionOperator) {
   verifyFormat("let x = {foo: 1}!;\n");
 }
 
+TEST_F(FormatTestJS, Conditional) {
+  verifyFormat("y = x ? 1 : 2;");
+  verifyFormat("x ? 1 : 2;");
+  verifyFormat("class Foo {\n"
+               "  field = true ? 1 : 2;\n"
+               "  method(a = true ? 1 : 2) {}\n"
+               "}");
+}
+
 } // end namespace tooling
 } // end namespace clang