From: Martin Probst Date: Mon, 13 Jun 2016 00:49:54 +0000 (+0000) Subject: clang-format: [JS] post-fix non-null assertion operator. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9c594c448bc19d54a9ec3b541c84b7b579cecfe9;p=clang clang-format: [JS] post-fix non-null assertion operator. Summary: Do not insert whitespace preceding the "!" postfix operator. This is an incomplete fix, but should cover common usage. Reviewers: djasper Subscribers: cfe-commits, klimek Differential Revision: http://reviews.llvm.org/D21204 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@272524 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index b469cfad2b..13bda71127 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -2126,6 +2126,11 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, // 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, tok::r_brace) || + 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; diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 3d9677922c..ffcc34d971 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -1293,5 +1293,14 @@ TEST_F(FormatTestJS, SupportShebangLines) { "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[0]!;\n"); + verifyFormat("let x = (foo)!;\n"); + verifyFormat("let x = {foo: 1}!;\n"); +} + } // end namespace tooling } // end namespace clang