Skip to content

Commit

Permalink
Add columns to FIXME and TODO tag checks
Browse files Browse the repository at this point in the history
This allows text editors to correctly render the diagnostic
  • Loading branch information
mhanberg committed Feb 28, 2025
1 parent 965a59c commit 97ade06
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/credo/check/design/tag_fixme.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ defmodule Credo.Check.Design.TagFIXME do
|> Enum.map(&issue_for(issue_meta, &1))
end

defp issue_for(issue_meta, {line_no, _line, trigger}) do
defp issue_for(issue_meta, {line_no, column, _line, trigger}) do
format_issue(
issue_meta,
message: "Found a #{@tag_name} tag in a comment: #{trigger}",
line_no: line_no,
column: column,
trigger: trigger
)
end
Expand Down
17 changes: 10 additions & 7 deletions lib/credo/check/design/tag_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ defmodule Credo.Check.Design.TagHelper do
|> Credo.Code.clean_charlists_strings_and_sigils()
|> String.split("\n")
|> Enum.with_index()
|> Enum.map(&find_tag_in_line(&1, regex))
|> Enum.filter(&tags?/1)
|> Enum.flat_map(&find_tag_in_line(&1, regex))
else
[]
end
Expand All @@ -41,7 +40,7 @@ defmodule Credo.Check.Design.TagHelper do
if string =~ regex do
trimmed = String.trim_trailing(string)

{nil, memo ++ [{meta[:line], trimmed, trimmed}]}
{nil, memo ++ [{meta[:line], meta[:column], trimmed, trimmed}]}
else
{ast, memo}
end
Expand All @@ -58,9 +57,13 @@ defmodule Credo.Check.Design.TagHelper do
|> List.wrap()
|> Enum.map(&String.trim/1)

{index + 1, line, List.first(tag_list)}
end
if tag = List.first(tag_list) do
col =
Regex.run(~r"(.*)#"U, line) |> List.first("") |> String.length()

defp tags?({_line_no, _line, nil}), do: false
defp tags?({_line_no, _line, _tag}), do: true
[{index + 1, col, line, tag}]
else
[]
end
end
end
3 changes: 2 additions & 1 deletion lib/credo/check/design/tag_todo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ defmodule Credo.Check.Design.TagTODO do
|> Enum.map(&issue_for(issue_meta, &1))
end

defp issue_for(issue_meta, {line_no, _line, trigger}) do
defp issue_for(issue_meta, {line_no, column, _line, trigger}) do
format_issue(
issue_meta,
message: "Found a #{@tag_name} tag in a comment: #{trigger}",
line_no: line_no,
column: column,
trigger: trigger
)
end
Expand Down
5 changes: 4 additions & 1 deletion test/credo/check/design/tag_fixme_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ defmodule Credo.Check.Design.TagFIXMETest do
"""
|> to_source_file
|> run_check(@described_check)
|> assert_issue()
|> assert_issue(fn issue ->
assert issue.line_no == 4
assert issue.column == 3
end)
end

test "it should report an issue when lower case" do
Expand Down
3 changes: 3 additions & 0 deletions test/credo/check/design/tag_helper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ defmodule Credo.Check.Design.TagHelperTest do
expected = [
{
4,
19,
" def some_fun do # TODO: find a better name for this",
"# TODO: find a better name for this"
}
Expand All @@ -62,6 +63,7 @@ defmodule Credo.Check.Design.TagHelperTest do
expected = [
{
4,
19,
" def some_fun do # TODO find a better name for this",
"# TODO find a better name for this"
}
Expand All @@ -88,6 +90,7 @@ defmodule Credo.Check.Design.TagHelperTest do
expected = [
{
4,
3,
" # todo find a better name for this",
"# todo find a better name for this"
}
Expand Down
5 changes: 4 additions & 1 deletion test/credo/check/design/tag_todo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ defmodule Credo.Check.Design.TagTODOTest do
"""
|> to_source_file
|> run_check(@described_check)
|> assert_issue()
|> assert_issue(fn issue ->
assert issue.line_no == 4
assert issue.column == 3
end)
end

test "it should report an issue for @doc tags" do

Check failure on line 96 in test/credo/check/design/tag_todo_test.exs

View workflow job for this annotation

GitHub Actions / all triggers are looked up and confirmed

test it should report an issue for @doc tags (Credo.Check.Design.TagTODOTest)
Expand Down

0 comments on commit 97ade06

Please sign in to comment.