-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds a github formatter (#463)
* refactor: Extracts formatter modules * refactor: Uses a behaviour over string and atom matching * feat: Adds a github formatter * chore: Updates log level and documentation * fix: Cleans up some warnings Co-authored-by: Isaac Sanders <isanders@drwholdings.com>
- Loading branch information
1 parent
a78730f
commit d38e42f
Showing
11 changed files
with
227 additions
and
146 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
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,92 @@ | ||
defmodule Dialyxir.Formatter.Dialyxir do | ||
@moduledoc false | ||
|
||
@behaviour Dialyxir.Formatter | ||
|
||
@impl Dialyxir.Formatter | ||
def format(dialyzer_warning = {_tag, {file, line}, message}) do | ||
{warning_name, arguments} = message | ||
base_name = Path.relative_to_cwd(file) | ||
|
||
formatted = | ||
try do | ||
warning = warning(warning_name) | ||
string = warning.format_long(arguments) | ||
|
||
""" | ||
#{base_name}:#{line}:#{warning_name} | ||
#{string} | ||
""" | ||
rescue | ||
e -> | ||
message = """ | ||
Unknown error occurred: #{inspect(e)} | ||
""" | ||
|
||
wrap_error_message(message, dialyzer_warning) | ||
catch | ||
{:error, :unknown_warning, warning_name} -> | ||
message = """ | ||
Unknown warning: | ||
#{inspect(warning_name)} | ||
""" | ||
|
||
wrap_error_message(message, dialyzer_warning) | ||
|
||
{:error, :lexing, warning} -> | ||
message = """ | ||
Failed to lex warning: | ||
#{inspect(warning)} | ||
""" | ||
|
||
wrap_error_message(message, dialyzer_warning) | ||
|
||
{:error, :parsing, failing_string} -> | ||
message = """ | ||
Failed to parse warning: | ||
#{inspect(failing_string)} | ||
""" | ||
|
||
wrap_error_message(message, dialyzer_warning) | ||
|
||
{:error, :pretty_printing, failing_string} -> | ||
message = """ | ||
Failed to pretty print warning: | ||
#{inspect(failing_string)} | ||
""" | ||
|
||
wrap_error_message(message, dialyzer_warning) | ||
|
||
{:error, :formatting, code} -> | ||
message = """ | ||
Failed to format warning: | ||
#{inspect(code)} | ||
""" | ||
|
||
wrap_error_message(message, dialyzer_warning) | ||
end | ||
|
||
formatted <> String.duplicate("_", 80) | ||
end | ||
|
||
defp wrap_error_message(message, warning) do | ||
""" | ||
Please file a bug in https://github.com/jeremyjh/dialyxir/issues with this message. | ||
#{message} | ||
Legacy warning: | ||
#{Dialyxir.Formatter.Dialyzer.format(warning)} | ||
""" | ||
end | ||
|
||
defp warning(warning_name) do | ||
warnings = Dialyxir.Warnings.warnings() | ||
|
||
if Map.has_key?(warnings, warning_name) do | ||
Map.get(warnings, warning_name) | ||
else | ||
throw({:error, :unknown_warning, warning_name}) | ||
end | ||
end | ||
end |
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,20 @@ | ||
defmodule Dialyxir.Formatter.Dialyzer do | ||
@moduledoc false | ||
|
||
@behaviour Dialyxir.Formatter | ||
|
||
@impl Dialyxir.Formatter | ||
def format(warning) do | ||
# OTP 22 uses indented output, but that's incompatible with dialyzer.ignore-warnings format. | ||
# Can be disabled, but OTP 21 and older only accept an atom, so only disable on OTP 22+. | ||
opts = | ||
if String.to_integer(System.otp_release()) < 22, | ||
do: :fullpath, | ||
else: [{:filename_opt, :fullpath}, {:indent_opt, false}] | ||
|
||
warning | ||
|> :dialyzer.format_warning(opts) | ||
|> String.Chars.to_string() | ||
|> String.replace_trailing("\n", "") | ||
end | ||
end |
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,25 @@ | ||
defmodule Dialyxir.Formatter.Github do | ||
@moduledoc false | ||
|
||
@behaviour Dialyxir.Formatter | ||
|
||
@impl Dialyxir.Formatter | ||
def format({_tag, {file, line}, {warning_name, arguments}}) do | ||
base_name = Path.relative_to_cwd(file) | ||
|
||
warning = warning(warning_name) | ||
string = warning.format_short(arguments) | ||
|
||
"::warning file=#{base_name},line=#{line},title=#{warning_name}::#{string}" | ||
end | ||
|
||
defp warning(warning_name) do | ||
warnings = Dialyxir.Warnings.warnings() | ||
|
||
if Map.has_key?(warnings, warning_name) do | ||
Map.get(warnings, warning_name) | ||
else | ||
throw({:error, :unknown_warning, warning_name}) | ||
end | ||
end | ||
end |
Oops, something went wrong.