Current section

Files

Jump to
green lib green rules linting no_unless_with_else.ex
Raw

lib/green/rules/linting/no_unless_with_else.ex

defmodule Green.Rules.Linting.NoUnlessWithElse do
@moduledoc """
This rule transforms `unless` with `else` into `if` with `else`.
"""
@behaviour Green.Rule
@impl true
def apply({forms, comments}, _opts) do
forms =
Macro.prewalk(forms, fn
{:unless, context,
[
condition,
[
{{:__block__, ctx1, [:do]}, do_expression},
{{:__block__, ctx2, [:else]}, else_expression}
]
]} ->
{:if, context,
[
condition,
[
{{:__block__, ctx1, [:do]}, else_expression},
{{:__block__, ctx2, [:else]}, do_expression}
]
]}
other ->
other
end)
{forms, comments}
end
end