Skip to content

Commit

Permalink
Add id for CustomMessage (fix scalacenter#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasseGuillaume committed Dec 12, 2017
1 parent e4a5c35 commit 9ae6071
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package scalafix
package config

import metaconfig.{Conf, ConfError, ConfDecoder, Configured, Metaconfig}
import org.langmeta._
import scalafix.internal.config.MetaconfigPendingUpstream.XtensionConfScalafix

import scala.language.implicitConversions

class CustomMessage[T](
val value: T,
val message: Option[String],
val id: Option[String])

object CustomMessage {
def getOption[T](obj: Conf.Obj, path: String)(
implicit ev: ConfDecoder[T]): Configured[Option[T]] =
Metaconfig
.getKey(obj, Seq(path))
.map(
value => ev.read(value).map(Some(_))
)
.getOrElse(Configured.Ok(None))

def decoder[T](field: String)(
implicit ev: ConfDecoder[T]): ConfDecoder[CustomMessage[T]] =
ConfDecoder.instance[CustomMessage[T]] {
case obj: Conf.Obj => {
(obj.get[T](field) |@|
getOption[String](obj, "message") |@|
getOption[String](obj, "id")).map {
case ((value, message0), id) =>
val message =
message0.map(msg =>
if (msg.isMultiline) {
"\n" + msg.stripMargin
} else {
msg
})

new CustomMessage(value, message, id)
}
}
case els =>
ev.read(els).map(value => new CustomMessage(value, None, None))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package scalafix.internal.config

import scalafix.config.CustomMessage

import metaconfig.ConfDecoder
import MetaconfigPendingUpstream.XtensionConfScalafix
import org.langmeta.Symbol
Expand All @@ -8,14 +10,13 @@ import scalafix.internal.util.SymbolOps
case class DisableConfig(symbols: List[CustomMessage[Symbol.Global]] = Nil) {
def allSymbols = symbols.map(_.value)

private val messageBySymbol: Map[String, String] =
private val messageBySymbol: Map[String, CustomMessage[Symbol.Global]] =
symbols
.flatMap(custom =>
custom.message.map(message =>
(SymbolOps.normalize(custom.value).syntax, message)))
.map(custom => (SymbolOps.normalize(custom.value).syntax, custom))
.toMap

def customMessage(symbol: Symbol.Global): Option[String] =
def customMessage(
symbol: Symbol.Global): Option[CustomMessage[Symbol.Global]] =
messageBySymbol.get(SymbolOps.normalize(symbol).syntax)

implicit val customMessageReader: ConfDecoder[CustomMessage[Symbol.Global]] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package scalafix.internal.config

import scalafix.config.CustomMessage

import metaconfig.{Conf, ConfError, ConfDecoder, Configured}
import org.langmeta._
import MetaconfigPendingUpstream.XtensionConfScalafix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,18 @@ final case class Disable(index: SemanticdbIndex, config: DisableConfig)
case _ =>
"" -> pos
}
val message = config
.customMessage(symbol)
val custom = config.customMessage(symbol)

val message = custom
.flatMap(_.message)
.getOrElse(s"${signature.name} is disabled$details")

val id = custom
.flatMap(_.id)
.getOrElse(signature.name)

errorCategory
.copy(id = signature.name)
.copy(id = id)
.at(message, caret)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final case class DisableSyntax(
while (matcher.find()) {
regexLintMessages +=
errorCategory
.copy(id = pattern)
.copy(id = regex.id.getOrElse(pattern))
.at(message, pos(matcher.start))
}
}
Expand Down
3 changes: 2 additions & 1 deletion scalafix-tests/input/src/main/scala/test/Disable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Disable.symbols = [
"test.Disable.D.disabledFunction"
{
symbol = "scala.Option.get"
id = "Option.get"
message =
"""|Option.get is the root of all evils
|
Expand Down Expand Up @@ -47,7 +48,7 @@ case object Disable {
def asInstanceOf: O = "test"
}
val yy = AA.asInstanceOf // OK, no errors
Option(1).get /* assert: Disable.get
Option(1).get /* assert: Disable.Option.get
^
Option.get is the root of all evils
Expand Down
3 changes: 2 additions & 1 deletion scalafix-tests/input/src/main/scala/test/DisableSyntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ DisableSyntax.noSemicolons = true
DisableSyntax.noXml = true
DisableSyntax.regex = [
{
id = offensive
pattern = "[P|p]imp"
message = "Please consider a less offensive word such as Extension"
}
Expand Down Expand Up @@ -39,7 +40,7 @@ case object DisableSyntax {
<a>xml</a> // assert: DisableSyntax.noXml
// assert: DisableSyntax.noTabs

implicit class StringPimp(value: String) { // assert: DisableSyntax.[P|p]imp
implicit class StringPimp(value: String) { // assert: DisableSyntax.offensive
def -(other: String): String = s"$value - $other"
}

Expand Down

0 comments on commit 9ae6071

Please sign in to comment.