Skip to content

Commit 306d0bf

Browse files
committed
Refactor warning handling for non-defd functions in Dx.Defd.NonDx
1 parent 4dc9d7c commit 306d0bf

File tree

1 file changed

+28
-44
lines changed

1 file changed

+28
-44
lines changed

lib/dx/defd/non_dx.ex

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule Dx.Defd.NonDx do
3030
{ast, state} = Compiler.normalize(ast, state)
3131

3232
if orig_state.warn_non_dx? and not state.called_non_dx? do
33-
warn(meta, state, """
33+
Compiler.warn(meta, state, """
3434
No function was called that is not defined with defd.
3535
3636
Please remove the call to non_dx/1.
@@ -42,19 +42,11 @@ defmodule Dx.Defd.NonDx do
4242
end
4343

4444
# &local_fun/2
45-
def normalize({:&, meta, [{:/, [], [{fun_name, _meta2, nil}, arity]}]}, state) do
45+
def normalize({:&, meta, [{:/, [], [{fun_name, _meta2, nil}, arity]}]} = ast, state) do
4646
args = Macro.generate_arguments(arity, __MODULE__)
4747
line = meta[:line] || state.line
4848

49-
if state.warn_non_dx? do
50-
warn(meta, state, """
51-
#{fun_name}/#{arity} is not defined with defd.
52-
53-
Either define it using defd (preferred) or wrap the call in the non_dx/1 function:
54-
55-
non_dx(...(&#{fun_name}/#{arity}))
56-
""")
57-
end
49+
maybe_warn_non_dx(meta, state, "#{fun_name}/#{arity}", Macro.to_string(ast))
5850

5951
quote line: line do
6052
{:ok,
@@ -67,21 +59,18 @@ defmodule Dx.Defd.NonDx do
6759

6860
# &Mod.fun/3
6961
def normalize(
70-
{:&, meta, [{:/, [], [{{:., _meta2, [module, fun_name]}, _meta3, []}, arity]}]},
62+
{:&, meta, [{:/, [], [{{:., _meta2, [module, fun_name]}, _meta3, []}, arity]}]} = ast,
7163
state
7264
) do
7365
args = Macro.generate_arguments(arity, __MODULE__)
7466
line = meta[:line] || state.line
7567

76-
if state.warn_non_dx? do
77-
warn(meta, state, """
78-
#{fun_name}/#{arity} is not defined with defd.
79-
80-
Either define it using defd (preferred) or wrap the call in the non_dx/1 function:
81-
82-
non_dx(...(&#{module}.#{fun_name}/#{arity}))
83-
""")
84-
end
68+
maybe_warn_non_dx(
69+
meta,
70+
state,
71+
"#{inspect(module)}.#{fun_name}/#{arity}",
72+
Macro.to_string(ast)
73+
)
8574

8675
quote line: line do
8776
{:ok,
@@ -99,15 +88,7 @@ defmodule Dx.Defd.NonDx do
9988

10089
cond do
10190
Util.has_function?(state.module, fun_name, arity) ->
102-
if state.warn_non_dx? do
103-
warn(meta, state, """
104-
#{fun_name}/#{arity} is not defined with defd.
105-
106-
Either define it using defd (preferred) or wrap the call in the non_dx/1 function:
107-
108-
non_dx(#{fun_name}(...))
109-
""")
110-
end
91+
maybe_warn_non_dx(meta, state, "#{fun_name}/#{arity}", "#{fun_name}(...)")
11192

11293
{ast, state} =
11394
normalize_external_call_args(args, state, fn args ->
@@ -135,15 +116,12 @@ defmodule Dx.Defd.NonDx do
135116
|> Ast.ok()
136117

137118
Util.has_function?(module, fun_name, arity) ->
138-
if state.warn_non_dx? do
139-
warn(meta2, state, """
140-
#{inspect(module)}.#{fun_name}/#{arity} is not defined with defd.
141-
142-
Either define it using defd (preferred) or wrap the call in the non_dx/1 function:
143-
144-
non_dx(#{inspect(module)}.#{fun_name}(...))
145-
""")
146-
end
119+
maybe_warn_non_dx(
120+
meta2,
121+
state,
122+
"#{inspect(module)}.#{fun_name}/#{arity}",
123+
"#{inspect(module)}.#{fun_name}/#{arity}(...)"
124+
)
147125

148126
{ast, state} =
149127
normalize_external_call_args(args, state, fn args ->
@@ -270,10 +248,16 @@ defmodule Dx.Defd.NonDx do
270248
@compile {:inline, with_state: 2}
271249
defp with_state(ast, state), do: {ast, state}
272250

273-
def warn(meta, state, message) do
274-
line = meta[:line] || state.line
275-
{name, arity} = state.function
276-
entry = {state.module, name, arity, [file: String.to_charlist(state.file), line: line]}
277-
IO.warn(message, [entry])
251+
# Helper to emit a warning for non-defd functions
252+
defp maybe_warn_non_dx(meta, state, mod_str, wrap_str) do
253+
if state.warn_non_dx? do
254+
Compiler.warn(meta, state, """
255+
#{mod_str} is not defined with defd.
256+
257+
Either define it using defd (preferred) or wrap the call in the non_dx/1 function:
258+
259+
non_dx(#{wrap_str})
260+
""")
261+
end
278262
end
279263
end

0 commit comments

Comments
 (0)