[PATCH] D108620: [clang-format] keep TypeScript argument decorators in line

Krasimir Georgiev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 24 03:38:13 PDT 2021


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGbefb9dc3694e: [clang-format] keep TypeScript argument decorators in line (authored by krasimir).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108620/new/

https://reviews.llvm.org/D108620

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/unittests/Format/FormatTestJS.cpp


Index: clang/unittests/Format/FormatTestJS.cpp
===================================================================
--- clang/unittests/Format/FormatTestJS.cpp
+++ clang/unittests/Format/FormatTestJS.cpp
@@ -701,8 +701,12 @@
                    getGoogleJSStyleWithColumns(20)));
 }
 
-TEST_F(FormatTestJS, FormatsDecoratedFunctions) {
-  // Regression test: ensure that there is a break before `get`.
+TEST_F(FormatTestJS, FormatsDecorators) {
+  // No line break after argument decorators.
+  verifyFormat("class A {\n"
+               "  constructor(@arg(DECOR) private arg: Type) {}\n"
+               "}");
+  // Ensure that there is a break before functions, getters and setters.
   EXPECT_EQ("class A {\n"
             "  private p = () => {}\n"
             "\n"
@@ -710,6 +714,24 @@
             "  get f() {\n"
             "    return result;\n"
             "  }\n"
+            "}\n"
+            "\n"
+            "class B {\n"
+            "  private p = () => {}\n"
+            "\n"
+            "  @decorated('a')\n"
+            "  set f() {\n"
+            "    return result;\n"
+            "  }\n"
+            "}\n"
+            "\n"
+            "class C {\n"
+            "  private p = () => {}\n"
+            "\n"
+            "  @decorated('a')\n"
+            "  function f() {\n"
+            "    return result;\n"
+            "  }\n"
             "}",
             format("class A {\n"
                    "  private p = () => {}\n"
@@ -718,6 +740,24 @@
                    "  get f() {\n"
                    "    return result;\n"
                    "  }\n"
+                   "}\n"
+                   "\n"
+                   "class B {\n"
+                   "  private p = () => {}\n"
+                   "\n"
+                   "  @decorated('a')\n"
+                   "  set f() {\n"
+                   "    return result;\n"
+                   "  }\n"
+                   "}\n"
+                   "\n"
+                   "class C {\n"
+                   "  private p = () => {}\n"
+                   "\n"
+                   "  @decorated('a')\n"
+                   "  function f() {\n"
+                   "    return result;\n"
+                   "  }\n"
                    "}",
                    getGoogleJSStyleWithColumns(50)));
 }
Index: clang/lib/Format/ContinuationIndenter.cpp
===================================================================
--- clang/lib/Format/ContinuationIndenter.cpp
+++ clang/lib/Format/ContinuationIndenter.cpp
@@ -19,6 +19,7 @@
 #include "clang/Basic/OperatorPrecedence.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Format/Format.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/Support/Debug.h"
 
 #define DEBUG_TYPE "format-indenter"
@@ -492,8 +493,12 @@
       return true;
   }
 
-  // Break after the closing parenthesis of TypeScript decorators.
+  // Break after the closing parenthesis of TypeScript decorators before
+  // functions, getters and setters.
+  static const llvm::StringSet<> BreakBeforeDecoratedTokens = {"get", "set",
+                                                               "function"};
   if (Style.Language == FormatStyle::LK_JavaScript &&
+      BreakBeforeDecoratedTokens.contains(Current.TokenText) &&
       Previous.is(tok::r_paren) && Previous.is(TT_JavaAnnotation)) {
     return true;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108620.368314.patch
Type: text/x-patch
Size: 3343 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210824/3fcf9fa1/attachment.bin>


More information about the cfe-commits mailing list