From cdd47315643f3051d0c9e74487944641b797828c Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Thu, 8 Jun 2023 08:25:13 +0900 Subject: [PATCH] RemoveIf --- input/src/main/scala/fix/RemoveIfTest.scala | 43 ++++++++++++ output/src/main/scala/fix/RemoveIfTest.scala | 23 +++++++ rules/src/main/scala/fix/RemoveIf.scala | 70 ++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 input/src/main/scala/fix/RemoveIfTest.scala create mode 100644 output/src/main/scala/fix/RemoveIfTest.scala create mode 100644 rules/src/main/scala/fix/RemoveIf.scala diff --git a/input/src/main/scala/fix/RemoveIfTest.scala b/input/src/main/scala/fix/RemoveIfTest.scala new file mode 100644 index 0000000..5da6df2 --- /dev/null +++ b/input/src/main/scala/fix/RemoveIfTest.scala @@ -0,0 +1,43 @@ +/* +rule = RemoveIf + */ +package fix + +class RemoveIfTest { + def f1[A](a1: A, a2: A): Boolean = { + if (a1 == a2) { + true + } else { + false + } + } + + def f2[A](a1: A, a2: A): Boolean = { + if (a1 != a2) { + false + } else { + true + } + } + + def f3[A](a1: A, a2: A): Boolean = { + if (a1 == a2) + false + else + true + } + + def f4[A](a1: Seq[A]): Boolean = { + if (a1.isEmpty) + true + else + false + } + + def f5[A](a1: Seq[A]): Boolean = { + if (a1.nonEmpty) + false + else + true + } +} diff --git a/output/src/main/scala/fix/RemoveIfTest.scala b/output/src/main/scala/fix/RemoveIfTest.scala new file mode 100644 index 0000000..10c1ce3 --- /dev/null +++ b/output/src/main/scala/fix/RemoveIfTest.scala @@ -0,0 +1,23 @@ +package fix + +class RemoveIfTest { + def f1[A](a1: A, a2: A): Boolean = { + a1 == a2 + } + + def f2[A](a1: A, a2: A): Boolean = { + a1 == a2 + } + + def f3[A](a1: A, a2: A): Boolean = { + a1 != a2 + } + + def f4[A](a1: Seq[A]): Boolean = { + a1.isEmpty + } + + def f5[A](a1: Seq[A]): Boolean = { + a1.isEmpty + } +} diff --git a/rules/src/main/scala/fix/RemoveIf.scala b/rules/src/main/scala/fix/RemoveIf.scala new file mode 100644 index 0000000..0ec056c --- /dev/null +++ b/rules/src/main/scala/fix/RemoveIf.scala @@ -0,0 +1,70 @@ +package fix + +import scala.meta.Lit +import scala.meta.Term +import scalafix.Patch +import scalafix.v1.SyntacticDocument +import scalafix.v1.SyntacticRule + +object RemoveIf { + object True { + def unapply(t: Term): Boolean = PartialFunction.cond(t) { + case Lit.Boolean(true) => + true + case Term.Block(Lit.Boolean(true) :: Nil) => + true + } + } + + object False { + def unapply(t: Term): Boolean = PartialFunction.cond(t) { + case Lit.Boolean(false) => + true + case Term.Block(Lit.Boolean(false) :: Nil) => + true + } + } +} + +class RemoveIf extends SyntacticRule("RemoveIf") { + override def fix(implicit doc: SyntacticDocument): Patch = { + doc.tree.collect { + case x @ Term.If.After_4_4_0( + cond, + RemoveIf.True(), + RemoveIf.False(), + _ + ) => + Patch.replaceTree(x, cond.toString) + case x @ Term.If.After_4_4_0(cond, RemoveIf.False(), RemoveIf.True(), _) => + cond match { + case Term.ApplyInfix.Initial( + a1, + Term.Name("=="), + _, + a2 :: Nil + ) => + Patch.replaceTree(x, s"${a1} != ${a2}") + case Term.ApplyInfix.Initial( + a1, + Term.Name("!="), + _, + a2 :: Nil + ) => + Patch.replaceTree(x, s"${a1} == ${a2}") + case Term.Select( + a1, + Term.Name("isEmpty") + ) => + Patch.replaceTree(x, s"${a1}.nonEmpty") + case Term.Select( + a1, + Term.Name("nonEmpty") + ) => + Patch.replaceTree(x, s"${a1}.isEmpty") + case _ => + Patch.replaceTree(x, s"!($cond)") + } + }.asPatch + } +}