germsvel / phoenix_test

MIT License
144 stars 20 forks source link

Document 'open_browser' credo check #95

Open ftes opened 2 months ago

ftes commented 2 months ago

Could we document how to add a basic custom credo warning for open_browser? Or even add a ready-to-use check to this library?

The following is my crude attempt, I'm sure it can be vastly improved:

defmodule MyProject.Credo.NoOpenBrowser do
  @moduledoc false

  use Credo.Check,
    base_priority: :normal,
    category: :warning

  def run(source_file, params \\ []) do
    issue_meta = IssueMeta.for(source_file, params)
    Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
  end

  defp traverse({:open_browser, meta, _} = ast, issues, issue_meta) do
    {ast, issues ++ [issue_for(meta[:line], issue_meta)]}
  end

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

  defp issue_for(line_no, issue_meta) do
    format_issue(
      issue_meta,
      message: "There should be no `open_browser` calls.",
      line_no: line_no
    )
  end
end
germsvel commented 1 month ago

I think that's a really cool idea! I wouldn't mind that being documented in the docs (maybe by the open_browser docs) or included in the library. I wonder... is there prior art here that we can lean on? Do other libraries create their own custom credo checks? (I can't recall using one from a library. Typically use the standard stuff + things we roll on our own).

ftes commented 1 month ago

The only other non-Credo check I've used is excellent_migrations.

Can't think of any other prior art off the top of my head.