Skip to content

Commit 6c2da64

Browse files
committed
IEx.Introspection.decompose/2 return :error on invalid expressions
And write a helper function on iex to Macro.espace
1 parent 75813e4 commit 6c2da64

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

lib/iex/lib/iex/helpers.ex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ defmodule IEx.Helpers do
333333
"""
334334
defmacro open(term) do
335335
quote do
336-
IEx.Introspection.open(unquote(Macro.escape(IEx.Introspection.decompose(term, __CALLER__))))
336+
IEx.Introspection.open(unquote(decompose(term, __CALLER__)))
337337
end
338338
end
339339

@@ -362,7 +362,7 @@ defmodule IEx.Helpers do
362362
"""
363363
defmacro h(term) do
364364
quote do
365-
IEx.Introspection.h(unquote(Macro.escape(IEx.Introspection.decompose(term, __CALLER__))))
365+
IEx.Introspection.h(unquote(decompose(term, __CALLER__)))
366366
end
367367
end
368368

@@ -381,7 +381,7 @@ defmodule IEx.Helpers do
381381
"""
382382
defmacro b(term) do
383383
quote do
384-
IEx.Introspection.b(unquote(Macro.escape(IEx.Introspection.decompose(term, __CALLER__))))
384+
IEx.Introspection.b(unquote(decompose(term, __CALLER__)))
385385
end
386386
end
387387

@@ -406,7 +406,7 @@ defmodule IEx.Helpers do
406406
"""
407407
defmacro t(term) do
408408
quote do
409-
IEx.Introspection.t(unquote(Macro.escape(IEx.Introspection.decompose(term, __CALLER__))))
409+
IEx.Introspection.t(unquote(decompose(term, __CALLER__)))
410410
end
411411
end
412412

@@ -553,6 +553,13 @@ defmodule IEx.Helpers do
553553
dont_display_result()
554554
end
555555

556+
defp decompose(term, context) do
557+
case IEx.Introspection.decompose(term, context) do
558+
:error -> term
559+
m_mf_mfa -> Macro.escape(m_mf_mfa)
560+
end
561+
end
562+
556563
# Given any "term", this function returns all the protocols in
557564
# :code.get_path() implemented by the data structure of such term, in the form
558565
# of a binary like "Protocol1, Protocol2, Protocol3".

lib/iex/lib/iex/introspection.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule IEx.Introspection do
1717
Macro.expand(module, context)
1818
end
1919

20-
def decompose({:/, _, [call, arity]} = term, context) do
20+
def decompose({:/, _, [call, arity]}, context) do
2121
case Macro.decompose_call(call) do
2222
{_mod, :__info__, []} when arity == 1 ->
2323
{Module, :__info__, 1}
@@ -29,7 +29,7 @@ defmodule IEx.Introspection do
2929
{find_decompose_fun_arity(fun, arity, context), fun, arity}
3030

3131
_ ->
32-
term
32+
:error
3333
end
3434
end
3535

@@ -44,7 +44,7 @@ defmodule IEx.Introspection do
4444
{find_decompose_fun_arity(maybe_sigil, 2, context), maybe_sigil, 2}
4545

4646
_ ->
47-
call
47+
:error
4848
end
4949

5050
{mod, fun, []} ->
@@ -54,7 +54,7 @@ defmodule IEx.Introspection do
5454
{find_decompose_fun(fun, context), fun}
5555

5656
_ ->
57-
call
57+
:error
5858
end
5959
end
6060

lib/mix/lib/mix/tasks/help.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ defmodule Mix.Tasks.Help do
106106
module
107107
|> Code.string_to_quoted!()
108108
|> IEx.Introspection.decompose(__ENV__)
109+
|> case do
110+
:error -> Mix.raise("Invalid expression: #{module}")
111+
decomposition -> decomposition
112+
end
109113
|> IEx.Introspection.h()
110114
after
111115
Application.put_env(:iex, :colors, iex_colors)

lib/mix/test/mix/tasks/help_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ defmodule Mix.Tasks.HelpTest do
265265
end)
266266
end
267267

268+
test "help ERROR" do
269+
assert_raise Mix.Error, "Invalid expression: Foo.bar(~s[baz])", fn ->
270+
Mix.Tasks.Help.run(["Foo.bar(~s[baz])"])
271+
end
272+
end
273+
268274
test "help --search PATTERN", context do
269275
in_tmp(context.test, fn ->
270276
Mix.Tasks.Help.run(["--search", "deps"])

0 commit comments

Comments
 (0)