Skip to content

Commit

Permalink
[clang-format] Fix regressions in BAS_AlwaysBreak (#107506)
Browse files Browse the repository at this point in the history
Fixes #107401.
Fixes #107574.
  • Loading branch information
owenca authored Sep 12, 2024
1 parent 335538c commit 8168088
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
14 changes: 11 additions & 3 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
return Tok.is(tok::l_paren) && Tok.ParameterCount > 0 && Tok.Previous &&
Tok.Previous->is(tok::identifier);
};
const auto IsInTemplateString = [this](const FormatToken &Tok) {
auto IsInTemplateString = [this](const FormatToken &Tok) {
if (!Style.isJavaScript())
return false;
for (const auto *Prev = &Tok; Prev; Prev = Prev->Previous) {
Expand All @@ -827,7 +827,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
return false;
};
// Identifies simple (no expression) one-argument function calls.
const auto IsSimpleFunction = [&](const FormatToken &Tok) {
auto StartsSimpleOneArgList = [&](const FormatToken &TokAfterLParen) {
assert(TokAfterLParen.isNot(tok::comment) || TokAfterLParen.Next);
const auto &Tok =
TokAfterLParen.is(tok::comment) ? *TokAfterLParen.Next : TokAfterLParen;
if (!Tok.FakeLParens.empty() && Tok.FakeLParens.back() > prec::Unknown)
return false;
// Nested calls that involve `new` expressions also look like simple
Expand All @@ -836,6 +839,11 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
// - foo(::new Bar())
if (Tok.is(tok::kw_new) || Tok.startsSequence(tok::coloncolon, tok::kw_new))
return true;
if (Tok.is(TT_UnaryOperator) ||
(Style.isJavaScript() &&
Tok.isOneOf(tok::ellipsis, Keywords.kw_await))) {
return true;
}
const auto *Previous = Tok.Previous;
if (!Previous || (!Previous->isOneOf(TT_FunctionDeclarationLParen,
TT_LambdaDefinitionLParen) &&
Expand All @@ -861,7 +869,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
// or
// caaaaaaaaaaaaaaaaaaaaal(
// new SomethingElseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee());
!IsSimpleFunction(Current)) {
!StartsSimpleOneArgList(Current)) {
CurrentState.NoLineBreak = true;
}

Expand Down
17 changes: 17 additions & 0 deletions clang/unittests/Format/FormatTestJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2850,5 +2850,22 @@ TEST_F(FormatTestJS, DontBreakFieldsAsGoToLabels) {
"};");
}

TEST_F(FormatTestJS, BreakAfterOpenBracket) {
auto Style = getGoogleStyle(FormatStyle::LK_JavaScript);
EXPECT_EQ(Style.AlignAfterOpenBracket, FormatStyle::BAS_AlwaysBreak);
verifyFormat("ctrl.onCopy(/** @type {!WizEvent}*/ (\n"
" {event, targetElement: {el: () => selectedElement}}));",
Style);
verifyFormat("failedUserIds.push(...subscriptioxxxxxxxxxxxxnSubset.map(\n"
" subscxxxxxxxxxxxxription => subscription.getUserId()));",
Style);
verifyFormat("failedUserIds.push(!subscriptioxxxxxxxxxxxxnSubset.map(\n"
" subscxxxxxxxxxxxxription => subscription.getUserId()));",
Style);
verifyFormat("failedUserIds.push(await subscriptioxxxxxxxxxxxxnSubset.map(\n"
" subscxxxxxxxxxxxxription => subscription.getUserId()));",
Style);
}

} // namespace format
} // end namespace clang

0 comments on commit 8168088

Please sign in to comment.