]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] strict prop init annotation.
authorMartin Probst <martin@probst.io>
Mon, 11 Jun 2018 16:20:13 +0000 (16:20 +0000)
committerMartin Probst <martin@probst.io>
Mon, 11 Jun 2018 16:20:13 +0000 (16:20 +0000)
Summary:
TypeScript uses the `!` token for strict property initialization
assertions, as in:

    class X {
      strictPropAsserted!: string;
    }

Previously, clang-format would wrap between the `!` and the `:` for
overly long lines. This patch fixes that by generally preventing the
wrap in that location.

Reviewers: krasimir

Subscribers: cfe-commits

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

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

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

index 0a026159a0ec91669361ed5cf221f6d2cbd4bdd5..5dce9d248a9605caa452810b00da4f042bdd6d5e 100644 (file)
@@ -2981,7 +2981,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
       // We deal with this case later by detecting an entry
       // following a closing paren of this submessage.
     }
-    
+
     // If this is an entry immediately following a submessage, it will be
     // preceded by a closing paren of that submessage, like in:
     //     left---.  .---right
@@ -3027,6 +3027,9 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
       return false;
     if (Left.is(TT_JsTypeColon))
       return true;
+    // Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
+    if (Left.is(tok::exclaim) && Right.is(tok::colon))
+      return false;
     if (Right.is(Keywords.kw_is))
       return false;
     if (Left.is(Keywords.kw_in))
index 1d6105557781ea3f02e253c35b04a78d3906f42e..9975b7d31125320a997d0378c42ead22d0882643 100644 (file)
@@ -1540,6 +1540,15 @@ TEST_F(FormatTestJS, ClassDeclarations) {
                "}");
 }
 
+TEST_F(FormatTestJS, StrictPropInitWrap) {
+  const FormatStyle &Style = getGoogleJSStyleWithColumns(22);
+  verifyFormat("class X {\n"
+               "  strictPropInitField!:\n"
+               "      string;\n"
+               "}",
+               Style);
+}
+
 TEST_F(FormatTestJS, InterfaceDeclarations) {
   verifyFormat("interface I {\n"
                "  x: string;\n"