[clang-format] break after the closing paren of a TypeScript decoration

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
This commit is contained in:
Krasimir Georgiev 2021-08-23 15:51:39 +02:00
parent d39d3a327b
commit f3671a688d
2 changed files with 28 additions and 0 deletions

View File

@ -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"
@ -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

View File

@ -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"