Skip to content

Commit 09c41a5

Browse files
authored
Make 'appendToComment' work on C-style comments (#165)
Previously, `appendToComment` assumed there was either no existing comment or an existing Javadoc comment. I ran into this situation when compiling Akka with -Yrangepos enabled (hence the test). This change makes `appendToComment` also work when there is an existing C-style comment.
1 parent 4202973 commit 09c41a5

File tree

7 files changed

+49
-8
lines changed

7 files changed

+49
-8
lines changed

plugin/src/main/scala/com/typesafe/genjavadoc/AST.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ trait AST { this: TransformCake ⇒
8585
def maybeSinceDot = if (since.endsWith(".")) " " else ". "
8686
def render = s" * @deprecated ${msg}${maybeDot}Since $since${maybeSinceDot}"
8787

88-
def appendToComment(comment: Seq[String]): Seq[String] = comment match {
89-
case Nil => List("/**", render, "*/")
90-
case c => c.dropRight(1) ++ List(" *", render, "*/")
88+
def appendToComment(comment: Seq[String]): Seq[String] = comment.toVector match {
89+
case init :+ " */" =>
90+
init ++ List(" *", render, " */")
91+
case c =>
92+
c ++ List("/**", render, "*/")
9193
}
9294
}
9395

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package akka.cluster;
2+
public class ClusterRouterGroupSettings {
3+
// not preceding
4+
public ClusterRouterGroupSettings () { throw new RuntimeException(); }
5+
/**
6+
* @deprecated useRole has been replaced with useRoles. Since 2.5.4.
7+
*/
8+
public scala.Option<java.lang.String> useRole () { throw new RuntimeException(); }
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package akka.cluster;
2+
3+
class ClusterRouterGroupSettings {
4+
@deprecated("useRole has been replaced with useRoles", since = "2.5.4")
5+
def useRole: Option[String] = ???
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.typesafe.genjavadoc
2+
3+
import java.io.File
4+
5+
import com.typesafe.genjavadoc.util.CompilerSpec
6+
7+
class RangePosSpec extends CompilerSpec {
8+
/** Sources to compile. */
9+
override def sources: Seq[String] = Seq(
10+
new File("src/test/resources/input/rangepos/akka/cluster/ClusterRouterGroupSettings.scala").getAbsolutePath
11+
)
12+
13+
override def rangepos = true
14+
15+
/** Root directory that contains expected Java output. */
16+
override def expectedPath: String =
17+
new File("src/test/resources/expected_output/rangepos").getAbsolutePath
18+
}

plugin/src/test/scala/com/typesafe/genjavadoc/SignatureSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class SignatureSpec {
5959
val scalac = new GenJavadocCompiler(Seq(
6060
s"genjavadoc:out=$docPath",
6161
"genjavadoc:suppressSynthetic=false"
62-
))
62+
), rangepos = false)
6363

6464
val javaSources = expectedClasses.map{cls =>
6565
docPath + "/" + cls.replace(".", "/") + ".java"

plugin/src/test/scala/com/typesafe/genjavadoc/util/CompilerSpec.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ trait CompilerSpec {
2020
/** Extra plugin arguments. */
2121
def extraSettings: Seq[String] = Seq.empty
2222

23+
/** whether to enable -Yrangepos */
24+
def rangepos: Boolean = false
25+
2326
@Test def compileSourcesAndGenerateExpectedOutput(): Unit = {
2427
val doc = IO.tempDir("java")
2528
val docPath = doc.getAbsolutePath
2629
val defaultSettings = Seq(s"out=$docPath", "suppressSynthetic=false")
2730
val scalac = new GenJavadocCompiler((defaultSettings ++ extraSettings).map{ kv =>
2831
s"genjavadoc:$kv"
29-
})
32+
}, rangepos)
3033

3134
scalac.compile(sources)
3235
assertFalse("Scala compiler reported errors", scalac.reporter.hasErrors)

plugin/src/test/scala/com/typesafe/genjavadoc/util/GenJavaDocCompiler.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import scala.tools.nsc.reporters.ConsoleReporter
77
import scala.tools.nsc.{Global, Settings}
88

99
/** An instance of the Scala compiler with the genjavadoc plugin enabled
10-
* @param params additional parameters to pass to the compiler
10+
* @param pluginOptions additional parameters to pass to the compiler
1111
*/
12-
class GenJavadocCompiler(params: Seq[String]) {
12+
class GenJavadocCompiler(pluginOptions: Seq[String], rangepos: Boolean) {
1313

1414
private val settings = new Settings
15+
16+
settings.Yrangepos.value = rangepos
17+
1518
val reporter = new ConsoleReporter(settings)
1619
private val global = new Global(settings, reporter) {
1720
override protected def loadRoughPluginsList() =
@@ -28,7 +31,7 @@ class GenJavadocCompiler(params: Seq[String]) {
2831
}
2932

3033
settings.outputDirs.setSingleOutput(AbstractFile.getDirectory(target))
31-
settings.pluginOptions.value = params.toList
34+
settings.pluginOptions.value = pluginOptions.toList
3235

3336
def compile(fileNames: Seq[String]): Unit = {
3437
val run = new global.Run

0 commit comments

Comments
 (0)