Skip to content

Commit 7cfce96

Browse files
authored
feat: Support ignored files configuration on Scala 3 (#954)
1 parent b3782d6 commit 7cfce96

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/main/scala-3/com/sksamuel/scapegoat/Plugin.scala

+8-3
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ class ScapegoatPlugin extends StandardPlugin {
2828

2929
}
3030

31-
class ScapegoatPhase(var configuration: Configuration, override val inspections: Seq[Inspection])
31+
class ScapegoatPhase(val configuration: Configuration, override val inspections: Seq[Inspection])
3232
extends PluginPhase
3333
with ScapegoatBasePlugin {
3434

3535
private[scapegoat] var feedback: Option[FeedbackDotty] = None
3636

37+
private val ignorePatterns = configuration.ignoredFiles.map(_.r)
38+
3739
override def phaseName: String = "scapegoat"
3840

3941
override val runsAfter: Set[String] = Set(TyperPhase.name)
@@ -92,8 +94,11 @@ class ScapegoatPhase(var configuration: Configuration, override val inspections:
9294
}
9395

9496
override def transformUnit(tree: tpd.Tree)(using ctx: Context): tpd.Tree = {
95-
activeInspections.foreach { inspection =>
96-
feedback.foreach(f => inspection.inspect(f, tree))
97+
val sourcePath = ctx.compilationUnit.source.path
98+
if (!ignorePatterns.exists(_.matches(sourcePath))) {
99+
activeInspections.foreach { inspection =>
100+
feedback.foreach(f => inspection.inspect(f, tree))
101+
}
97102
}
98103
tree
99104
}

src/test/scala-3/com/sksamuel/scapegoat/DottyRunner.scala

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class DottyRunner(
5959
val inspection: Class[? <: Inspection],
6060
reports: String = "none",
6161
disabledInspections: List[String] = List("none"),
62+
ignoredFiles: List[String] = List.empty,
6263
createDataDir: () => File = DottyRunner.createTempDataDir
6364
) extends Driver {
6465

@@ -82,6 +83,7 @@ class DottyRunner(
8283
"-Xplugin-require:scapegoat",
8384
"-P:scapegoat:enabledInspections:" + inspection.getSimpleName,
8485
"-P:scapegoat:disabledInspections:" + disabledInspections.mkString(":"),
86+
"-P:scapegoat:ignoredFiles:" + ignoredFiles.mkString(":"),
8587
"-P:scapegoat:verbose:true",
8688
s"-P:scapegoat:reports:$reports",
8789
s"-P:scapegoat:dataDir:${targetDir.getAbsolutePath}",

src/test/scala-3/com/sksamuel/scapegoat/ScapegoatPhaseTest.scala

+23
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ScapegoatPhaseTest extends AnyFreeSpec with should.Matchers {
1414
classOf[OptionGet],
1515
report,
1616
if (disabledAll) List("all") else List("none"),
17+
List.empty,
1718
() => targetFolder
1819
)
1920
val _ = dotty.compileCodeSnippet("class Test {}")
@@ -32,6 +33,28 @@ class ScapegoatPhaseTest extends AnyFreeSpec with should.Matchers {
3233
outputDir.listFiles(reportFilter) should contain theSameElementsAs (Array.empty[File])
3334
}
3435

36+
"ignore files" - {
37+
38+
def runDottyWithIgnore(patterns: List[String]) = {
39+
val dotty = new DottyRunner(classOf[OptionGet], "html", List("none"), patterns)
40+
dotty.compileCodeSnippet("""
41+
class Test {
42+
val o = Option("sammy")
43+
o.get
44+
}""".stripMargin)
45+
}
46+
47+
"should report issue" in {
48+
val feedback = runDottyWithIgnore(List.empty)
49+
feedback.errors.size shouldBe 1
50+
}
51+
52+
"should not report issue" in {
53+
val feedback = runDottyWithIgnore(List(".*scapegoat_snippet.*\\.scala$"))
54+
feedback.errors.size shouldBe 0
55+
}
56+
}
57+
3558
"generate reports" - {
3659
"should generate html report" in {
3760
val outputDir = runDotty("html")

0 commit comments

Comments
 (0)