[clang] 77b4841 - [clang-format] do not break before @tags in JS comments

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 03:31:22 PST 2020


Author: Krasimir Georgiev
Date: 2020-11-11T12:27:15+01:00
New Revision: 77b484116971337c9584fe101cdf1bca7f07f4dd

URL: https://github.com/llvm/llvm-project/commit/77b484116971337c9584fe101cdf1bca7f07f4dd
DIFF: https://github.com/llvm/llvm-project/commit/77b484116971337c9584fe101cdf1bca7f07f4dd.diff

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

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 `{`.

Reviewed By: mprobst

Differential Revision: https://reviews.llvm.org/D91078

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp
index c3853687c228..4975c89164a4 100644
--- a/clang/lib/Format/BreakableToken.cpp
+++ b/clang/lib/Format/BreakableToken.cpp
@@ -124,9 +124,10 @@ getCommentSplit(StringRef Text, unsigned ContentStartColumn,
       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;
     }

diff  --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 84d76c67764a..8963c9f19024 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -187,22 +187,11 @@ TEST_F(FormatTestJS, JSDocComments) {
             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 @@ TEST_F(FormatTestJS, JSDocAnnotations) {
                  " */",
                  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"
                " */",


        


More information about the cfe-commits mailing list