explain.ex (1373B)
1 defmodule Mix.Tasks.Dialyzer.Explain do 2 @shortdoc "Display information about Dialyzer warnings." 3 4 @moduledoc """ 5 This task provides background information about Dialyzer warnings. 6 If invoked without any arguments it will list all warning atoms. 7 When invoked with the name of a particular warning, it will display 8 information regarding it. 9 10 ## Command line options 11 12 * `[warning]` - display information regarding warning 13 14 ``` 15 mix dialyzer.explain pattern_match 16 ``` 17 """ 18 use Mix.Task 19 alias Dialyxir.Output 20 21 def run(args) do 22 case OptionParser.parse(args, strict: []) do 23 {_, [warning], _} -> 24 warning |> explanation_text() |> Output.info() 25 26 {_, [], _} -> 27 list_warnings() |> Output.info() 28 29 _ -> 30 Mix.Task.run("help", ["dialyzer.explain"]) 31 end 32 end 33 34 defp explanation_text(warning_name) do 35 warning = String.to_atom(warning_name) 36 37 case Map.get(Dialyxir.Warnings.warnings(), warning) do 38 nil -> 39 "Unknown warning named: #{warning_name}" 40 41 module -> 42 module.explain() 43 end 44 end 45 46 defp list_warnings do 47 warnings = 48 Dialyxir.Warnings.warnings() 49 |> Map.keys() 50 |> Enum.sort() 51 |> Enum.map_join("\n", &Atom.to_string/1) 52 53 [ 54 """ 55 Explain warning with mix dialyzer.explain <warning> 56 57 #{warnings} 58 """ 59 ] 60 end 61 end