Skip to content
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

Add columns to FIXME and TODO tag checks #1181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -26,7 +26,7 @@
# cases raising issues
#

test "it should report an issue" do

Check failure on line 29 in test/credo/check/design/tag_fixme_test.exs

View workflow job for this annotation

GitHub Actions / Test for lib/ changes

test it should report an issue (Credo.Check.Design.TagFIXMETest)
"""
defmodule CredoSampleModule do
use ExUnit.Case # TODO: this should not appear in the FIXME test
Expand All @@ -40,7 +40,10 @@
"""
|> 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 Expand Up @@ -84,7 +87,7 @@
end)
end

test "it should report a couple of issues when including docstrings" do

Check failure on line 90 in test/credo/check/design/tag_fixme_test.exs

View workflow job for this annotation

GitHub Actions / all triggers are looked up and confirmed

test it should report a couple of issues when including docstrings (Credo.Check.Design.TagFIXMETest)
~S'''
defmodule CredoSampleModule do
use ExUnit.Case # FIXME: this is the first
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 @@ -20,7 +20,7 @@
assert [] == tags
end

test "it should return a tag with colon" do

Check failure on line 23 in test/credo/check/design/tag_helper_test.exs

View workflow job for this annotation

GitHub Actions / Test for lib/ changes

test it should return a tag with colon (Credo.Check.Design.TagHelperTest)
tags =
"""
defmodule CredoSampleModule do
Expand All @@ -37,6 +37,7 @@
expected = [
{
4,
19,
" def some_fun do # TODO: find a better name for this",
"# TODO: find a better name for this"
}
Expand All @@ -45,7 +46,7 @@
assert expected == tags
end

test "it should return a tag without colon" do

Check failure on line 49 in test/credo/check/design/tag_helper_test.exs

View workflow job for this annotation

GitHub Actions / Test for lib/ changes

test it should return a tag without colon (Credo.Check.Design.TagHelperTest)
tags =
"""
defmodule CredoSampleModule do
Expand All @@ -62,6 +63,7 @@
expected = [
{
4,
19,
" def some_fun do # TODO find a better name for this",
"# TODO find a better name for this"
}
Expand All @@ -70,7 +72,7 @@
assert expected == tags
end

test "it should report a violation when lower case" do

Check failure on line 75 in test/credo/check/design/tag_helper_test.exs

View workflow job for this annotation

GitHub Actions / Test for lib/ changes

test it should report a violation when lower case (Credo.Check.Design.TagHelperTest)
tags =
"""
defmodule CredoSampleModule do
Expand All @@ -88,6 +90,7 @@
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 @@ -73,7 +73,7 @@
# cases raising issues
#

test "it should report an issue" do

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

View workflow job for this annotation

GitHub Actions / Test for lib/ changes

test it should report an issue (Credo.Check.Design.TagTODOTest)
"""
defmodule CredoSampleModule do
use ExUnit.Case # FIXME: this should not appear in the TODO test
Expand All @@ -87,10 +87,13 @@
"""
|> 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)
"""
defmodule CredoSampleModule do
@moduledoc \"\"\"
Expand All @@ -109,7 +112,7 @@
|> assert_issue()
end

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

Check failure on line 115 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 @moduledoc tags (Credo.Check.Design.TagTODOTest)
"""
defmodule CredoSampleModule do
@moduledoc \"\"\"
Expand All @@ -128,7 +131,7 @@
|> assert_issue()
end

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

Check failure on line 134 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 @shortdoc tags (Credo.Check.Design.TagTODOTest)
"""
defmodule CredoSampleModule do
@shortdoc \"\"\"
Expand Down Expand Up @@ -197,7 +200,7 @@
|> assert_issue()
end

test "it should report an issue at the end of a line w/o space" do

Check failure on line 203 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 at the end of a line w/o space (Credo.Check.Design.TagTODOTest)
"""
defmodule CredoSampleModule do
def some_fun do
Expand Down Expand Up @@ -251,7 +254,7 @@
end)
end

test "it should report a couple of issues when including docs" do

Check failure on line 257 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 a couple of issues when including docs (Credo.Check.Design.TagTODOTest)
~S'''
defmodule CredoSampleModule do
use ExUnit.Case # TODO: this is the first
Expand Down
Loading