[PATCH] D36139: clang-format: [JS] prefer wrapping chains over empty literals.

Martin Probst via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 1 04:47:09 PDT 2017


mprobst created this revision.
Herald added a subscriber: klimek.

E.g. don't wrap like this:

  (foo.bar.baz).and.bam(Blah.of({
  }))

But rather:

  (foo.bar.baz)
      .and.bam(Blah.of({}))


https://reviews.llvm.org/D36139

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


Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -832,6 +832,15 @@
 
 }
 
+TEST_F(FormatTestJS, DontWrapEmptyLiterals) {
+  verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n"
+               "    .and.returnValue(Observable.of([]));");
+  verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n"
+               "    .and.returnValue(Observable.of({}));");
+  verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n"
+               "    .and.returnValue(Observable.of(()));");
+}
+
 TEST_F(FormatTestJS, InliningFunctionLiterals) {
   FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2005,6 +2005,11 @@
     if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
         (Right.is(TT_TemplateString) && Right.TokenText.startswith("}")))
       return 100;
+    // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
+    if ((Left.is(tok::l_brace) && Right.is(tok::r_brace)) ||
+        (Left.is(tok::l_square) && Right.is(tok::r_square)) ||
+        (Left.is(tok::l_paren) && Right.is(tok::r_paren)))
+      return 200;
   }
 
   if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36139.109082.patch
Type: text/x-patch
Size: 1569 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170801/1c481423/attachment.bin>


More information about the cfe-commits mailing list