Skip to content

Commit 8168088

Browse files
authored
[clang-format] Fix regressions in BAS_AlwaysBreak (#107506)
Fixes #107401. Fixes #107574.
1 parent 335538c commit 8168088

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
815815
return Tok.is(tok::l_paren) && Tok.ParameterCount > 0 && Tok.Previous &&
816816
Tok.Previous->is(tok::identifier);
817817
};
818-
const auto IsInTemplateString = [this](const FormatToken &Tok) {
818+
auto IsInTemplateString = [this](const FormatToken &Tok) {
819819
if (!Style.isJavaScript())
820820
return false;
821821
for (const auto *Prev = &Tok; Prev; Prev = Prev->Previous) {
@@ -827,7 +827,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
827827
return false;
828828
};
829829
// Identifies simple (no expression) one-argument function calls.
830-
const auto IsSimpleFunction = [&](const FormatToken &Tok) {
830+
auto StartsSimpleOneArgList = [&](const FormatToken &TokAfterLParen) {
831+
assert(TokAfterLParen.isNot(tok::comment) || TokAfterLParen.Next);
832+
const auto &Tok =
833+
TokAfterLParen.is(tok::comment) ? *TokAfterLParen.Next : TokAfterLParen;
831834
if (!Tok.FakeLParens.empty() && Tok.FakeLParens.back() > prec::Unknown)
832835
return false;
833836
// Nested calls that involve `new` expressions also look like simple
@@ -836,6 +839,11 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
836839
// - foo(::new Bar())
837840
if (Tok.is(tok::kw_new) || Tok.startsSequence(tok::coloncolon, tok::kw_new))
838841
return true;
842+
if (Tok.is(TT_UnaryOperator) ||
843+
(Style.isJavaScript() &&
844+
Tok.isOneOf(tok::ellipsis, Keywords.kw_await))) {
845+
return true;
846+
}
839847
const auto *Previous = Tok.Previous;
840848
if (!Previous || (!Previous->isOneOf(TT_FunctionDeclarationLParen,
841849
TT_LambdaDefinitionLParen) &&
@@ -861,7 +869,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
861869
// or
862870
// caaaaaaaaaaaaaaaaaaaaal(
863871
// new SomethingElseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee());
864-
!IsSimpleFunction(Current)) {
872+
!StartsSimpleOneArgList(Current)) {
865873
CurrentState.NoLineBreak = true;
866874
}
867875

clang/unittests/Format/FormatTestJS.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,5 +2850,22 @@ TEST_F(FormatTestJS, DontBreakFieldsAsGoToLabels) {
28502850
"};");
28512851
}
28522852

2853+
TEST_F(FormatTestJS, BreakAfterOpenBracket) {
2854+
auto Style = getGoogleStyle(FormatStyle::LK_JavaScript);
2855+
EXPECT_EQ(Style.AlignAfterOpenBracket, FormatStyle::BAS_AlwaysBreak);
2856+
verifyFormat("ctrl.onCopy(/** @type {!WizEvent}*/ (\n"
2857+
" {event, targetElement: {el: () => selectedElement}}));",
2858+
Style);
2859+
verifyFormat("failedUserIds.push(...subscriptioxxxxxxxxxxxxnSubset.map(\n"
2860+
" subscxxxxxxxxxxxxription => subscription.getUserId()));",
2861+
Style);
2862+
verifyFormat("failedUserIds.push(!subscriptioxxxxxxxxxxxxnSubset.map(\n"
2863+
" subscxxxxxxxxxxxxription => subscription.getUserId()));",
2864+
Style);
2865+
verifyFormat("failedUserIds.push(await subscriptioxxxxxxxxxxxxnSubset.map(\n"
2866+
" subscxxxxxxxxxxxxription => subscription.getUserId()));",
2867+
Style);
2868+
}
2869+
28532870
} // namespace format
28542871
} // end namespace clang

0 commit comments

Comments
 (0)