Skip to content

Commit

Permalink
Merge pull request #1126 from mhanberg/fix-debugger-columns
Browse files Browse the repository at this point in the history
rrrene authored Apr 28, 2024
2 parents c0b82e4 + 199eca4 commit e48a5eb
Showing 5 changed files with 60 additions and 14 deletions.
9 changes: 8 additions & 1 deletion lib/credo/check/readability/function_names.ex
Original file line number Diff line number Diff line change
@@ -149,7 +149,14 @@ defmodule Credo.Check.Readability.FunctionNames do
issues
end

defp issues_for_name("sigil_" <> sigil_letters = name, args, meta, issues, issue_meta, _allow_acronyms?) do
defp issues_for_name(
"sigil_" <> sigil_letters = name,
args,
meta,
issues,
issue_meta,
_allow_acronyms?
) do
multi_letter_sigil? = String.match?(sigil_letters, ~r/^[A-Z]+$/)

if multi_letter_sigil? do
17 changes: 9 additions & 8 deletions lib/credo/check/warning/dbg.ex
Original file line number Diff line number Diff line change
@@ -32,59 +32,60 @@ defmodule Credo.Check.Warning.Dbg do
issues,
issue_meta
) do
{ast, [issue_for(issue_meta, meta[:line]) | issues]}
{ast, [issue_for(issue_meta, meta) | issues]}
end

defp traverse(
{:dbg, meta, [_single_param]} = ast,
issues,
issue_meta
) do
{ast, [issue_for(issue_meta, meta[:line]) | issues]}
{ast, [issue_for(issue_meta, meta) | issues]}
end

defp traverse(
{:dbg, meta, [_first_param, _second_param]} = ast,
issues,
issue_meta
) do
{ast, [issue_for(issue_meta, meta[:line]) | issues]}
{ast, [issue_for(issue_meta, meta) | issues]}
end

defp traverse(
{{:., _, [{:__aliases__, _, [:"Elixir", :Kernel]}, :dbg]}, meta, _args} = ast,
issues,
issue_meta
) do
{ast, [issue_for(issue_meta, meta[:line]) | issues]}
{ast, [issue_for(issue_meta, meta) | issues]}
end

defp traverse(
{{:., _, [{:__aliases__, _, [:Kernel]}, :dbg]}, meta, _args} = ast,
issues,
issue_meta
) do
{ast, [issue_for(issue_meta, meta[:line]) | issues]}
{ast, [issue_for(issue_meta, meta) | issues]}
end

defp traverse(
{:|>, _, [_, {:dbg, meta, nil}]} = ast,
issues,
issue_meta
) do
{ast, [issue_for(issue_meta, meta[:line]) | issues]}
{ast, [issue_for(issue_meta, meta) | issues]}
end

defp traverse(ast, issues, _issue_meta) do
{ast, issues}
end

defp issue_for(issue_meta, line_no) do
defp issue_for(issue_meta, meta) do
format_issue(
issue_meta,
message: "There should be no calls to `dbg/1`.",
trigger: "dbg",
line_no: line_no
line_no: meta[:line],
column: meta[:column]
)
end
end
11 changes: 6 additions & 5 deletions lib/credo/check/warning/io_inspect.ex
Original file line number Diff line number Diff line change
@@ -23,15 +23,15 @@ defmodule Credo.Check.Warning.IoInspect do
end

defp traverse(
{{:., _, [{:__aliases__, _, [:"Elixir", :IO]}, :inspect]}, meta, _arguments} = ast,
{{:., _, [{:__aliases__, meta, [:"Elixir", :IO]}, :inspect]}, _, _arguments} = ast,
issues,
issue_meta
) do
{ast, issues_for_call(meta, issues, issue_meta)}
end

defp traverse(
{{:., _, [{:__aliases__, _, [:IO]}, :inspect]}, meta, _arguments} = ast,
{{:., _, [{:__aliases__, meta, [:IO]}, :inspect]}, _meta, _arguments} = ast,
issues,
issue_meta
) do
@@ -43,15 +43,16 @@ defmodule Credo.Check.Warning.IoInspect do
end

defp issues_for_call(meta, issues, issue_meta) do
[issue_for(issue_meta, meta[:line], @call_string) | issues]
[issue_for(issue_meta, meta, @call_string) | issues]
end

defp issue_for(issue_meta, line_no, trigger) do
defp issue_for(issue_meta, meta, trigger) do
format_issue(
issue_meta,
message: "There should be no calls to `IO.inspect/1`.",
trigger: trigger,
line_no: line_no
line_no: meta[:line],
column: meta[:column]
)
end
end
18 changes: 18 additions & 0 deletions test/credo/check/warning/dbg_test.exs
Original file line number Diff line number Diff line change
@@ -71,6 +71,24 @@ defmodule Credo.Check.Warning.DbgTest do
|> assert_issue()
end

test "it should report a violation with two on the same line" do
"""
defmodule CredoSampleModule do
def some_function(parameter1, parameter2) do
dbg(parameter1) + dbg(parameter2)
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> assert_issues(fn [one, two] ->
assert one.line_no == 3
assert one.column == 23
assert two.line_no == 3
assert two.column == 5
end)
end

test "it should report a violation /2" do
"""
defmodule CredoSampleModule do
19 changes: 19 additions & 0 deletions test/credo/check/warning/io_inspect_test.exs
Original file line number Diff line number Diff line change
@@ -37,6 +37,25 @@ defmodule Credo.Check.Warning.IoInspectTest do
|> assert_issue()
end

test "it should report a violation with two on the same line" do
"""
defmodule CredoSampleModule do
def some_function(parameter1, parameter2) do
foo(IO.inspect(parameter1), parameter2) |> IO.inspect()
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> assert_issues(fn [first, second] ->
assert first.line_no == 3
assert first.column == 48

assert second.line_no == 3
assert second.column == 9
end)
end

test "it should report a violation /2" do
"""
defmodule CredoSampleModule do

0 comments on commit e48a5eb

Please sign in to comment.