Skip to content

Commit e797a38

Browse files
exoegoexoego
exoego
authored andcommitted
Fix 196
1 parent d0c89c3 commit e797a38

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

scalac-scoverage-plugin/src/main/scala/scoverage/plugin.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ class ScoverageInstrumentationComponent(val global: Global, extraAfterPhase: Opt
107107
private var options: ScoverageOptions = new ScoverageOptions()
108108
private var coverageFilter: CoverageFilter = AllCoverageFilter
109109

110+
private val isScalaJsEnabled: Boolean = {
111+
try {
112+
rootMirror.getClassIfDefined("scala.scalajs.js.Any") != NoSymbol
113+
} catch {
114+
case _: Throwable => false
115+
}
116+
}
117+
110118
def setOptions(options: ScoverageOptions): Unit = {
111119
this.options = options
112120
coverageFilter = new RegexCoverageFilter(options.excludedPackages, options.excludedFiles, options.excludedSymbols)
@@ -215,6 +223,10 @@ class ScoverageInstrumentationComponent(val global: Global, extraAfterPhase: Opt
215223
if (tree.pos.isDefined && !isStatementIncluded(tree.pos)) {
216224
coverage.add(statement.copy(ignored = true))
217225
tree
226+
} else if (isUndefinedParameterInScalaJs(tree.symbol)) {
227+
coverage.add(statement.copy(ignored = true))
228+
statementIds.decrementAndGet()
229+
tree
218230
} else {
219231
coverage.add(statement)
220232

@@ -225,6 +237,11 @@ class ScoverageInstrumentationComponent(val global: Global, extraAfterPhase: Opt
225237
}
226238
}
227239

240+
def isUndefinedParameterInScalaJs(symbol: Symbol): Boolean = {
241+
isScalaJsEnabled && symbol != null && symbol.isSynthetic && symbol.isMethod &&
242+
symbol.nameString.contains("$default$") &&
243+
symbol.tpe.resultType.annotations.exists(_.symbol.nameString == "uncheckedVariance")
244+
}
228245
def isClassIncluded(symbol: Symbol): Boolean = coverageFilter.isClassIncluded(symbol.fullNameString)
229246
def isFileIncluded(source: SourceFile): Boolean = coverageFilter.isFileIncluded(source)
230247
def isStatementIncluded(pos: Position): Boolean = coverageFilter.isLineIncluded(pos)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package scoverage
2+
3+
import org.scalatest.{ BeforeAndAfterEachTestData, FunSuite, OneInstancePerTest }
4+
5+
/**
6+
* https://github.com/scoverage/scalac-scoverage-plugin/issues/196
7+
*/
8+
class PluginCoverageScalaJsTest
9+
extends FunSuite
10+
with OneInstancePerTest
11+
with BeforeAndAfterEachTestData
12+
with MacroSupport {
13+
14+
test("scoverage should ignore default undefined parameter") {
15+
val compiler = ScoverageCompiler.default
16+
compiler.compileCodeSnippet(
17+
"""import scala.scalajs.js
18+
|
19+
|object JSONHelper {
20+
| def toJson(value: String): String = js.JSON.stringify(value)
21+
|}""".stripMargin)
22+
assert(!compiler.reporter.hasErrors)
23+
compiler.assertNMeasuredStatements(4)
24+
}
25+
}

scalac-scoverage-plugin/src/test/scala/scoverage/ScoverageCompiler.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package scoverage
33
import java.io.{File, FileNotFoundException}
44
import java.net.URL
55

6+
import scoverage.ScoverageCompiler.getScalaJsJars
7+
68
import scala.collection.mutable.ListBuffer
79
import scala.tools.nsc.{Settings, Global}
810
import scala.tools.nsc.plugins.PluginComponent
@@ -17,7 +19,7 @@ object ScoverageCompiler {
1719
case _ => ScalaVersion
1820
}
1921

20-
def classPath = getScalaJars.map(_.getAbsolutePath) :+ sbtCompileDir.getAbsolutePath :+ runtimeClasses.getAbsolutePath
22+
def classPath = (getScalaJars ++ getScalaJsJars ++ List(sbtCompileDir, runtimeClasses)).map(_.getAbsolutePath)
2123

2224
def settings: Settings = {
2325
val s = new scala.tools.nsc.Settings
@@ -48,6 +50,11 @@ object ScoverageCompiler {
4850
scalaJars.map(findScalaJar)
4951
}
5052

53+
private def getScalaJsJars: List[File] = {
54+
val scalaJars = List("scalajs-library")
55+
scalaJars.map(findScalaJsJar)
56+
}
57+
5158
private def sbtCompileDir: File = {
5259
val dir = new File(s"./scalac-scoverage-plugin/target/scala-$ShortScalaVersion/classes")
5360
if (!dir.exists)
@@ -59,6 +66,8 @@ object ScoverageCompiler {
5966

6067
private def findScalaJar(artifactId: String): File = findIvyJar("org.scala-lang", artifactId, ScalaVersion)
6168

69+
private def findScalaJsJar(artifactId: String): File = findIvyJar(s"org.scala-js", s"${artifactId}_${ShortScalaVersion}", "0.6.28")
70+
6271
private def findIvyJar(groupId: String, artifactId: String, version: String, packaging: String = "jar"): File = {
6372
val userHome = System.getProperty("user.home")
6473
val jarPath = s"$userHome/.ivy2/cache/$groupId/$artifactId/${packaging}s/$artifactId-$version.jar"

0 commit comments

Comments
 (0)