From 967dda3dfd3153f228e142a5c166c0c39a5fc983 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Wed, 21 Feb 2024 17:43:16 +0900 Subject: [PATCH] HeadLastTailInit --- .../src/main/scala/fix/HeadLastTailInit.scala | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 rules/src/main/scala/fix/HeadLastTailInit.scala diff --git a/rules/src/main/scala/fix/HeadLastTailInit.scala b/rules/src/main/scala/fix/HeadLastTailInit.scala new file mode 100644 index 0000000..e3b646d --- /dev/null +++ b/rules/src/main/scala/fix/HeadLastTailInit.scala @@ -0,0 +1,52 @@ +package fix + +import scala.meta.Term +import scalafix.Patch +import scalafix.lint.Diagnostic +import scalafix.lint.LintSeverity +import scalafix.v1.ByNameType +import scalafix.v1.MethodSignature +import scalafix.v1.SemanticDocument +import scalafix.v1.SemanticRule +import scalafix.v1.TypeRef +import scalafix.v1.ValueSignature +import scalafix.v1.XtensionTreeScalafix + +class HeadLastTailInit extends SemanticRule("HeadLastTailInit") { + private val collectionTypes: Set[String] = Set( + "scala.Array.", + "scala.package.Seq.", + "scala.package.List.", + "scala.Predef.Map.", + "scala.Predef.Set.", + "scala.Predef.Iterable.", + "scala.Predef.String." + ) + private def maybeScalaCollection(x: String): Boolean = { + x.startsWith("scala.collection.") || collectionTypes(x) + } + + override def fix(implicit doc: SemanticDocument): Patch = { + doc.tree.collect { + case Term.Select(obj, t @ Term.Name("head" | "last" | "tail" | "init" | "min" | "max" | "maxBy" | "minBy")) => + def p = Patch.lint( + Diagnostic( + id = "", + message = "", + position = t.pos, + severity = LintSeverity.Warning + ) + ) + obj.symbol.info.flatMap { i => + PartialFunction.condOpt(i.signature) { + case ValueSignature(t: TypeRef) if maybeScalaCollection(t.symbol.normalized.value) => + p + case MethodSignature(_, _, t: TypeRef) if maybeScalaCollection(t.symbol.normalized.value) => + p + case ValueSignature(ByNameType(t: TypeRef)) if maybeScalaCollection(t.symbol.normalized.value) => + p + } + }.asPatch + }.asPatch + } +}