Skip to content

Commit

Permalink
Remove LintID.owner.
Browse files Browse the repository at this point in the history
Instead of using LintID.owner, the RewriteCtx.printMessages takes an
`owner: RewriteName` argument. This results  in a simpler LintID data
structure.
  • Loading branch information
olafurpg committed Aug 17, 2017
1 parent 6d06f8f commit a6f474d
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ case class LintConfig(
).map { case ((((a, b), c), d), e) => LintConfig(a, b, c, d, e) }
}

def getCategory(id: LintID): LintCategory = id.key match {
case error() => LintCategory.Error
case warning() => LintCategory.Warning
case info() => LintCategory.Info
case _ => id.category
}
def getConfiguredCategory(key: String): Option[LintCategory] =
Option(key).collect {
case error() => LintCategory.Error
case warning() => LintCategory.Warning
case info() => LintCategory.Info
}
}

object LintConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ object LintCategory {
case object Info extends LintCategory
case object Warning extends LintCategory
case object Error extends LintCategory
}
}
12 changes: 4 additions & 8 deletions scalafix-core/shared/src/main/scala/scalafix/lint/LintID.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@ import scala.meta.inputs.Position
*
* @param id a string ID for this message, typically the name of the
* assigned variable.
* @param owner The rewrite that owns this lint message, to distinguish
* lint messages with conflicting IDs from different rewrites.
* @param explanation An optional explanation for this kind of message.
* @param category The default category this message should get reported to.
* Note that users can configure/override the default category.
*/
final case class LintID(
id: String,
owner: RewriteName,
explanation: String,
category: LintCategory
) {
override def toString: String = key
lazy val key = s"$owner.$id"
def key(owner: RewriteName) = s"$owner.$id"
private def noExplanation: LintID =
new LintID(id, owner, explanation, category)
new LintID(id, explanation, category)
def at(message: String, position: Position): LintMessage =
LintMessage(message, position, this)
def at(message: String): LintMessage =
Expand All @@ -33,9 +29,9 @@ final case class LintID(

object LintID {
def error(explain: String)(implicit alias: sourcecode.Name): LintID =
new LintID(alias.value, RewriteName.empty, explain, LintCategory.Error)
new LintID(alias.value, explain, LintCategory.Error)
def warning(explain: String)(
implicit alias: sourcecode.Name,
rewrite: RewriteName): LintID =
new LintID(alias.value, RewriteName.empty, explain, LintCategory.Warning)
new LintID(alias.value, explain, LintCategory.Warning)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scalafix.lint

import scala.meta.Position
import scalafix.rewrite.RewriteName

/** An instance of a LintID with a custom message at a particular position
*
Expand All @@ -15,14 +16,14 @@ final case class LintMessage(
position: Position,
id: LintID
) {
def format(explain: Boolean): String = {
def format(owner: RewriteName, explain: Boolean): String = {
val explanation =
if (explain)
s"""
|Explanation:
|${id.explanation}
|""".stripMargin
else ""
s"[${id.owner.name}.${id.id}] $message$explanation"
s"[${owner.name}.${id.id}] $message$explanation"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ private[scalafix] object TreePatch {
}

// implementation detail
private[scalafix] case class LintPatch(
message: LintMessage
) extends Patch
private[scalafix] case class LintPatch(message: LintMessage) extends Patch
private[scalafix] case class Concat(a: Patch, b: Patch) extends Patch
private[scalafix] case object EmptyPatch extends Patch with LowLevelPatch

Expand All @@ -116,6 +114,9 @@ object Patch {
case (rem: Remove, rem2: Remove) => rem
case _ => throw Failure.TokenPatchMergeError(a, b)
}
// Patch.apply and Patch.lintMessages package private. Feel free to use them
// for your application, but please ask on the Gitter channel to see if we
// can expose a better api for your use case.
private[scalafix] def apply(
p: Patch,
ctx: RewriteCtx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ abstract class Rewrite(implicit val rewriteName: RewriteName) { self =>
final def apply(ctx: RewriteCtx, patch: Patch): String = {
val result = Patch(patch, ctx, semanticOption)
Patch.lintMessages(patch, ctx).foreach { msg =>
ctx.printLintMessage(msg.copy(id = msg.id.copy(owner = rewriteName)))
// Set the
ctx.printLintMessage(msg, rewriteName)
}
result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ case class RewriteCtx(tree: Tree, config: ScalafixConfig) extends PatchOps {
logger.elem(values: _*)
}

def printLintMessage(msg: LintMessage): Unit = {
if (config.lint.ignore.matches(msg.id.key)) ()
def printLintMessage(msg: LintMessage, owner: RewriteName): Unit = {
val key = msg.id.key(owner)
if (config.lint.ignore.matches(key)) ()
else {
val category = config.lint.getCategory(msg.id)
val category = config.lint
.getConfiguredCategory(key)
.getOrElse(msg.id.category)
reporter.handleMessage(
msg.format(config.lint.explain),
msg.format(owner, config.lint.explain),
msg.position,
category.toSeverity
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ abstract class SemanticRewriteSuite(
}.mkString
}
if (lintMessages.nonEmpty) {
Patch.lintMessages(patch, ctx).foreach(ctx.printLintMessage)
Patch
.lintMessages(patch, ctx)
.foreach(x => ctx.printLintMessage(x, rewrite.rewriteName))
val explanation =
"""To fix this problem, suffix the culprit lines with
| // scalafix: warning
Expand Down

0 comments on commit a6f474d

Please sign in to comment.