r332968 - clang-format: [JS] do not wrap before any `is`.

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Tue May 22 03:39:08 PDT 2018


Author: mprobst
Date: Tue May 22 03:39:07 2018
New Revision: 332968

URL: http://llvm.org/viewvc/llvm-project?rev=332968&view=rev
Log:
clang-format: [JS] do not wrap before any `is`.

Summary:
`is` type annotations can occur at any nesting level. For example:

    function x() {
      return function y(): a is B { ... };
    }

Breaking before the `is` above breaks TypeScript parsing the code. This
change prevents the wrap.

Reviewers: krasimir

Subscribers: klimek, cfe-commits

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

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=332968&r1=332967&r2=332968&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue May 22 03:39:07 2018
@@ -2958,7 +2958,7 @@ bool TokenAnnotator::canBreakBefore(cons
       return false;
     if (Left.is(TT_JsTypeColon))
       return true;
-    if (Right.NestingLevel == 0 && Right.is(Keywords.kw_is))
+    if (Right.is(Keywords.kw_is))
       return false;
     if (Left.is(Keywords.kw_in))
       return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=332968&r1=332967&r2=332968&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Tue May 22 03:39:07 2018
@@ -1164,7 +1164,15 @@ TEST_F(FormatTestJS, WrapRespectsAutomat
   verifyFormat("await theReckoning;", getGoogleJSStyleWithColumns(10));
   verifyFormat("some['a']['b']", getGoogleJSStyleWithColumns(10));
   verifyFormat("x = (a['a']\n"
-               "      ['b']);", getGoogleJSStyleWithColumns(10));
+               "      ['b']);",
+               getGoogleJSStyleWithColumns(10));
+  verifyFormat("function f() {\n"
+               "  return foo.bar(\n"
+               "      (param): param is {\n"
+               "        a: SomeType\n"
+               "      }&ABC => 1)\n"
+               "}",
+               getGoogleJSStyleWithColumns(25));
 }
 
 TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) {




More information about the cfe-commits mailing list