]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Indent expressions in ${} relative to their surrounding
authorDaniel Jasper <djasper@google.com>
Tue, 31 Jan 2017 14:39:33 +0000 (14:39 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 31 Jan 2017 14:39:33 +0000 (14:39 +0000)
This only affects expressions inside ${} scopes of template strings.
Here, we want to indent relative to the surrounding template string and
not the surrounding expression. Otherwise, this can create quite a mess.

Before:
  var f = `
    aaaaaaaaaaaaaaaaaa: ${someFunction(
      aaaaa +  //
      bbbb)}`;

After:
  var f = `
    aaaaaaaaaaaaaaaaaa: ${someFunction(
                              aaaaa +  //
                              bbbb)}`;

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

lib/Format/ContinuationIndenter.cpp
unittests/Format/FormatTestJS.cpp

index 13a6177669138b706b5fb82dfd59b466aab4ec8f..995ff06b540ce97e9f4d6c32ef2e43ad9d731ca0 100644 (file)
@@ -985,12 +985,23 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
     //                       int> v);
     // FIXME: We likely want to do this for more combinations of brackets.
     // Verify that it is wanted for ObjC, too.
-    if (Current.Tok.getKind() == tok::less &&
-        Current.ParentBracket == tok::l_paren) {
+    if (Current.is(tok::less) && Current.ParentBracket == tok::l_paren) {
       NewIndent = std::max(NewIndent, State.Stack.back().Indent);
       LastSpace = std::max(LastSpace, State.Stack.back().Indent);
     }
 
+    // JavaScript template strings are special as we always want to indent
+    // nested expressions relative to the ${}. Otherwise, this can create quite
+    // a mess.
+    if (Current.is(TT_TemplateString)) {
+      unsigned Column = Current.IsMultiline
+                            ? Current.LastLineColumnWidth
+                            : State.Column + Current.ColumnWidth;
+      NewIndent = Column;
+      LastSpace = Column;
+      NestedBlockIndent = Column;
+    }
+
     AvoidBinPacking =
         (State.Line->MustBeDeclaration && !Style.BinPackParameters) ||
         (!State.Line->MustBeDeclaration && !Style.BinPackArguments) ||
index 6cc3bcd16c466aaeabc7c187692e264e8f4257c7..9feda05226c04e5fbff8f70a5c2eab6b26b0a3f9 100644 (file)
@@ -1401,6 +1401,63 @@ TEST_F(FormatTestJS, TemplateStrings) {
                "         aaaaaaaaaaaaa:${  aaaaaaa. aaaaa} aaaaaaaa`;");
 }
 
+TEST_F(FormatTestJS, TemplateStringMultiLineExpression) {
+  verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa +  //\n"
+               "                               bbbb}`;",
+               "var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa +  //\n"
+               "                               bbbb}`;");
+  verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${         //\n"
+               "                               aaaaa +  //\n"
+               "                               bbbb}`;",
+               "var f = `aaaaaaaaaaaaaaaaaa: ${         //\n"
+               "                               aaaaa +  //\n"
+               "                               bbbb}`;");
+  verifyFormat("var f = `\n"
+               "  aaaaaaaaaaaaaaaaaa: ${aaaaa +  //\n"
+               "                        bbbb}`;",
+               "var f  =  `\n"
+               "  aaaaaaaaaaaaaaaaaa: ${   aaaaa  +  //\n"
+               "                        bbbb }`;");
+  verifyFormat("var f = `\n"
+               "  aaaaaaaaaaaaaaaaaa: ${         //\n"
+               "                        aaaaa +  //\n"
+               "                        bbbb}`;",
+               "var f  =  `\n"
+               "  aaaaaaaaaaaaaaaaaa: ${     //\n"
+               "                        aaaaa  +  //\n"
+               "                        bbbb}`  ;");
+  verifyFormat("var f = `\n"
+               "  aaaaaaaaaaaaaaaaaa: ${someFunction(\n"
+               "                            aaaaa +  //\n"
+               "                            bbbb)}`;",
+               "var f  =  `\n"
+               "  aaaaaaaaaaaaaaaaaa: ${ someFunction  (\n"
+               "                            aaaaa  +   //\n"
+               "                            bbbb)}`;");
+  verifyFormat("var f = `\n"
+               "  aaaaaaaaaaaaaaaaaa: ${  //\n"
+               "                        someFunction(\n"
+               "                            aaaaa +  //\n"
+               "                            bbbb)}`;",
+               "var f  =  `\n"
+               "  aaaaaaaaaaaaaaaaaa: ${  //\n"
+               "                        someFunction (\n"
+               "                            aaaaa  +   //\n"
+               "                            bbbb)}`;");
+  verifyFormat("var f = `\n"
+               "  aaaaaaaaaaaaaaaaaa: ${  //\n"
+               "                        someFunction({\n"
+               "                          aaaa: aaaaa,\n"
+               "                          bbbb: bbbbb,\n"
+               "                        })}`;",
+               "var f  =  `\n"
+               "  aaaaaaaaaaaaaaaaaa: ${  //\n"
+               "                        someFunction ({\n"
+               "                          aaaa:  aaaaa,\n"
+               "                          bbbb:  bbbbb,\n"
+               "                        })}`;");
+}
+
 TEST_F(FormatTestJS, TemplateStringASI) {
   verifyFormat("var x = `hello${world}`;", "var x = `hello${\n"
                                            "    world\n"