Repository: scalameta/scalameta
Pull request #2540 (2022-01-04 07:12:02)

ScaladocParser: make CodeExpr/Link punct optional


Patch:
diff --git a/scalameta/trees/shared/src/main/scala/scala/meta/internal/Scaladoc.scala b/scalameta/trees/shared/src/main/scala/scala/meta/internal/Scaladoc.scala
index a9e1f1a17..36fcaa38d 100644
--- a/scalameta/trees/shared/src/main/scala/scala/meta/internal/Scaladoc.scala
+++ b/scalameta/trees/shared/src/main/scala/scala/meta/internal/Scaladoc.scala
@@ -30,7 +30,7 @@ object Scaladoc {
   }
 
   /** A reference to a symbol */
-  final case class Link(ref: String, anchor: Seq[String], punct: String) extends TextPart {
+  final case class Link(ref: String, anchor: Seq[String], punct: String = "") extends TextPart {
     def this(parts: Seq[String], punct: String) = this(parts.head, parts.tail, punct)
     override def syntax: String = {
       val sb = new StringBuilder
@@ -42,7 +42,7 @@ object Scaladoc {
   }
 
   /** A single embedded code expression */
-  final case class CodeExpr(code: String, punct: String) extends TextPart {
+  final case class CodeExpr(code: String, punct: String = "") extends TextPart {
     override def syntax: String = s"{{{$code}}}$punct"
   }
 


Test patch:
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/ScaladocParserSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/ScaladocParserSuite.scala
index 547742b6f..23172af43 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/ScaladocParserSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/ScaladocParserSuite.scala
@@ -19,13 +19,16 @@ class ScaladocParserSuite extends FunSuite {
   ): Unit = {
     val nl = "\n *"
     sb.append(' ').append(tagType.tag)
-    if (tagType.hasLabel) sb.append(" TestLabel")
     if (tagType.optDesc) {
+      if (tagType.hasLabel) sb.append(" TestLabel")
       // another, with description
       sb.append(nl).append(' ').append(tagType.tag)
       if (tagType.hasLabel) sb.append(" TestLabel")
-      sb.append(nl).append("  Test Description")
+      sb.append("  Test Description")
       sb.append(nl).append("  ").append(testStringToMerge)
+    } else {
+      // label with spaces
+      if (tagType.hasLabel) sb.append(" Test Label")
     }
     sb.append(nl)
   }
@@ -131,7 +134,7 @@ class ScaladocParserSuite extends FunSuite {
   test("paragraph parsing with references") {
     val descriptionBody = "Description Body"
     val words = descriptionBody.split("\\s+").toSeq.map(Word.apply)
-    val refNone = Seq(Link("Description", Seq("Body"), ""))
+    val refNone = Seq(Link("Description", Seq("Body")))
     val refDots = Seq(Link("Description", Seq("Body"), "..."))
     val refWithSuffix = Seq(Link("Description", Seq("Body"), "'s"))
     assertEquals(
@@ -226,7 +229,7 @@ class ScaladocParserSuite extends FunSuite {
       Scaladoc(
         Seq(
           Paragraph(
-            Seq(Text((words :+ CodeExpr(codeBlock1, "?")) ++ (words :+ CodeExpr(codeBlock2, ""))))
+            Seq(Text((words :+ CodeExpr(codeBlock1, "?")) ++ (words :+ CodeExpr(codeBlock2))))
           ),
           Paragraph(Seq(Text(words))),
           Paragraph(
@@ -277,7 +280,7 @@ class ScaladocParserSuite extends FunSuite {
     )
 
     val expectation = Option(
-      Scaladoc(Seq(Paragraph(Seq(Text(Seq(CodeExpr(codeBlock1, ""), CodeExpr(codeBlock2, "")))))))
+      Scaladoc(Seq(Paragraph(Seq(Text(Seq(CodeExpr(codeBlock1), CodeExpr(codeBlock2)))))))
     )
     assertEquals(result, expectation)
   }
@@ -1187,40 +1190,37 @@ class ScaladocParserSuite extends FunSuite {
     val parsedScaladocParas = parsedScaladocOpt.get.para
     assertEquals(parsedScaladocParas.length, 1)
     val parsedScaladocTerms = parsedScaladocParas.head.terms
+    val parsedTags = parsedScaladocTerms.collect { case t: Tag => t }
 
+    val tagsWithDesc = TagType.predefined.count(_.optDesc)
     assertEquals(
-      parsedScaladocTerms.count {
-        case t: Tag => t.tag.optDesc && t.desc.isEmpty
-        case _ => false
-      },
-      TagType.predefined.count(_.optDesc)
+      parsedTags.count(t => t.tag.optDesc && t.desc.isEmpty),
+      tagsWithDesc
     )
     assertEquals(
-      parsedScaladocTerms.count {
-        case t: Tag => t.tag.optDesc && t.desc.nonEmpty
-        case _ => false
-      },
-      TagType.predefined.count(_.optDesc)
+      parsedTags.count(t => t.tag.optDesc && t.desc.nonEmpty),
+      tagsWithDesc
     )
     assertEquals(
-      parsedScaladocTerms.count {
-        case t: Tag => !t.tag.optDesc && t.desc.isEmpty
-        case _ => false
-      },
-      TagType.predefined.count(!_.optDesc)
+      parsedTags.count(t => !t.tag.optDesc && t.desc.isEmpty),
+      TagType.predefined.length - tagsWithDesc
     )
     assertEquals(
-      parsedScaladocTerms.count {
-        case t: Tag => !t.tag.optDesc && t.desc.nonEmpty
-        case _ => false
-      },
+      parsedTags.count(t => !t.tag.optDesc && t.desc.nonEmpty),
       0
     )
 
     // Inherit doc does not merge
-    parsedScaladocTerms.foreach {
-      case t: Tag if t.tag.optDesc && t.desc.nonEmpty =>
-        val actual = t.desc.map {
+    parsedTags.foreach { // check label
+      case Tag(t, label, _) if t.hasLabel =>
+        val expected = if (t.optDesc) "TestLabel" else "Test"
+        assertEquals(label, Some(Word(expected)))
+      case _ =>
+    }
+    parsedTags.foreach { // check desc
+      case Tag(t, _, desc) if !t.optDesc => assertEquals(desc, Nil)
+      case Tag(_, _, desc) if desc.nonEmpty =>
+        val actual = desc.map {
           case x: Text => x.parts.takeRight(words.length)
           case x => x
         }