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

Document 'open_browser' credo check #95

Open
ftes opened this issue Jun 18, 2024 · 2 comments
Open

Document 'open_browser' credo check #95

ftes opened this issue Jun 18, 2024 · 2 comments

Comments

@ftes
Copy link
Contributor

ftes commented Jun 18, 2024

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
Copy link
Owner

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
Copy link
Contributor Author

ftes commented Jul 10, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants