]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] avoid line breaks before unindented r_parens.
authorMartin Probst <martin@probst.io>
Mon, 22 May 2017 14:58:26 +0000 (14:58 +0000)
committerMartin Probst <martin@probst.io>
Mon, 22 May 2017 14:58:26 +0000 (14:58 +0000)
The change that enabled wrapping at the previous scope's indentation had
unintended side-effects in that clang-format would prefer to wrap
closing parentheses to the next line if it avoided a wrap on the next
line (assuming very narrow lines):

    fooObject
        .someCall(barbazbam)
        .then(bam);

Would get formatted as:

    fooObject.someCall(barbazbam
    ).then(bam);

Because the ')' is now indented at the parent level (fooObject).

Normally formatting a builder pattern style call sequence like that is
outlawed in clang-format anyway. However for JavaScript this is special
cased to support trailing .bind calls.

This change disallows this special case when following a closing ')' to
avoid the problem.

Included are some random comment fixes.

Reviewers: djasper

Subscribers: klimek, cfe-commits

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

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

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

index e99193791f06f250772ab695b248dca2fa206855..006a9710148fe76cb5526b6eb22c455590dcd122 100644 (file)
@@ -207,7 +207,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
       //     ...
       //   }.bind(...));
       // FIXME: We should find a more generic solution to this problem.
-      !(State.Column <= NewLineColumn &&
+      !(State.Column <= NewLineColumn && Previous.isNot(tok::r_paren) &&
         Style.Language == FormatStyle::LK_JavaScript))
     return true;
 
index 69e2d43014ef4a5c5da4e35ec051076feaf2869a..79f438eb0f88fb9904d0a3bc8983e397a9489c61 100644 (file)
@@ -2570,7 +2570,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
             Keywords.kw_interface, Keywords.kw_type, tok::kw_static,
             tok::kw_public, tok::kw_private, tok::kw_protected,
             Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set))
-      return false; // Otherwise a semicolon is inserted.
+      return false; // Otherwise automatic semicolon insertion would trigger.
     if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace))
       return false;
     if (Left.is(TT_JsTypeColon))
index c36b4359c29b24fdccf2ed5107ca1e4573345f43..ba6ff023608513f1f3e4910d47b238636488ddd5 100644 (file)
@@ -1823,6 +1823,11 @@ TEST_F(FormatTestJS, NonNullAssertionOperator) {
   verifyFormat("let x = !foo;\n");
   verifyFormat("let x = foo[0]!;\n");
   verifyFormat("let x = (foo)!;\n");
+  verifyFormat("let x = x(foo!);\n");
+  verifyFormat(
+      "a.aaaaaa(a.a!).then(\n"
+      "    x => x(x));\n",
+      getGoogleJSStyleWithColumns(20));
   verifyFormat("let x = foo! - 1;\n");
   verifyFormat("let x = {foo: 1}!;\n");
   verifyFormat(