[clang] 8168088 - [clang-format] Fix regressions in BAS_AlwaysBreak (#107506)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 11 19:34:58 PDT 2024
Author: Owen Pan
Date: 2024-09-11T19:34:54-07:00
New Revision: 8168088f0a9015bc6d930e8bc1c639dee06ca82c
URL: https://github.com/llvm/llvm-project/commit/8168088f0a9015bc6d930e8bc1c639dee06ca82c
DIFF: https://github.com/llvm/llvm-project/commit/8168088f0a9015bc6d930e8bc1c639dee06ca82c.diff
LOG: [clang-format] Fix regressions in BAS_AlwaysBreak (#107506)
Fixes #107401.
Fixes #107574.
Added:
Modified:
clang/lib/Format/ContinuationIndenter.cpp
clang/unittests/Format/FormatTestJS.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 5843571718b3a2..f29f8796ea9290 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -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) {
@@ -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
@@ -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) &&
@@ -861,7 +869,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
// or
// caaaaaaaaaaaaaaaaaaaaal(
// new SomethingElseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee());
- !IsSimpleFunction(Current)) {
+ !StartsSimpleOneArgList(Current)) {
CurrentState.NoLineBreak = true;
}
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 4b29ba720f6823..c25228a69a748f 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -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
More information about the cfe-commits
mailing list