[PATCH] D21204: clang-format: [JS] post-fix non-null assertion operator.

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 9 22:08:24 PDT 2016


mprobst updated this revision to Diff 60308.
mprobst marked an inline comment as done.
mprobst added a comment.

- invert check to whitelist known tokens that can precede a non-null assertion.


http://reviews.llvm.org/D21204

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

Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -1291,5 +1291,12 @@
                "var x   =  hello();");
 }
 
+TEST_F(FormatTestJS, NonNullAssertionOperator) {
+  verifyFormat("let x = foo!.bar();\n");
+  verifyFormat("let x = foo ? bar! : baz;\n");
+  verifyFormat("let x = !foo;\n");
+  verifyFormat("let x = foo!;\n");
+}
+
 } // end namespace tooling
 } // end namespace clang
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2123,6 +2123,11 @@
       // locations that should have whitespace following are identified by the
       // above set of follower tokens.
       return false;
+    // Postfix non-null assertion operator, as in `foo!.bar()`.
+    if (Right.is(tok::exclaim) &&
+        (Left.isOneOf(tok::identifier, tok::r_paren, tok::r_square) ||
+         Left.Tok.isLiteral()))
+      return false;
   } else if (Style.Language == FormatStyle::LK_Java) {
     if (Left.is(tok::r_square) && Right.is(tok::l_brace))
       return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21204.60308.patch
Type: text/x-patch
Size: 1222 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160610/530c6f23/attachment.bin>


More information about the cfe-commits mailing list