Credo v1.3.1 Credo.Check behaviour View Source

Check modules represent the checks which are run during Credo’s analysis.

Example:

defmodule MyCheck do
  use Credo.Check, category: :warning, base_priority: :high

  def run(source_file, params) do
    #
  end
end

The check can be configured by passing the following options to use Credo.Check:

  • :base_priority Sets the checks’s base priority (:low, :normal, :high, :higher or :ignore).

  • :category Sets the check’s category.

  • :elixir_version Sets the check’s version requirement for Elixir (defaults to >= 0.0.1).

  • :explanations Sets explanations displayed for the check, e.g.

    [
      check: "...",
      params: [
        param1: "Your favorite number",
        param2: "Online/Offline mode"
      ]
    ]
  • :param_defaults Sets the default values for the check’s params (e.g. [param1: 42, param2: "offline"])

  • :run_on_all Sets whether the check runs on all source files at once or each source file separatly.

Please also note that these options to use Credo.Check are just a convenience to implement the Credo.Check behaviour. You can implement any of these by hand:

defmodule MyCheck do
  use Credo.Check

  def category, do: :warning

  def base_priority, do: :high

  def explanations do
    [
      check: "...",
      params: [
        param1: "Your favorite number",
        param2: "Online/Offline mode"
      ]
    ]
  end

  def param_defaults, do: [param1: 42, param2: "offline"]

  def run(source_file, params) do
    #
  end
end

The run/2 function of a Check module takes two parameters: a source file and a list of parameters for the check. It has to return a list of found issues.

Link to this section Summary

Functions

format_issue takes an issue_meta and returns an issue. The resulting issue can be made more explicit by passing the following options to format_issue/2

Converts a given category to an exit status

Callbacks

Returns the base priority for the check

Returns the category for the check

Returns the required Elixir version for the check

Returns the explanations for the check and params as a keyword list

Returns the default values for the check’s params as a keyword list

Returns wether or not this check runs on all source files

Link to this section Functions

Link to this function explanation_for(keywords, key) View Source
Link to this function format_issue(issue_meta, opts, issue_category, issue_base_priority, check) View Source

format_issue takes an issue_meta and returns an issue. The resulting issue can be made more explicit by passing the following options to format_issue/2:

  • :priority Sets the issue’s priority.
  • :trigger Sets the issue’s trigger.
  • :line_no Sets the issue’s line number. Tries to find column if :trigger is supplied.
  • :column Sets the issue’s column.
  • :exit_status Sets the issue’s exit_status.
  • :severity Sets the issue’s severity.

Converts a given category to an exit status

Link to this section Callbacks

Link to this callback base_priority() View Source
base_priority() :: :higher | :high | :normal | :low | :ignore | integer()

Returns the base priority for the check.

This can be one of :higher, :high, :normal, :low or :ignore (technically it can also be or an integer, but these are internal representations although that is not recommended).

Link to this callback category() View Source
category() :: atom()

Returns the category for the check.

Link to this callback elixir_version() View Source
elixir_version() :: String.t()

Returns the required Elixir version for the check.

Link to this callback explanations() View Source
explanations() :: Keyword.t()

Returns the explanations for the check and params as a keyword list.

Link to this callback format_issue(issue_meta, opts) View Source
format_issue(issue_meta :: Credo.IssueMeta.t(), opts :: Keyword.t()) ::
  Credo.Issue.t()
Link to this callback param_defaults() View Source
param_defaults() :: Keyword.t()

Returns the default values for the check’s params as a keyword list.

Link to this callback run_on_all?() View Source
run_on_all?() :: boolean()

Returns wether or not this check runs on all source files.