Skip to content

Commit

Permalink
Merge pull request #924 from rorygraves/rule_begin_end
Browse files Browse the repository at this point in the history
Add beforeStart and afterComplete methods on Rule
  • Loading branch information
olafurpg authored Jan 30, 2019
2 parents 0f1afb5 + 6bbf4f1 commit 0158ff9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ object MainOps {
val N = files.length
val width = N.toString.length
var exit = ExitStatus.Ok

args.rules.rules.foreach(_.beforeStart())

files.foreach { file =>
if (args.args.verbose) {
val message = s"Processing (%${width}s/%s) %s".format(i, N, file)
Expand All @@ -273,6 +276,9 @@ object MainOps {
val next = handleFile(args, file)
exit = ExitStatus.merge(exit, next)
}

args.rules.rules.foreach(_.afterComplete())

adjustExitCode(args, exit, files)
}

Expand Down
11 changes: 11 additions & 0 deletions scalafix-core/src/main/scala/scalafix/v1/Rule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ abstract class Rule(val name: RuleName) {
* on the website.
*/
def isExperimental: Boolean = false

/**
* Called before the rule starts processing documents
*/
def beforeStart(): Unit = {}

/**
* Called after all documents have been processed
*/
def afterComplete(): Unit = {}

}

abstract class SyntacticRule(name: RuleName) extends Rule(name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ abstract class SemanticRuleSuite(val props: TestkitProperties)
def runOn(diffTest: RuleTest): Unit = {
test(diffTest.path.testName) {
val (rule, sdoc) = diffTest.run.apply()
rule.rules.foreach(_.beforeStart())
val (fixed, messages) = rule.semanticPatch(sdoc, suppress = false)

rule.rules.foreach(_.afterComplete())
val tokens = fixed.tokenize.get
val obtained = SemanticRuleSuite.stripTestkitComments(tokens)
val expected = diffTest.path.resolveOutput(props) match {
Expand Down

0 comments on commit 0158ff9

Please sign in to comment.