r209413 - clang-format: [JS] Understand line breaks in concatenated strings.

Daniel Jasper djasper at google.com
Thu May 22 02:10:09 PDT 2014


Author: djasper
Date: Thu May 22 04:10:04 2014
New Revision: 209413

URL: http://llvm.org/viewvc/llvm-project?rev=209413&view=rev
Log:
clang-format: [JS] Understand line breaks in concatenated strings.

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.

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=209413&r1=209412&r2=209413&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu May 22 04:10:04 2014
@@ -1622,6 +1622,13 @@ bool TokenAnnotator::mustBreakBefore(con
       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;
 }
 

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=209413&r1=209412&r2=209413&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Thu May 22 04:10:04 2014
@@ -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/;");





More information about the cfe-commits mailing list