Skip to content

Commit 4f4f278

Browse files
committed
[clang-format] don't break between function and function name in JS
The patch https://reviews.llvm.org/D105964 (llvm@58494c8) updated detection of function declaration names. It had the unfortunate consequence that it started breaking between `function` and the function name in some cases in JavaScript code. This patch addresses this. Reviewed By: MyDeveloperDay, owenpan Differential Revision: https://reviews.llvm.org/D107267
1 parent d77b476 commit 4f4f278

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,10 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
495495
if (((Current.is(TT_FunctionDeclarationName) &&
496496
// Don't break before a C# function when no break after return type
497497
(!Style.isCSharp() ||
498-
Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None)) ||
498+
Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None) &&
499+
// Don't always break between a JavaScript `function` and the function
500+
// name.
501+
Style.Language != FormatStyle::LK_JavaScript) ||
499502
(Current.is(tok::kw_operator) && !Previous.is(tok::coloncolon))) &&
500503
!Previous.is(tok::kw_template) && State.Stack.back().BreakBeforeParameter)
501504
return true;

clang/unittests/Format/FormatTestJS.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,13 @@ TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
692692
" let x = 1;\n"
693693
" console.log(x);\n"
694694
"}\n");
695+
EXPECT_EQ("a = function(x) {}\n"
696+
"\n"
697+
"function f(x) {}",
698+
format("a = function(x) {}\n"
699+
"\n"
700+
"function f(x) {}",
701+
getGoogleJSStyleWithColumns(20)));
695702
}
696703

697704
TEST_F(FormatTestJS, GeneratorFunctions) {

0 commit comments

Comments
 (0)