r356447 - [clang-format] [JS] Don't break between template string and tag

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 19 04:15:53 PDT 2019


Author: mprobst
Date: Tue Mar 19 04:15:52 2019
New Revision: 356447

URL: http://llvm.org/viewvc/llvm-project?rev=356447&view=rev
Log:
[clang-format] [JS] Don't break between template string and tag

Before:
    const x = veryLongIdentifier
        `hello`;
After:
    const x =
        veryLongIdentifier`hello`;

While it's allowed to have the template string and tag identifier
separated by a line break, currently the clang-format output is not
stable when a break is forced. Additionally, disallowing a line break
makes it clear that the identifier is actually a tag for a template
string.

Patch originally by mitchellwills (thanks!).

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTestJS.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=356447&r1=356446&r2=356447&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue Mar 19 04:15:52 2019
@@ -3171,6 +3171,11 @@ bool TokenAnnotator::canBreakBefore(cons
       return false; // must not break in "module foo { ...}"
     if (Right.is(TT_TemplateString) && Right.closesScope())
       return false;
+    // Don't split tagged template literal so there is a break between the tag
+    // identifier and template string.
+    if (Left.is(tok::identifier) && Right.is(TT_TemplateString)) {
+      return false;
+    }
     if (Left.is(TT_TemplateString) && Left.opensScope())
       return true;
   }

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=356447&r1=356446&r2=356447&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Tue Mar 19 04:15:52 2019
@@ -1962,6 +1962,12 @@ TEST_F(FormatTestJS, NestedTemplateStrin
 TEST_F(FormatTestJS, TaggedTemplateStrings) {
   verifyFormat("var x = html`<ul>`;");
   verifyFormat("yield `hello`;");
+  verifyFormat("var f = {\n"
+               "  param: longTagName`This is a ${\n"
+               "                    'really'} long line`\n"
+               "};",
+               "var f = {param: longTagName`This is a ${'really'} long line`};",
+               getGoogleJSStyleWithColumns(40));
 }
 
 TEST_F(FormatTestJS, CastSyntax) {




More information about the cfe-commits mailing list