-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb7b3de
commit cbf1f7d
Showing
4 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
rule = NoGeneralException | ||
*/ | ||
package fix | ||
|
||
import scala.language.higherKinds | ||
|
||
class CustomErrorType(message: String) extends Exception(message) | ||
|
||
object NoGeneralException { | ||
|
||
def raiseThrowable(error: Throwable): Unit = {} | ||
|
||
def raiseException(error: Exception): Unit = {} | ||
|
||
raiseException(new Exception("Message")) /* assert: NoGeneralException | ||
^^^^^^^^^^^^^^^^^^^^^^^^ | ||
Exception is not allowed | ||
*/ | ||
|
||
raiseThrowable(new Throwable("Message")) /* assert: NoGeneralException | ||
^^^^^^^^^^^^^^^^^^^^^^^^ | ||
Exception is not allowed | ||
*/ | ||
|
||
raiseThrowable(new CustomErrorType("Message")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
fix.MakeArgsNamed | ||
fix.NoGeneralException | ||
fix.NoHead | ||
fix.NoMapApply | ||
fix.NoOptionGet | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package fix | ||
|
||
import fix.NoGeneralException._ | ||
import metaconfig.{ConfDecoder, Configured} | ||
import metaconfig.generic.Surface | ||
import scalafix.v1._ | ||
|
||
import scala.meta._ | ||
|
||
case class NoGeneralException(config: Config) extends SemanticRule("NoGeneralException") { | ||
|
||
def this() = this(NoGeneralException.defaultConfig) | ||
|
||
override def fix(implicit doc: SemanticDocument): Patch = { | ||
doc.tree.collect { | ||
case t@Term.New(_) => { | ||
t.symbol.info.map(_.displayName) match { | ||
case Some(exception) if config.forbidden.contains(exception) => | ||
Patch.lint(Diagnostic("", "Exception is not allowed", t.pos)) | ||
case _ => Patch.empty | ||
} | ||
} | ||
}.asPatch | ||
} | ||
|
||
override def withConfiguration(config: Configuration): Configured[Rule] = { | ||
config.conf.getOrElse("NoGeneralException")(defaultConfig).map( | ||
config => NoGeneralException(config.copy(forbidden = forbidden ++ config.forbidden)) | ||
) | ||
} | ||
} | ||
|
||
object NoGeneralException { | ||
|
||
case class Config(forbidden: List[String]) | ||
|
||
private val forbidden = List( | ||
"Exception", | ||
"Throwable" | ||
) | ||
|
||
private val defaultConfig = Config(forbidden) | ||
|
||
implicit val surface: Surface[Config] = | ||
metaconfig.generic.deriveSurface[Config] | ||
|
||
implicit val decoder: ConfDecoder[Config] = | ||
metaconfig.generic.deriveDecoder(defaultConfig) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters