Skip to content

Commit

Permalink
clang-format: [JS] ignore comments when wrapping returns.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mprobst committed Sep 6, 2016
1 parent 5baa1c7 commit 56ff7aa
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions clang/unittests/Format/FormatTestJS.cpp
Original file line number Diff line number Diff line change
@@ -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));

0 comments on commit 56ff7aa

Please sign in to comment.