Skip to content

Instantly share code, notes, and snippets.

@markolson
Created June 4, 2024 02:26
Show Gist options
  • Save markolson/ae3c63e4f48b6a96967beabeed2c22fb to your computer and use it in GitHub Desktop.
Save markolson/ae3c63e4f48b6a96967beabeed2c22fb to your computer and use it in GitHub Desktop.
Credo check that can take a custom date to begin failing
defmodule CustomCredo.DatedTodo do
use Credo.Check,
id: "EX2005",
param_defaults: [include_doc: true],
explanations: [
check: """
TODO comments are used to remind yourself of source code related things.
Example:
# TODO: move this to a Helper module
defp fun do
# ...
end
The premise here is that TODO should be dealt with in the near future and
are therefore reported by Credo.
Sometimes, you can give a deadline to yourself for what the "near future"
is, and give yourself some grace until them.
Example:
# TODO(2024-06-01): move this to a Helper module
defp fun do
# ...
end
Like all `Software Design` issues, this is just advice and might not be
applicable to your project/situation.
""",
params: [
include_doc: "Set to `true` to also include tags from @doc attributes."
]
]
alias Credo.Check.Design.TagHelper
@tag_name "TODO"
@date_regex ~r/#{@tag_name}\((\d+-\d+-\d+)\)/
@doc false
@impl true
def run(%SourceFile{} = source_file, params) do
issue_meta = IssueMeta.for(source_file, params)
include_doc? = Params.get(params, :include_doc, __MODULE__)
source_file
|> TagHelper.tags(@tag_name, include_doc?)
|> Enum.filter(fn {_line_no, _line, match} ->
case Regex.run(@date_regex, match) do
[_match, date] ->
case Date.from_iso8601(date) do
{:ok, check_date} -> Date.before?(check_date, Date.utc_today)
_ -> true
end
_ -> true
end
end)
|> Enum.map(&issue_for(issue_meta, &1))
end
defp issue_for(issue_meta, {line_no, _line, trigger}) do
format_issue(
issue_meta,
message: "Found a #{@tag_name} tag in a comment: #{trigger}",
line_no: line_no,
trigger: trigger
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment