]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Fix incorrect line break in template strings.
authorDaniel Jasper <djasper@google.com>
Tue, 31 Jan 2017 12:07:35 +0000 (12:07 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 31 Jan 2017 12:07:35 +0000 (12:07 +0000)
Before:
  var f = `aaaa ${a ? 'a' : 'b'
                            }`;

After:
  var f = `aaaa ${a ? 'a' : 'b'}`;

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

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

index 61d14432561b1ecd4d0e7c407c995157830f8fac..e58ca6d803e1b60352f992bb559ea6f5c4be5c9b 100644 (file)
@@ -2388,6 +2388,11 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
     if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
         Right.Next->is(tok::string_literal))
       return true;
+  } else if (Style.Language == FormatStyle::LK_Cpp ||
+             Style.Language == FormatStyle::LK_ObjC) {
+    if (Left.isStringLiteral() &&
+        (Right.isStringLiteral() || Right.is(TT_ObjCStringLiteral)))
+      return true;
   }
 
   // If the last token before a '}' is a comma or a trailing comment, the
@@ -2411,9 +2416,6 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
            (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
   if (Left.isTrailingComment())
     return true;
-  if (Left.isStringLiteral() &&
-      (Right.isStringLiteral() || Right.is(TT_ObjCStringLiteral)))
-    return true;
   if (Right.Previous->IsUnterminatedLiteral)
     return true;
   if (Right.is(tok::lessless) && Right.Next &&
index 8e33346d630bd15edbd3b39d38a88215a25ec6ca..71821a06499d357170c78f521f1516b1cd5845a9 100644 (file)
@@ -1388,6 +1388,10 @@ TEST_F(FormatTestJS, TemplateStrings) {
                "var y;");
   // Escaped dollar.
   verifyFormat("var x = ` \\${foo}`;\n");
+
+  // The token stream can contain two string_literals in sequence, but that
+  // doesn't mean that they are implicitly concatenated in JavaScript.
+  verifyFormat("var f = `aaaa ${a ? 'a' : 'b'}`;");
 }
 
 TEST_F(FormatTestJS, TemplateStringASI) {