Skip to content

Commit

Permalink
[clang-format] don't break between function and function name in JS
Browse files Browse the repository at this point in the history
The patch https://reviews.llvm.org/D105964 (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
  • Loading branch information
krasimirgg committed Aug 3, 2021
1 parent d77b476 commit 4f4f278
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clang/lib/Format/ContinuationIndenter.cpp
Expand Up @@ -495,7 +495,10 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
if (((Current.is(TT_FunctionDeclarationName) &&
// Don't break before a C# function when no break after return type
(!Style.isCSharp() ||
Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None)) ||
Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None) &&
// Don't always break between a JavaScript `function` and the function
// name.
Style.Language != FormatStyle::LK_JavaScript) ||
(Current.is(tok::kw_operator) && !Previous.is(tok::coloncolon))) &&
!Previous.is(tok::kw_template) && State.Stack.back().BreakBeforeParameter)
return true;
Expand Down
7 changes: 7 additions & 0 deletions clang/unittests/Format/FormatTestJS.cpp
Expand Up @@ -692,6 +692,13 @@ TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
" let x = 1;\n"
" console.log(x);\n"
"}\n");
EXPECT_EQ("a = function(x) {}\n"
"\n"
"function f(x) {}",
format("a = function(x) {}\n"
"\n"
"function f(x) {}",
getGoogleJSStyleWithColumns(20)));
}

TEST_F(FormatTestJS, GeneratorFunctions) {
Expand Down

0 comments on commit 4f4f278

Please sign in to comment.