Skip to content

Commit fa8d1b1

Browse files
committed
[clang-format] detect nesting in template strings
The helper to check if a token is in a template string scans too far backward. It should stop if a different scope is found. Fixes #107571
1 parent aaadaee commit fa8d1b1

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
826826
for (const auto *Prev = &Tok; Prev; Prev = Prev->Previous) {
827827
if (Prev->is(TT_TemplateString) && Prev->opensScope())
828828
return true;
829-
if (Prev->is(TT_TemplateString) && Prev->closesScope())
829+
if (Prev->opensScope() ||
830+
(Prev->is(TT_TemplateString) && Prev->closesScope())) {
830831
break;
832+
}
831833
}
832834
return false;
833835
};

clang/unittests/Format/FormatTestJS.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,13 @@ TEST_F(FormatTestJS, TemplateStringMultiLineExpression) {
21572157
" aaaa: aaaaa,\n"
21582158
" bbbb: bbbbb,\n"
21592159
" })}`;");
2160+
2161+
verifyFormat("`${\n"
2162+
" (\n"
2163+
" FOOFOOFOOFOO____FOO_FOO_FO_FOO_FOOO -\n"
2164+
" (barbarbarbar____bar_bar_bar_bar_bar_bar +\n"
2165+
" bar_bar_bar_barbarbar___bar_bar_bar + 1),\n"
2166+
" )}`;\n");
21602167
}
21612168

21622169
TEST_F(FormatTestJS, TemplateStringASI) {

0 commit comments

Comments
 (0)