All Downloads are FREE. Search and download functionalities are using the official Maven repository.

unused_code.WarnUnusedCode.scala Maven / Gradle / Ivy

There is a newer version: 0.4.1
Show newest version
package unused_code

import scalafix.v1.XtensionSeqPatch
import scala.meta.XtensionCollectionLikeUI
import metaconfig.Configured
import scalafix.Patch
import scalafix.v1.Configuration
import scalafix.v1.Rule
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule
import scala.meta.Position
import scalafix.Diagnostic
import scalafix.lint.LintSeverity
import java.nio.file.Paths

class WarnUnusedCode(config: UnusedCodeScalafixConfig) extends SyntacticRule("WarnUnusedCode") {
  def this() = this(UnusedCodeScalafixConfig.default)

  override def withConfiguration(config: Configuration): Configured[Rule] = {
    config.conf
      .getOrElse(UnusedCodeScalafixConfig.configKey)(this.config)
      .map(newConfig => new WarnUnusedCode(newConfig))
  }

  private[this] lazy val unusedNames: Set[String] = {
    FindResults.loadFromFile(Paths.get(config.outputPath)).values.map(_.value).toSet
  }

  override def fix(implicit doc: SyntacticDocument): Patch = {
    doc.tree
      .collect(UnusedCode.extractDefineValue)
      .collect {
        case (tree, _, name) if unusedNames.contains(name) =>
          Patch.lint(UnusedClassWarning(tree.pos))
      }
      .asPatch
  }
}

final case class UnusedClassWarning(override val position: Position) extends Diagnostic {
  override def message = "maybe unused"
  override def severity: LintSeverity = LintSeverity.Warning
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy