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.

llvm-svn: 209413

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

index f3d655a..ce847d6 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 33bfe06..ecf4e69 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/;");