Bläddra i källkod

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

Summary:
E.g. don't wrap like this:

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

But rather:

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

Reviewers: djasper

Subscribers: klimek, cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@309712 91177308-0d34-0410-b5e6-96231b3b80d8
Martin Probst 8 år sedan
förälder
incheckning
b42a4ce357
2 ändrade filer med 12 tillägg och 0 borttagningar
  1. 3 0
      lib/Format/TokenAnnotator.cpp
  2. 9 0
      unittests/Format/FormatTestJS.cpp

+ 3 - 0
lib/Format/TokenAnnotator.cpp

@@ -2006,6 +2006,9 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
     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.opensScope() && Right.closesScope())
+      return 200;
   }
 
   if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))

+ 9 - 0
unittests/Format/FormatTestJS.cpp

@@ -841,6 +841,15 @@ TEST_F(FormatTestJS, FunctionLiterals) {
 
 }
 
+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;