Skip to content

implement dbg for if expressions #13603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/elixir/lib/macro.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2642,6 +2642,15 @@ defmodule Macro do
end
end

defp dbg_ast_to_debuggable({:if, _meta, [condition_ast, _clauses]} = ast) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might need to check the meta here - since if is not a special form unlike case & co, it can be redefined.

 import Kernel, except: [if: 2]
 import MyOtherIf

We'd need a test for this case too I think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that but maybe there is a better way to implement it?

quote do
condition_result = unquote(condition_ast)
result = unquote(ast)

{:if, unquote(escape(ast)), unquote(escape(condition_ast)), condition_result, result}
end
end

# Any other AST.
defp dbg_ast_to_debuggable(ast) do
quote do: {:value, unquote(escape(ast)), unquote(ast)}
Expand Down Expand Up @@ -2755,6 +2764,23 @@ defmodule Macro do
{formatted, value}
end

defp dbg_format_ast_to_debug(
{:if, ast, condition_ast, condition_result, result},
options
) do
formatted = [
dbg_maybe_underline("If condition", options),
":\n",
dbg_format_ast_with_value(condition_ast, condition_result, options),
?\n,
dbg_maybe_underline("If expression", options),
" (#{if result, do: "do", else: "else"} clause executed):\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call the "do clause" the "truthy clause" instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I would just print the value of the condition and the value we return. It is easy to infer which clause was executed from that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think it's enough - I was trying to make it similar to other implementations.

dbg_format_ast_with_value(ast, result, options)
]

{formatted, result}
end

defp dbg_format_ast_to_debug({:value, code_ast, value}, options) do
{dbg_format_ast_with_value(code_ast, value, options), value}
end
Expand Down
56 changes: 56 additions & 0 deletions lib/elixir/test/elixir/macro_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,62 @@ defmodule MacroTest do
"""
end

test "if statement" do
x = true
map = %{a: 5, b: 1}

{result, formatted} =
dbg_format(
if true and x do
map[:a] * 2
else
map[:b]
end
)

assert result == 10

assert formatted =~ "macro_test.exs"

assert formatted =~ """
If condition:
true and x #=> true

If expression (do clause executed):
if true and x do
map[:a] * 2
else
map[:b]
end #=> 10
"""
end

test "if statement without else" do
x = true
map = %{a: 5, b: 1}

{result, formatted} =
dbg_format(
if false and x do
map[:a] * 2
end
)

assert result == nil

assert formatted =~ "macro_test.exs"

assert formatted =~ """
If condition:
false and x #=> false

If expression (else clause executed):
if false and x do
map[:a] * 2
end #=> nil
"""
end

test "with \"syntax_colors: []\" it doesn't print any color sequences" do
{_result, formatted} = dbg_format("hello")
refute formatted =~ "\e["
Expand Down