Scalafix

A formatter changes white space; a linter decides that some code should not exist at all. Scalafix 0.14.10 is the rewrite engine of the Scala ecosystem: a rule reads the syntax tree, reports a problem, and may write a fixed version of the file. scala-cli runs it through its fix sub-command, which is marked experimental, and the rules of a project live in a .scalafix.conf.

A rule that refuses a var

DisableSyntax is the rule for "this construct is not allowed here". It has one switch per construct, and DisableSyntax.noVars = true forbids var in the folder the configuration belongs to. The program below counts down with two of them:

vars/.scalafix.conf

rules = [DisableSyntax] DisableSyntax.noVars = true

vars/Counter.scala

@main def counter(): Unit = var n = 3 var result = List.empty[String] while n > 0 do result = s"$n" :: result n = n - 1 println(result.mkString(" "))

scala-cli --power fix --check --server=false vars 2>&1

Running scalafix rules...
<PROJECT>/vars/Counter.scala:2:3: error: [DisableSyntax.var] mutable state should be avoided
  var n = 3
  ^^^
<PROJECT>/vars/Counter.scala:3:3: error: [DisableSyntax.var] mutable state should be avoided
  var result = List.empty[String]
  ^^^

--check writes nothing and exits 64, which is the status that says "the code does not satisfy the rules". The message names the rule and the line, so the editor and the build show the same thing. Without --check the rule would be asked to rewrite the file, and DisableSyntax has no rewrite: a construct it forbids is a decision for the author, not a transformation.

The rewrite is the author's job here: a range, a map and a mkString say the same thing without mutable state, and the second program is the result:

countdown.scala

@main def countdown(): Unit = val result = (1 to 3).toList.map(_.toString) println(result.mkString(" "))

scala-cli run --server=false countdown.scala

1 2 3

A rule that rewrites

OrganizeImports does have a rewrite: it groups the imports of a file, sorts the groups, and removes the ones nothing uses. Removing an unused import needs the compiler's help, so the file turns on -Wunused:imports with a directive, and its own configuration sits beside it in the folder:

imports/.scalafix.conf

rules = [OrganizeImports]

imports/Imports.scala

//> using options -Wunused:imports import scala.collection.mutable.ArrayBuffer import java.nio.file.{Path, Files} import scala.util.Random @main def imports(): Unit = val random = Random(42) println(random.nextInt(10) + Path.of(".").getNameCount)

scala-cli --power fix --check --server=false imports 2>&1

--- <PROJECT>/imports/Imports.scala
+++ <expected fix>
@@ -1,7 +1,6 @@
 //> using options -Wunused:imports
 
-import scala.collection.mutable.ArrayBuffer
-import java.nio.file.{Path, Files}
+import java.nio.file.Path
 import scala.util.Random
 
 @main def imports(): Unit =

The diff is the whole proposal: two imports go, and the two that stay are the ones the program uses. Applied, the file is the one below. The command writes the file and its own log is left out here, so the file itself is the proof:

scala-cli --power fix --server=false imports >/dev/null 2>&1

cat imports/Imports.scala

//> using options -Wunused:imports

import java.nio.file.Path
import scala.util.Random

@main def imports(): Unit =
  val random = Random(42)
  println(random.nextInt(10) + Path.of(".").getNameCount)

The rule set is not fixed: RemoveUnused, ExplicitResultTypes, LeakingImplicitClassVal and the rest of the built-in rules come from the same release, and a project lists the ones it wants. A rule that reports is a check; a rule that rewrites is a migration, and the two are run differently: every pull request runs --check, and a migration is a branch of its own.

Related

Автор статьи: Arthur Isaev

Изображение баннера