]> granicus.if.org Git - clang/commitdiff
clang-format: [TypeScript] Fix bug in handling of non-null operator.
authorDaniel Jasper <djasper@google.com>
Wed, 9 Nov 2016 14:12:55 +0000 (14:12 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 9 Nov 2016 14:12:55 +0000 (14:12 +0000)
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

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

index cba9a03b3e0d400025dfd0796e2a244fd295de90..bba803dc3ea7f11a668d7353ef2deebc5d24321d 100644 (file)
@@ -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.
index f4b50591aae6c9891e2ce02e0d0e5fcc5a231ecd..764159882c23263e1cd0d83d96ad2bda05d9e136 100644 (file)
@@ -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");
 }