r284807 - clang-format: [JS] Fix template string ASI.

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 20 22:11:39 PDT 2016


Author: mprobst
Date: Fri Oct 21 00:11:38 2016
New Revision: 284807

URL: http://llvm.org/viewvc/llvm-project?rev=284807&view=rev
Log:
clang-format: [JS] Fix template string ASI.

Summary:
Previously, automatic semicolon insertion would add an unwrapped line
when a template string contained a line break.

    var x = `foo${
        bar}`;

Would be formatted with `bar...` on a separate line and no indent.

Reviewers: djasper

Subscribers: cfe-commits, klimek

Differential Revision: https://reviews.llvm.org/D25675

Modified:
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/unittests/Format/FormatTestJS.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=284807&r1=284806&r2=284807&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Fri Oct 21 00:11:38 2016
@@ -727,6 +727,8 @@ void UnwrappedLineParser::readTokenWithJ
     return;
 
   bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
+  bool PreviousStartsTemplateExpr =
+      Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${");
   if (PreviousMustBeValue && Line && Line->Tokens.size() > 1) {
     // If the token before the previous one is an '@', the previous token is an
     // annotation and can precede another identifier/value.
@@ -737,9 +739,12 @@ void UnwrappedLineParser::readTokenWithJ
   if (Next->is(tok::exclaim) && PreviousMustBeValue)
     addUnwrappedLine();
   bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
-  if (NextMustBeValue && (PreviousMustBeValue ||
-                          Previous->isOneOf(tok::r_square, tok::r_paren,
-                                            tok::plusplus, tok::minusminus)))
+  bool NextEndsTemplateExpr =
+      Next->is(TT_TemplateString) && Next->TokenText.startswith("}");
+  if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
+      (PreviousMustBeValue ||
+       Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
+                         tok::minusminus)))
     addUnwrappedLine();
   if (PreviousMustBeValue && isJSDeclOrStmt(Keywords, Next))
     addUnwrappedLine();

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=284807&r1=284806&r2=284807&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Fri Oct 21 00:11:38 2016
@@ -1276,6 +1276,12 @@ TEST_F(FormatTestJS, TemplateStrings) {
   verifyFormat("var x = ` \\${foo}`;\n");
 }
 
+TEST_F(FormatTestJS, TemplateStringASI) {
+  verifyFormat("var x = `hello${world}`;", "var x = `hello${\n"
+                                           "    world\n"
+                                           "}`;");
+}
+
 TEST_F(FormatTestJS, NestedTemplateStrings) {
   verifyFormat(
       "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;");




More information about the cfe-commits mailing list