Skip to content

Commit 57c161a

Browse files
authored
[clang-format] Detect nesting in template strings (#119989)
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 146240e commit 57c161a

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
@@ -2161,6 +2161,13 @@ TEST_F(FormatTestJS, TemplateStringMultiLineExpression) {
21612161
" aaaa: aaaaa,\n"
21622162
" bbbb: bbbbb,\n"
21632163
" })}`;");
2164+
2165+
verifyFormat("`${\n"
2166+
" (\n"
2167+
" FOOFOOFOOFOO____FOO_FOO_FO_FOO_FOOO -\n"
2168+
" (barbarbarbar____bar_bar_bar_bar_bar_bar +\n"
2169+
" bar_bar_bar_barbarbar___bar_bar_bar + 1),\n"
2170+
" )}`;");
21642171
}
21652172

21662173
TEST_F(FormatTestJS, TemplateStringASI) {

0 commit comments

Comments
 (0)