]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Understand line breaks in concatenated strings.
authorDaniel Jasper <djasper@google.com>
Thu, 22 May 2014 09:10:04 +0000 (09:10 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 22 May 2014 09:10:04 +0000 (09:10 +0000)
Before:
  var literal = 'hello ' + 'world';

After:
  var literal = 'hello ' +
                'world';

There is no reason to concatenated two string literals with a '+' unless
the line break is intended.

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

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

index f3d655ace5bb67ff10f15ca10f1ade49e90ecdeb..ce847d6427e4bda6385dee3423fd2704191ba93f 100644 (file)
@@ -1622,6 +1622,13 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
       BeforeClosingBrace->isOneOf(tok::comma, tok::comment))
     return true;
 
+  if (Style.Language == FormatStyle::LK_JavaScript) {
+    // FIXME: This might apply to other languages and token kinds.
+    if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
+        Left.Previous->is(tok::char_constant))
+      return true;
+  }
+
   return false;
 }
 
index 33bfe06e5f14a67f54637cf3e010deac1b8d1d68..ecf4e699995d45a131364cb5857cd933af0608e3 100644 (file)
@@ -164,6 +164,11 @@ TEST_F(FormatTestJS, TryCatch) {
                "}");
 }
 
+TEST_F(FormatTestJS, StringLiteralConcatenation) {
+  verifyFormat("var literal = 'hello ' +\n"
+               "              'world';");
+}
+
 TEST_F(FormatTestJS, RegexLiteralClassification) {
   // Regex literals.
   verifyFormat("var regex = /abc/;");