How to use one or multiple SemanticRules with scalafix similar to scalameta's Transformer?

360 views Asked by At

In the tutorial https://scalacenter.github.io/scalafix/docs/developers/tutorial.html I see how a SemanticRule is implemented for scalafix. I would like to call it directly in the source code on a Scala file which I have read from the disk and transform the file.

In scalameta I can do the following:

val f = .... // some File
val path = f.toPath
val bytes = java.nio.file.Files.readAllBytes(path)
val text = new String(bytes, "UTF-8")
val input = Input.VirtualFile(path.toString, text)
val source = input.parse[Source].get
val transformer = new MyTransformer()
val transformedSource = transformer(source)
// The transformed source can be written into an output file

I would like to do the same with scalafix but with a SemanticRule. If I read this: https://scalacenter.github.io/scalafix/docs/developers/tutorial.html#run-the-rule-from-source-code it only shows me how I can start the scalafix program with a file path to the Scala rule file but this is not what I want.

1

There are 1 answers

3
Dmytro Mitin On

The only way I know is to use file path to the rule if it's in a single file and doesn't have external dependencies or publish it otherwise.

Example projects for code generation with Scalafix are

https://github.com/olafurpg/scalafix-codegen

https://github.com/DmytroMitin/scalafix-codegen