summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Probst <martin@probst.io>2016-09-06 18:39:30 +0000
committerMartin Probst <martin@probst.io>2016-09-06 18:39:30 +0000
commit56ff7aaacb4413b537d7d381a2502b371150466b (patch)
tree90834ad636b9c966fecaecbb83c237e501b7aafb
parent5baa1c76410e2d250a897f92ddc92dbdcd69040c (diff)
downloadbcm5719-llvm-56ff7aaacb4413b537d7d381a2502b371150466b.tar.gz
bcm5719-llvm-56ff7aaacb4413b537d7d381a2502b371150466b.zip
clang-format: [JS] ignore comments when wrapping returns.
Summary: When code contains a comment between `return` and the value: return /* lengthy comment here */ ( lengthyValueComesHere); Do not wrap before the comment, as that'd break the code through JS' automatic semicolon insertion. Reviewers: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D24257 llvm-svn: 280730
-rw-r--r--clang/lib/Format/TokenAnnotator.cpp7
-rw-r--r--clang/unittests/Format/FormatTestJS.cpp2
2 files changed, 8 insertions, 1 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 7f201119a79..07085939810 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2381,7 +2381,12 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
Keywords.kw_implements))
return true;
} else if (Style.Language == FormatStyle::LK_JavaScript) {
- if (Left.is(tok::kw_return))
+ const FormatToken *NonComment = Right.getPreviousNonComment();
+ if (Left.isOneOf(tok::kw_return, tok::kw_continue, tok::kw_break,
+ tok::kw_throw) ||
+ (NonComment &&
+ NonComment->isOneOf(tok::kw_return, tok::kw_continue, tok::kw_break,
+ tok::kw_throw)))
return false; // Otherwise a semicolon is inserted.
if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace))
return false;
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index ed227852ef4..17d5cd58f1d 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -686,7 +686,9 @@ TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) {
// would change due to automatic semicolon insertion.
// See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1.
verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10));
verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10));
verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10));
OpenPOWER on IntegriCloud