typelevel / typelevel/scalac-options

Warning configuration DSL

Open
#4 1 comment 2 reactions 1 assignee View on GitHub

@satorg is already working on this.

Since Sep 18, 2022.

Dominant language
Scala
Stars
48
Forks
17
Avg merge
5h 6m
Merged PRs (30d)
4

Description

Hi! I have defined this little DSL for warnings configuration and thought of sharing it. It's pretty hard to remember all available filters, their short names, which actions have configurable verbosity, etc. and then piece them correctly together into a single string. With a DSL it's much easier to discover what's available via auto-complete, see full names and have a clear, readable configuration definition.

sealed trait WarningAction

case object silent extends WarningAction
case object error extends WarningAction

sealed trait WarningActionWithVerbosity extends WarningAction { action =>

  def summary: WarningAction = new WarningAction {
    override def toString = s"${action}-summary"
  }

  def verbose: WarningAction = new WarningAction {
    override def toString = s"${action}-verbose"
  }

}

case object warning extends WarningActionWithVerbosity
case object info extends WarningActionWithVerbosity

sealed class WarningFilter(val asString: String) {
  override def toString = asString

  def &(that: WarningFilter): WarningFilter = new WarningFilter(
    Seq(this.asString, that.asString).mkString("&")
  )

}

case object any extends WarningFilter("any")
case class cat(cat: String) extends WarningFilter(s"cat=${cat}")
case class msg(regex: String) extends WarningFilter(s"msg=${regex}")
case class src(regex: String) extends WarningFilter(s"src=${regex}")

case class origin(segments: String*)
    extends WarningFilter(s"origin=${segments.mkString(raw"\.")}")

case class WarningConfig(val rules: (WarningFilter, WarningAction)*) {

  override def toString = {
    val conf = rules.toSeq
      .map { case (filter, action) => Seq(filter, action).mkString(":") }
      .mkString(",")
    s"-Wconf:${conf}"
  }

  def asScalacOption =
    ScalacOption(
      option = this.toString,
      isSupported = {
        case ScalaVersion(2, 12, x) => x >= 13
        case ScalaVersion(2, 13, x) => x >= 2
        case _                      => false
      },
    )

}

Example usage:

WarningConfig(
  src("src_managed/.*") -> silent,
  cat("unused-imports") -> warning,
  (cat("deprecation") & origin("scala", ".*")) -> error,
  cat("deprecation") -> warning.verbose,
  cat("unused") -> warning.summary,
  any -> error,
)

is transformed into -Wconf:src=src_managed/.*:silent,cat=unused-imports:warning,cat=deprecation&origin=scala\..*:error,cat=deprecation:warning-verbose,cat=unused:warning-summary,any:error or with line breaks for readability:

-Wconf:
src=src_managed/.*:silent,
cat=unused-imports:warning,
cat=deprecation&origin=scala\..*:error,
cat=deprecation:warning-verbose,
cat=unused:warning-summary,
any:error

Would you be open to a pull request adding such DSL here? Details of the design are open for discussion, of course.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.