-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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"
- Loading branch information
1 parent
13b4fd2
commit 857c7f9
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/scala/zio/intellij/inspections/mistakes/ZIOFailThrowInspection.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/scala/zio/inspections/ZIOFailThrowInspectionTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |