From: Daniel Jasper Date: Wed, 9 Nov 2016 14:12:55 +0000 (+0000) Subject: clang-format: [TypeScript] Fix bug in handling of non-null operator. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d3859bc2da6c4578acf232bf948f91d4f3d3a926;p=clang clang-format: [TypeScript] Fix bug in handling of non-null operator. Before: var i = x!-1; After: var i = x! - 1; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@286367 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index cba9a03b3e..bba803dc3e 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1311,7 +1311,13 @@ private: TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) { const FormatToken *PrevToken = Tok.getPreviousNonComment(); - if (!PrevToken || PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator)) + if (!PrevToken) + return TT_UnaryOperator; + + if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator) && + !PrevToken->is(tok::exclaim)) + // There aren't any trailing unary operators except for TypeScript's + // non-null operator (!). Thus, this must be squence of leading operators. return TT_UnaryOperator; // Use heuristics to recognize unary operators. diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index f4b50591aa..764159882c 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -1462,6 +1462,7 @@ TEST_F(FormatTestJS, NonNullAssertionOperator) { verifyFormat("let x = !foo;\n"); verifyFormat("let x = foo[0]!;\n"); verifyFormat("let x = (foo)!;\n"); + verifyFormat("let x = foo! - 1;\n"); verifyFormat("let x = {foo: 1}!;\n"); }