Skip to content

Commit befb9dc

Browse files
committed
[clang-format] keep TypeScript argument decorators in line
As a follow-up from https://reviews.llvm.org/D108538, ensure TypeScript argument decorators are kept in line with the argument. Reviewed By: MyDeveloperDay Differential Revision: https://reviews.llvm.org/D108620
1 parent 2e8534b commit befb9dc

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/Basic/OperatorPrecedence.h"
2020
#include "clang/Basic/SourceManager.h"
2121
#include "clang/Format/Format.h"
22+
#include "llvm/ADT/StringSet.h"
2223
#include "llvm/Support/Debug.h"
2324

2425
#define DEBUG_TYPE "format-indenter"
@@ -492,8 +493,12 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
492493
return true;
493494
}
494495

495-
// Break after the closing parenthesis of TypeScript decorators.
496+
// Break after the closing parenthesis of TypeScript decorators before
497+
// functions, getters and setters.
498+
static const llvm::StringSet<> BreakBeforeDecoratedTokens = {"get", "set",
499+
"function"};
496500
if (Style.Language == FormatStyle::LK_JavaScript &&
501+
BreakBeforeDecoratedTokens.contains(Current.TokenText) &&
497502
Previous.is(tok::r_paren) && Previous.is(TT_JavaAnnotation)) {
498503
return true;
499504
}

clang/unittests/Format/FormatTestJS.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,15 +701,37 @@ TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
701701
getGoogleJSStyleWithColumns(20)));
702702
}
703703

704-
TEST_F(FormatTestJS, FormatsDecoratedFunctions) {
705-
// Regression test: ensure that there is a break before `get`.
704+
TEST_F(FormatTestJS, FormatsDecorators) {
705+
// No line break after argument decorators.
706+
verifyFormat("class A {\n"
707+
" constructor(@arg(DECOR) private arg: Type) {}\n"
708+
"}");
709+
// Ensure that there is a break before functions, getters and setters.
706710
EXPECT_EQ("class A {\n"
707711
" private p = () => {}\n"
708712
"\n"
709713
" @decorated('a')\n"
710714
" get f() {\n"
711715
" return result;\n"
712716
" }\n"
717+
"}\n"
718+
"\n"
719+
"class B {\n"
720+
" private p = () => {}\n"
721+
"\n"
722+
" @decorated('a')\n"
723+
" set f() {\n"
724+
" return result;\n"
725+
" }\n"
726+
"}\n"
727+
"\n"
728+
"class C {\n"
729+
" private p = () => {}\n"
730+
"\n"
731+
" @decorated('a')\n"
732+
" function f() {\n"
733+
" return result;\n"
734+
" }\n"
713735
"}",
714736
format("class A {\n"
715737
" private p = () => {}\n"
@@ -718,6 +740,24 @@ TEST_F(FormatTestJS, FormatsDecoratedFunctions) {
718740
" get f() {\n"
719741
" return result;\n"
720742
" }\n"
743+
"}\n"
744+
"\n"
745+
"class B {\n"
746+
" private p = () => {}\n"
747+
"\n"
748+
" @decorated('a')\n"
749+
" set f() {\n"
750+
" return result;\n"
751+
" }\n"
752+
"}\n"
753+
"\n"
754+
"class C {\n"
755+
" private p = () => {}\n"
756+
"\n"
757+
" @decorated('a')\n"
758+
" function f() {\n"
759+
" return result;\n"
760+
" }\n"
721761
"}",
722762
getGoogleJSStyleWithColumns(50)));
723763
}

0 commit comments

Comments
 (0)