Skip to content

[clang-format] Fix regressions in BAS_AlwaysBreak #107506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading