Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin option for global filters #14

Merged
merged 3 commits into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ val saveTestClasspath = taskKey[File](

val commonSettings = Seq(
organization := "com.github.ghik",
version := "1.1",
version := "1.2-SNAPSHOT",
scalaVersion := "2.12.6",
crossScalaVersions := Seq("2.11.12", "2.12.6", "2.13.0-M4"),
crossScalaVersions := Seq("2.11.12", scalaVersion.value, "2.13.0-M4"),
projectInfo := ModuleInfo(
nameFormal = "Silencer",
description = "Scala compiler plugin for annotation-based warning suppression",
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.1.5
sbt.version=1.2.1
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
package com.github.ghik.silencer

import scala.util.matching.Regex

import scala.reflect.internal.util.Position
import scala.tools.nsc.plugins.{Plugin, PluginComponent}
import scala.tools.nsc.{Global, Phase}

class SilencerPlugin(val global: Global) extends Plugin { plugin =>
val name = "SilencerPlugin"
val name = "silencer"
val description = "Scala compiler plugin for warning suppression"
val components: List[PluginComponent] = List(component)
private var globalFilters = List.empty[Regex]

private lazy val reporter =
new SuppressingReporter(global.reporter, globalFilters)

override def processOptions(options: List[String], error: String => Unit): Unit = {
options.foreach { opt =>
if (opt startsWith "globalFilters=") {
globalFilters = opt.drop(14).split(";").map(_.r).toList
}
}

global.inform(s"""Silencer using global filters: ${globalFilters.mkString(",")}""")

global.reporter = reporter
}

private val reporter = new SuppressingReporter(global.reporter)
global.reporter = reporter
override val optionsHelp: Option[String] = Some(
" -P:silencer:globalFilters=... Semi-colon separated patterns to filter the warning messages")

private object component extends PluginComponent {
val global: plugin.global.type = plugin.global
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.github.ghik.silencer

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.util.matching.Regex

import scala.collection.mutable, mutable.ArrayBuffer

import scala.reflect.internal.util.{Position, SourceFile}
import scala.tools.nsc.reporters.Reporter

class SuppressingReporter(original: Reporter) extends Reporter {
class SuppressingReporter(original: Reporter, globalFilters: List[Regex]) extends Reporter {

private val deferredWarnings = new mutable.HashMap[SourceFile, ArrayBuffer[(Position, String)]]
private val suppressedRanges = new mutable.HashMap[SourceFile, List[Position]]

Expand All @@ -28,6 +31,8 @@ class SuppressingReporter(original: Reporter) extends Reporter {
severity match {
case INFO =>
original.info(pos, msg, force)
case WARNING if globalFilters.exists(_.findFirstIn(msg).isDefined) =>
()
case WARNING if !pos.isDefined =>
original.warning(pos, msg)
case WARNING if !suppressedRanges.contains(pos.source) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ class SilencerPluginTest extends FunSuite { suite =>

val testdata = "silencer-plugin/testdata/"
val settings = new Settings

settings.deprecation.value = true


settings.pluginOptions.value = settings.pluginOptions.value :+ (
"silencer:globalFilters=depreFunc1\\ in\\ object\\ globallyFiltered\\ is\\ deprecated;useless.*filter")

Option(getClass.getResourceAsStream("/embeddedcp")) match {
case Some(is) =>
Source.fromInputStream(is).getLines().foreach(settings.classpath.append)
Expand Down Expand Up @@ -68,10 +73,12 @@ class SilencerPluginTest extends FunSuite { suite =>
test("macro expandee") {
testFile("macroExpandeeSuppression.scala", 1)
}
test("global filters") {
testFile("globallyFiltered.scala", 1)
}
test("multiple files compilation") {
val files = new File(testdata).listFiles().filter(_.isFile).map(_.getName)
compile(files: _*)
assertWarnings(files.length)
}

}
1 change: 0 additions & 1 deletion silencer-plugin/testdata/classSuppression.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package testdata

import com.github.ghik.silencer.silent


object classSuppression {
@silent
class suppressed {
Expand Down
15 changes: 15 additions & 0 deletions silencer-plugin/testdata/globallyFiltered.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package testdata

object globallyFiltered {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please write this test so that there's one warning suppressed and one similar warning unsuppressed (e.g. one that doesn't match the regex). This way we test if silencer only suppresses exactly what we want it to suppress and we also avoid changing the multiple files compilation test.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

class notSuppressed {
def method1(): Unit = depreFunc1() // global filter ignore this one

def method2(): Unit = depreFunc2() // not suppressed, no filter
}

@deprecated("", "")
private def depreFunc1() = print("ignored")

@deprecated("", "")
private def depreFunc2() = print("warning")
}