-
Notifications
You must be signed in to change notification settings - Fork 11
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
Showing
2 changed files
with
126 additions
and
18 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
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,93 @@ | ||
package fix | ||
|
||
import scala.meta.Defn | ||
import scala.meta.Init | ||
import scala.meta.Lit | ||
import scala.meta.Mod | ||
import scala.meta.Template | ||
import scala.meta.Term | ||
import scala.meta.Type | ||
import scalafix.Patch | ||
import scalafix.lint.Diagnostic | ||
import scalafix.lint.LintSeverity | ||
import scalafix.v1.SyntacticDocument | ||
import scalafix.v1.SyntacticRule | ||
|
||
class MyScalafixRuleRule extends SyntacticRule("MyScalafixRuleRule") { | ||
private[this] def when(cond: Boolean)(patch: => Patch): Patch = { | ||
if (cond) { | ||
patch | ||
} else { | ||
Patch.empty | ||
} | ||
} | ||
|
||
override def fix(implicit doc: SyntacticDocument): Patch = { | ||
doc.tree.collect { | ||
case Defn.Class.After_4_6_0( | ||
_, | ||
className, | ||
_, | ||
primaryCtor, | ||
Template.After_4_4_0( | ||
_, | ||
List( | ||
Init.After_4_6_0( | ||
Type.Name("SyntacticRule" | "SemanticRule"), | ||
_, | ||
List(Term.ArgClause(Lit.String(ruleName) :: Nil, _)) | ||
) | ||
), | ||
_, | ||
stats, | ||
_ | ||
) | ||
) => | ||
val withConfigurationMethodOpt = | ||
stats.collectFirst { | ||
case d: Defn.Def if d.name.value == "withConfiguration" && d.mods.exists(_.is[Mod.Override]) => | ||
d | ||
} | ||
Seq( | ||
when(className.value != ruleName) { | ||
Patch.lint( | ||
Diagnostic( | ||
id = "", | ||
message = s"${className} != ${ruleName}", | ||
position = className.pos, | ||
severity = LintSeverity.Error | ||
) | ||
) | ||
}, | ||
when(primaryCtor.paramClauses.nonEmpty) { | ||
withConfigurationMethodOpt match { | ||
case Some(withConfigurationMethod) => | ||
when( | ||
withConfigurationMethod.body.collect { | ||
case Lit.String(x) if x == ruleName => () | ||
}.isEmpty | ||
) { | ||
Patch.lint( | ||
Diagnostic( | ||
id = "", | ||
message = "maybe incorrect `withConfiguration` method", | ||
position = className.pos, | ||
severity = LintSeverity.Error | ||
) | ||
) | ||
} | ||
case None => | ||
Patch.lint( | ||
Diagnostic( | ||
id = "", | ||
message = "there is primary constructor args but not defined `withConfiguration` method", | ||
position = className.pos, | ||
severity = LintSeverity.Error | ||
) | ||
) | ||
} | ||
} | ||
).asPatch | ||
}.asPatch | ||
} | ||
} |