Skip to content

[clang-format] Detect nesting in template strings #119989

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 2 commits into from
Dec 17, 2024
Merged

Conversation

gedare
Copy link
Contributor

@gedare gedare commented Dec 14, 2024

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

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 llvm#107571
@llvmbot
Copy link
Member

llvmbot commented Dec 14, 2024

@llvm/pr-subscribers-clang-format

Author: Gedare Bloom (gedare)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/119989.diff

2 Files Affected:

  • (modified) clang/lib/Format/ContinuationIndenter.cpp (+3-1)
  • (modified) clang/unittests/Format/FormatTestJS.cpp (+7)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index aed86c1fb99551..9ffdc044e6784d 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -826,8 +826,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
     for (const auto *Prev = &Tok; Prev; Prev = Prev->Previous) {
       if (Prev->is(TT_TemplateString) && Prev->opensScope())
         return true;
-      if (Prev->is(TT_TemplateString) && Prev->closesScope())
+      if (Prev->opensScope() ||
+          (Prev->is(TT_TemplateString) && Prev->closesScope())) {
         break;
+      }
     }
     return false;
   };
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 4b15e7b7da3393..678fd1f3fc8d0e 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -2157,6 +2157,13 @@ TEST_F(FormatTestJS, TemplateStringMultiLineExpression) {
                "                          aaaa:  aaaaa,\n"
                "                          bbbb:  bbbbb,\n"
                "                        })}`;");
+
+  verifyFormat("`${\n"
+               "    (\n"
+               "        FOOFOOFOOFOO____FOO_FOO_FO_FOO_FOOO -\n"
+               "            (barbarbarbar____bar_bar_bar_bar_bar_bar +\n"
+               "             bar_bar_bar_barbarbar___bar_bar_bar + 1),\n"
+               "        )}`;\n");
 }
 
 TEST_F(FormatTestJS, TemplateStringASI) {

@owenca owenca changed the title [clang-format] detect nesting in template strings [clang-format] Detect nesting in template strings Dec 15, 2024
@owenca owenca requested a review from kadircet December 15, 2024 03:21
@owenca owenca merged commit 57c161a into llvm:main Dec 17, 2024
8 checks passed
@gedare gedare deleted the fix-107571 branch April 30, 2025 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression in formatting for JS template strings
3 participants