summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Probst <martin@probst.io>2016-06-23 21:51:49 +0000
committerMartin Probst <martin@probst.io>2016-06-23 21:51:49 +0000
commit31d6da7c0ca24fd247078c32c16e0e95b99e20d6 (patch)
tree780fdc1608f3e4980d9e388abe49804a0180e4e7
parent4d624ae0cbed5e27c7bbb717bc6a24160f426b22 (diff)
downloadbcm5719-llvm-31d6da7c0ca24fd247078c32c16e0e95b99e20d6.tar.gz
bcm5719-llvm-31d6da7c0ca24fd247078c32c16e0e95b99e20d6.zip
clang-format: [JS] handle conditionals in fields, default params.
Summary: Reviewers: djasper Subscribers: klimek, cfe-commits Differential Revision: http://reviews.llvm.org/D21658 llvm-svn: 273619
-rw-r--r--clang/lib/Format/TokenAnnotator.cpp4
-rw-r--r--clang/unittests/Format/FormatTestJS.cpp10
2 files changed, 11 insertions, 3 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 5302f13340f..4a90522e6e3 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -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;
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index bd5a505d59e..3b9667d5ca4 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -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
OpenPOWER on IntegriCloud