[PATCH] D91078: [clang-format] do not break before @tags in JS comments

Krasimir Georgiev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 9 06:57:12 PST 2020


krasimir created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
krasimir requested review of this revision.

In JavaScript breaking before a `@tag` in a comment puts it on a new line, and
machinery that parses these comments will fail to understand such comments.

This adapts clang-format to not break before `@`. Similar functionality exists
for not breaking before `{`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91078

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


Index: clang/unittests/Format/FormatTestJS.cpp
===================================================================
--- clang/unittests/Format/FormatTestJS.cpp
+++ clang/unittests/Format/FormatTestJS.cpp
@@ -187,22 +187,11 @@
             format("/** @returns j */", getGoogleJSStyleWithColumns(20)));
 
   // Break a single line long jsdoc comment pragma.
-  EXPECT_EQ("/**\n"
-            " * @returns {string} jsdoc line 12\n"
-            " */",
-            format("/** @returns {string} jsdoc line 12 */",
-                   getGoogleJSStyleWithColumns(20)));
   EXPECT_EQ("/**\n"
             " * @returns {string}\n"
             " *     jsdoc line 12\n"
             " */",
             format("/** @returns {string} jsdoc line 12 */",
-                   getGoogleJSStyleWithColumns(25)));
-
-  EXPECT_EQ("/**\n"
-            " * @returns {string} jsdoc line 12\n"
-            " */",
-            format("/** @returns {string} jsdoc line 12  */",
                    getGoogleJSStyleWithColumns(20)));
 
   // FIXME: this overcounts the */ as a continuation of the 12 when breaking.
@@ -2194,6 +2183,16 @@
                  " */",
                  getGoogleJSStyleWithColumns(ColumnLimit));
   }
+  // don't break before @tags
+  verifyFormat("/**\n"
+               " * This\n"
+               " * tag @param\n"
+               " * stays.\n"
+               " */",
+               "/**\n"
+               " * This tag @param stays.\n"
+               " */",
+               getGoogleJSStyleWithColumns(13));
   verifyFormat("/**\n"
                " * @see http://very/very/long/url/is/long\n"
                " */",
Index: clang/lib/Format/BreakableToken.cpp
===================================================================
--- clang/lib/Format/BreakableToken.cpp
+++ clang/lib/Format/BreakableToken.cpp
@@ -124,9 +124,10 @@
       continue;
     }
 
-    // Avoid ever breaking before a { in JavaScript.
+    // Avoid ever breaking before a @tag or a { in JavaScript.
     if (Style.Language == FormatStyle::LK_JavaScript &&
-        SpaceOffset + 1 < Text.size() && Text[SpaceOffset + 1] == '{') {
+        SpaceOffset + 1 < Text.size() &&
+        (Text[SpaceOffset + 1] == '{' || Text[SpaceOffset + 1] == '@')) {
       SpaceOffset = Text.find_last_of(Blanks, SpaceOffset);
       continue;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91078.303862.patch
Type: text/x-patch
Size: 2319 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201109/7084be43/attachment.bin>


More information about the cfe-commits mailing list