From 857c7f96c46eec0138c9779297eda4cac011965f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=8BAndrzej=20Ressel?= Date: Sat, 23 Nov 2024 21:08:17 +0100 Subject: [PATCH] Add inspection for "ZIO.fail(throw" (#491) * Add inspection for "ZIO.fail(throw" * Add inspection for "ZIO.fail(throw" * Add inspection for "ZIO.fail(throw" --- src/main/resources/META-INF/plugin.xml | 6 +++ .../mistakes/ZIOFailThrowInspection.scala | 39 +++++++++++++++++++ .../ZIOFailThrowInspectionTest.scala | 22 +++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/main/scala/zio/intellij/inspections/mistakes/ZIOFailThrowInspection.scala create mode 100644 src/test/scala/zio/inspections/ZIOFailThrowInspectionTest.scala diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index f3fa0b0..f7fafd2 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -313,6 +313,12 @@ shortName="IncorrectPreludeNewTypeAliasInspection" level="ERROR" enabledByDefault="true" language="Scala"/> + + ZIO/Suggestions zio.intellij.intentions.suggestions.SuggestTypeAlias diff --git a/src/main/scala/zio/intellij/inspections/mistakes/ZIOFailThrowInspection.scala b/src/main/scala/zio/intellij/inspections/mistakes/ZIOFailThrowInspection.scala new file mode 100644 index 0000000..1557d78 --- /dev/null +++ b/src/main/scala/zio/intellij/inspections/mistakes/ZIOFailThrowInspection.scala @@ -0,0 +1,39 @@ +package zio.intellij.inspections.mistakes + +import com.intellij.codeInspection.{LocalInspectionTool, ProblemHighlightType, ProblemsHolder} +import com.intellij.openapi.project.Project +import org.jetbrains.plugins.scala.codeInspection.{AbstractFixOnPsiElement, PsiElementVisitorSimple} +import org.jetbrains.plugins.scala.lang.psi.api.expr.{ScExpression, ScThrow} +import zio.intellij.inspections.`ZIO.fail` + +class ZIOFailThrowInspection extends LocalInspectionTool { + + override def buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitorSimple = { + case fail @ `ZIO.fail`(_, scThrow: ScThrow) => + scThrow.expression match { + case Some(throwableExpr) => + holder.registerProblem( + fail, + ZIOFailThrowInspection.message, + ProblemHighlightType.WARNING, + new QuickFix(scThrow, throwableExpr) + ) + case None => + } + case _ => + } + + final class QuickFix( + val toReplace: ScExpression, + val replaceWith: ScExpression + ) extends AbstractFixOnPsiElement(s"Remove throw from ZIO.fail", toReplace) { + + override protected def doApplyFix(element: ScExpression)(implicit project: Project): Unit = + element.replace(replaceWith) + } + +} + +object ZIOFailThrowInspection { + val message = "Mistaken throwing of the exception inside ZIO.fail" +} diff --git a/src/test/scala/zio/inspections/ZIOFailThrowInspectionTest.scala b/src/test/scala/zio/inspections/ZIOFailThrowInspectionTest.scala new file mode 100644 index 0000000..c2ef8fb --- /dev/null +++ b/src/test/scala/zio/inspections/ZIOFailThrowInspectionTest.scala @@ -0,0 +1,22 @@ +package zio.inspections + +import zio.intellij.inspections.mistakes.ZIOFailThrowInspection + +class ZIOFailThrowInspectionTest extends ZScalaInspectionTest[ZIOFailThrowInspection] { + + override protected def description: String = ZIOFailThrowInspection.message + val hint = "Remove throw from ZIO.fail" + + def test_throw_inside_zio_fail(): Unit = { + z(s"""for { + | x <- ${START}ZIO.fail(throw new RuntimeException("FAIL"))${END} + |} yield ()""".stripMargin).assertHighlighted() + val text = z(s"""for { + | x <- ZIO.fail(throw new RuntimeException("FAIL")) + |} yield ()""".stripMargin) + val result = z(s"""for { + | x <- ZIO.fail(new RuntimeException("FAIL")) + |} yield ()""".stripMargin) + testQuickFix(text, result, hint) + } +}