-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add credo check to assert a supervisor is named
- Loading branch information
1 parent
5705973
commit 5d92dff
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
defmodule Archethic.Checks.NamedSupervisor do | ||
@moduledoc """ | ||
Check that supervisor has a name | ||
""" | ||
|
||
use Credo.Check, category: :warning | ||
|
||
defstruct supervisor?: false, named?: false | ||
|
||
def run(source_file = %SourceFile{}, params = []) do | ||
# IssueMeta helps keeping track of the source file and the check's params | ||
# (technically, it's just a custom tagged tuple) | ||
issue_meta = IssueMeta.for(source_file, params) | ||
|
||
case Credo.Code.prewalk(source_file, &traverse(&1, &2), %__MODULE__{}) do | ||
%__MODULE__{supervisor?: true, named?: false} -> | ||
[format_issue(issue_meta, message: "Supervisor must be named")] | ||
|
||
_ -> | ||
[] | ||
end | ||
end | ||
|
||
defp traverse(ast = {:use, _, [{:__aliases__, _, [:Supervisor]} | _]}, acc) do | ||
{ast, %__MODULE__{acc | supervisor?: true}} | ||
end | ||
|
||
defp traverse( | ||
ast = | ||
{{:., _, | ||
[ | ||
{:__aliases__, _, [:Supervisor]}, | ||
:start_link | ||
]}, _, sup_ast}, | ||
acc = %__MODULE__{supervisor?: true} | ||
) do | ||
case sup_ast do | ||
[ | ||
{:__MODULE__, _, nil}, | ||
_, | ||
[name: _] | ||
] -> | ||
{ast, %__MODULE__{acc | named?: true}} | ||
|
||
[ | ||
{:__MODULE__, _, nil}, | ||
[name: _] | ||
] -> | ||
{ast, %__MODULE__{acc | named?: true}} | ||
|
||
_ -> | ||
{ast, acc} | ||
end | ||
end | ||
|
||
defp traverse(ast, acc), do: {ast, acc} | ||
end |