]> granicus.if.org Git - clang/commitdiff
[clang-format] [JS] Don't break between template string and tag
authorMartin Probst <martin@probst.io>
Tue, 19 Mar 2019 11:15:52 +0000 (11:15 +0000)
committerMartin Probst <martin@probst.io>
Tue, 19 Mar 2019 11:15:52 +0000 (11:15 +0000)
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!).

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@356447 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTestJS.cpp

index 246ac781fd9b33168e2e273d1d8171904265b61b..ef449e44c9526bdd51b8f15a027bc54f2a4fc48d 100644 (file)
@@ -3171,6 +3171,11 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
       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;
   }
index ed8faabb3f2aa9367a0c8bf7078de00b7ebdaf37..528e4cf57cbf8c9dceb0043b5f4cbb955bbcd325 100644 (file)
@@ -1962,6 +1962,12 @@ TEST_F(FormatTestJS, NestedTemplateStrings) {
 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) {