Skip to content

Commit 73e7ba4

Browse files
author
exoego
committed
Fix 196
1 parent ea2a7e6 commit 73e7ba4

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ lazy val `scalac-scoverage-runtimeJVM` = runtime.jvm
8282
lazy val `scalac-scoverage-runtimeJS` = runtime.js
8383

8484
lazy val plugin = Project("scalac-scoverage-plugin", file("scalac-scoverage-plugin"))
85-
.dependsOn(`scalac-scoverage-runtimeJVM` % "test")
85+
.dependsOn(`scalac-scoverage-runtimeJS` % "test")
8686
.settings(name := "scalac-scoverage-plugin")
8787
.settings(appSettings: _*)
8888
.settings(

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ 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+
getClass.getClassLoader.loadClass("scala.scalajs.js.Any")
113+
true
114+
} catch {
115+
case _: ClassNotFoundException => false
116+
}
117+
}
118+
110119
def setOptions(options: ScoverageOptions): Unit = {
111120
this.options = options
112121
coverageFilter = new RegexCoverageFilter(options.excludedPackages, options.excludedFiles, options.excludedSymbols)
@@ -215,6 +224,10 @@ class ScoverageInstrumentationComponent(val global: Global, extraAfterPhase: Opt
215224
if (tree.pos.isDefined && !isStatementIncluded(tree.pos)) {
216225
coverage.add(statement.copy(ignored = true))
217226
tree
227+
} else if (isUndefinedParameterInScalaJs(tree.symbol)) {
228+
coverage.add(statement.copy(ignored = true))
229+
statementIds.decrementAndGet()
230+
tree
218231
} else {
219232
coverage.add(statement)
220233

@@ -225,6 +238,11 @@ class ScoverageInstrumentationComponent(val global: Global, extraAfterPhase: Opt
225238
}
226239
}
227240

241+
def isUndefinedParameterInScalaJs(symbol: Symbol): Boolean = {
242+
isScalaJsEnabled && symbol != null && symbol.isSynthetic && symbol.isMethod &&
243+
symbol.nameString.contains("$default$") &&
244+
symbol.tpe.resultType.annotations.exists(_.symbol.nameString == "uncheckedVariance")
245+
}
228246
def isClassIncluded(symbol: Symbol): Boolean = coverageFilter.isClassIncluded(symbol.fullNameString)
229247
def isFileIncluded(source: SourceFile): Boolean = coverageFilter.isFileIncluded(source)
230248
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(2)
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)