Skip to content

Commit

Permalink
[clang-format] break after the closing paren of a TypeScript decoration
Browse files Browse the repository at this point in the history
This fixes up a regression we found from
https://reviews.llvm.org/D107267: in specific contexts, clang-format
stopped breaking after the `)` in TypeScript decorations. There were no test cases covering this, so I added one.

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D108538
  • Loading branch information
krasimirgg committed Aug 23, 2021
1 parent d39d3a3 commit f3671a6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions clang/lib/Format/ContinuationIndenter.cpp
Expand Up @@ -14,6 +14,7 @@
#include "ContinuationIndenter.h"
#include "BreakableToken.h"
#include "FormatInternal.h"
#include "FormatToken.h"
#include "WhitespaceManager.h"
#include "clang/Basic/OperatorPrecedence.h"
#include "clang/Basic/SourceManager.h"
Expand Down Expand Up @@ -491,6 +492,12 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
return true;
}

// Break after the closing parenthesis of TypeScript decorators.
if (Style.Language == FormatStyle::LK_JavaScript &&
Previous.is(tok::r_paren) && Previous.is(TT_JavaAnnotation)) {
return true;
}

// If the return type spans multiple lines, wrap before the function name.
if (((Current.is(TT_FunctionDeclarationName) &&
// Don't break before a C# function when no break after return type
Expand Down
21 changes: 21 additions & 0 deletions clang/unittests/Format/FormatTestJS.cpp
Expand Up @@ -701,6 +701,27 @@ TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
getGoogleJSStyleWithColumns(20)));
}

TEST_F(FormatTestJS, FormatsDecoratedFunctions) {
// Regression test: ensure that there is a break before `get`.
EXPECT_EQ("class A {\n"
" private p = () => {}\n"
"\n"
" @decorated('a')\n"
" get f() {\n"
" return result;\n"
" }\n"
"}",
format("class A {\n"
" private p = () => {}\n"
"\n"
" @decorated('a')\n"
" get f() {\n"
" return result;\n"
" }\n"
"}",
getGoogleJSStyleWithColumns(50)));
}

TEST_F(FormatTestJS, GeneratorFunctions) {
verifyFormat("function* f() {\n"
" let x = 1;\n"
Expand Down

0 comments on commit f3671a6

Please sign in to comment.