MetaCredo. Check behaviour
(MetaCredo v0.1.0)
View Source
Behaviour and macro for defining MetaCredo checks.
Mirrors Credo.Check ergonomics but operates on MetaAST via Metastatic.
Usage
defmodule MetaCredo.Check.Security.HardcodedValue do
use MetaCredo.Check,
category: :security,
base_priority: :high,
param_defaults: [exclude_localhost: true],
explanations: [
check: "Detects hardcoded URLs, IPs, and sensitive values.",
params: [
exclude_localhost: "Skip localhost URLs (default: true)"
]
]
@impl true
def run(%SourceFile{} = source_file, params) do
source_file
|> SourceFile.ast()
|> Metastatic.AST.prewalk([], &traverse(&1, &2, params))
|> elem(1)
end
defp traverse({:literal, meta, value} = _node, issues, _params)
when is_list(meta) and is_binary(value) do
# ... detection logic ...
issues
end
end
Summary
Callbacks
Returns the base priority for this check.
Returns the category for this check.
Returns the explanations for this check.
Returns a unique string ID for this check.
Returns the default values for params.
Runs the check on a source file. Returns a list of issues.
Returns the tags for this check.
Functions
Creates an Issue struct from check module, source file, and options.
Retrieves a parameter value, falling back to the check's defaults.
Returns the list of valid check categories.
Types
@type params() :: Keyword.t()
Callbacks
@callback base_priority() :: MetaCredo.Issue.priority()
Returns the base priority for this check.
@callback category() :: atom()
Returns the category for this check.
@callback explanations() :: Keyword.t()
Returns the explanations for this check.
@callback id() :: String.t()
Returns a unique string ID for this check.
@callback param_defaults() :: Keyword.t()
Returns the default values for params.
@callback run(source_file :: MetaCredo.SourceFile.t(), params :: params()) :: [ MetaCredo.Issue.t() ]
Runs the check on a source file. Returns a list of issues.
@callback tags() :: [atom()]
Returns the tags for this check.
Functions
@spec format_issue(module(), MetaCredo.SourceFile.t(), Keyword.t()) :: MetaCredo.Issue.t()
Creates an Issue struct from check module, source file, and options.
Options:
:message(required) - The issue message:trigger- The text fragment causing the issue:line_no- Line number:column- Column number:severity- Override severity:priority- Override priority:metadata- Additional metadata map
Retrieves a parameter value, falling back to the check's defaults.
@spec valid_categories() :: [atom()]
Returns the list of valid check categories.