Skip to content

Commit 6bbf4f1

Browse files
committed
Add beforeStart and afterComplete methods on Rule
Rules can then implement initialisation and cleanup behaviours before and after all semantic documents have been seen.
1 parent 8bcc7d4 commit 6bbf4f1

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

scalafix-cli/src/main/scala/scalafix/internal/v1/MainOps.scala

+6
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ object MainOps {
264264
val N = files.length
265265
val width = N.toString.length
266266
var exit = ExitStatus.Ok
267+
268+
args.rules.rules.foreach(_.beforeStart())
269+
267270
files.foreach { file =>
268271
if (args.args.verbose) {
269272
val message = s"Processing (%${width}s/%s) %s".format(i, N, file)
@@ -273,6 +276,9 @@ object MainOps {
273276
val next = handleFile(args, file)
274277
exit = ExitStatus.merge(exit, next)
275278
}
279+
280+
args.rules.rules.foreach(_.afterComplete())
281+
276282
adjustExitCode(args, exit, files)
277283
}
278284

scalafix-core/src/main/scala/scalafix/v1/Rule.scala

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ abstract class Rule(val name: RuleName) {
3434
* on the website.
3535
*/
3636
def isExperimental: Boolean = false
37+
38+
/**
39+
* Called before the rule starts processing documents
40+
*/
41+
def beforeStart(): Unit = {}
42+
43+
/**
44+
* Called after all documents have been processed
45+
*/
46+
def afterComplete(): Unit = {}
47+
3748
}
3849

3950
abstract class SyntacticRule(name: RuleName) extends Rule(name) {

scalafix-testkit/src/main/scala/scalafix/testkit/SemanticRuleSuite.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ abstract class SemanticRuleSuite(val props: TestkitProperties)
4141
def runOn(diffTest: RuleTest): Unit = {
4242
test(diffTest.path.testName) {
4343
val (rule, sdoc) = diffTest.run.apply()
44+
rule.rules.foreach(_.beforeStart())
4445
val (fixed, messages) = rule.semanticPatch(sdoc, suppress = false)
45-
46+
rule.rules.foreach(_.afterComplete())
4647
val tokens = fixed.tokenize.get
4748
val obtained = SemanticRuleSuite.stripTestkitComments(tokens)
4849
val expected = diffTest.path.resolveOutput(props) match {

0 commit comments

Comments
 (0)