Skip to content

Scalafix 0.4.0

Compare
Choose a tag to compare
@olafurpg olafurpg released this 11 Sep 17:15
· 3516 commits to main since this release

This release represents a significant milestone for Scalafix. Scalafix no longer runs as a compiler plugin as it did in previous releases. Instead, Scalafix runs now independently from the compiler, and uses the Scalameta Semantic API to query information from the compiler.

New features

Breaking changes

  • The scalafix-nsc compiler plugin has been removed in favor of the Scalameta Semantic DB, that is emitted by the Scalameta semanticdb-scalac compiler plugin. This change enables Scalafix rules to run separately from compilation. This means that running scalafix will be much faster, since it no longer requires creating a custom instance of the Scala compiler and re-typechecking an entire project. Scalafix reads all of its inputs from .semanticdb files that are created by the semanticdb-scalac compiler plugin during regular compilation.

  • The signature of Rule.rule has been simplified

    // before
    def rule[T](ctx: RuleCtx[T]): Seq[Patch]
    // after
    def rule(ctx: RuleCtx): Patch
    
    // a Seq[Patch] can be converted into Patch with .asPatch
    Seq[Patch]().asPatch: Patch
    
  • RuleCtx.mirror has been removed. To write semantic rules, accept SemanticCtx as a constructor parameter instead

    // before
    case object MySemanticRule extends Rule {
      def rule[T](ctx: RuleCtx[T]): Seq[Patch]
    }
    // after
    case class MySemanticRule(index: SemanticdbIndex) extends SemanticRule(index) {
      def rule(ctx: RuleCtx): Patch
    }
    
  • scalafix.patch.TreePatch.{AddGlobalImport, RemoveGlobalImport, Replace, ...} have been made private[scalafix]. Instead, use the provided operations on ctx: RuleCtx.

    // before
    TokenPatch.AddGlobalImport(importer"scala.meta._")
    
    // after
    ctx.addGlobalImport(importer"scala.meta._")
    

    The motivation for this change was to statically enforce that a SemanticCtx is in scope during Patch creation. For the full available API of patch operations, see SemanticPatchOps

  • The following configuration options have been removed

    imports.removeUnusedImports // replaced by RemoveUnusedImports rule.
    
    // These were wishful thinking, too eagerly merged.
    imports.organizeImports
    imports.expandRelative
    imports.groupByPrefix
    
  • scalafix-cli now enabled the --in-place (-i) flag by default. Use --stdout to print to standard output.

  • scalafix-testkit no longer reads tests from .source files. Instead, tests are written in regular .scala files that are compiled through sbt or another build tool. See scalafix-testkit for how to use it.

git shortlog -sn --no-merges v0.3.0..v0.4.0 tells us that 4 people contributed to this release:

  • Ólafur Páll Geirsson
  • Gabriele Petronella
  • Allan Renucci
  • Matthias Langer