Skip to content

Commit

Permalink
Merge pull request #526 from maennchen/jm/fix_file_location_with_column
Browse files Browse the repository at this point in the history
Fix formatting of file locations including column
  • Loading branch information
jeremyjh authored Dec 28, 2023
2 parents b928e39 + 2bc9d23 commit 0340de8
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/dialyxir/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ defmodule Dialyxir.Formatter do

alias Dialyxir.FilterMap

@type warning() :: {tag :: term(), {file :: Path.t(), line :: pos_integer()}, {atom(), list()}}
@type warning() ::
{tag :: term(), {file :: Path.t(), location :: :erl_anno.location()}, {atom(), list()}}

@type t() :: module()

Expand Down
4 changes: 2 additions & 2 deletions lib/dialyxir/formatter/dialyxir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Dialyxir.Formatter.Dialyxir do
@behaviour Dialyxir.Formatter

@impl Dialyxir.Formatter
def format(dialyzer_warning = {_tag, {file, line}, message}) do
def format(dialyzer_warning = {_tag, {file, location}, message}) do
{warning_name, arguments} = message
base_name = Path.relative_to_cwd(file)

Expand All @@ -16,7 +16,7 @@ defmodule Dialyxir.Formatter.Dialyxir do
string = warning.format_long(arguments)

"""
#{base_name}:#{line}:#{warning_name}
#{base_name}:#{Utils.format_location(location)}:#{warning_name}
#{string}
"""
rescue
Expand Down
10 changes: 8 additions & 2 deletions lib/dialyxir/formatter/github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ defmodule Dialyxir.Formatter.Github do
@behaviour Dialyxir.Formatter

@impl Dialyxir.Formatter
def format({_tag, {file, line}, {warning_name, arguments}}) do
def format({_tag, {file, location}, {warning_name, arguments}}) do
base_name = Path.relative_to_cwd(file)

warning = Utils.warning(warning_name)
string = warning.format_short(arguments)

"::warning file=#{base_name},line=#{line},title=#{warning_name}::#{string}"
case location do
{line, col} ->
"::warning file=#{base_name},line=#{line},col=#{col},title=#{warning_name}::#{string}"

line ->
"::warning file=#{base_name},line=#{line},title=#{warning_name}::#{string}"
end
end
end
2 changes: 1 addition & 1 deletion lib/dialyxir/formatter/ignore_file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Dialyxir.Formatter.IgnoreFile do
@behaviour Dialyxir.Formatter

@impl Dialyxir.Formatter
def format({_tag, {file, _line}, {warning_name, _arguments}}) do
def format({_tag, {file, _location}, {warning_name, _arguments}}) do
~s({"#{file}", :#{warning_name}},)
end
end
2 changes: 1 addition & 1 deletion lib/dialyxir/formatter/ignore_file_strict.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Dialyxir.Formatter.IgnoreFileStrict do
@behaviour Dialyxir.Formatter

@impl Dialyxir.Formatter
def format({_tag, {file, _line}, {warning_name, arguments}}) do
def format({_tag, {file, _location}, {warning_name, arguments}}) do
warning = Utils.warning(warning_name)
string = warning.format_short(arguments)

Expand Down
4 changes: 2 additions & 2 deletions lib/dialyxir/formatter/short.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ defmodule Dialyxir.Formatter.Short do
@behaviour Dialyxir.Formatter

@impl Dialyxir.Formatter
def format({_tag, {file, line}, {warning_name, arguments}}) do
def format({_tag, {file, location}, {warning_name, arguments}}) do
base_name = Path.relative_to_cwd(file)

warning = Utils.warning(warning_name)
string = warning.format_short(arguments)

"#{base_name}:#{line}:#{warning_name} #{string}"
"#{base_name}:#{Utils.format_location(location)}:#{warning_name} #{string}"
end
end
6 changes: 6 additions & 0 deletions lib/dialyxir/formatter/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ defmodule Dialyxir.Formatter.Utils do
throw({:error, :unknown_warning, warning_name})
end
end

@doc false
@spec format_location(:erl_anno.location()) :: String.t()
def format_location(location)
def format_location({line, column}), do: "#{line}:#{column}"
def format_location(line), do: "#{line}"
end
40 changes: 40 additions & 0 deletions test/dialyxir/formatter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,46 @@ defmodule Dialyxir.FormatterTest do
Mix.Project.in_project(app, "test/fixtures/#{Atom.to_string(app)}", fn _ -> f.() end)
end

describe "formats dialyzer warning" do
if System.otp_release() >= "24" do
for {formatter, message} <- %{
Formatter.Dialyxir =>
"lib/file/warning_type/line.ex:19:4:no_return\nFunction format_long/1 has no local return.",
Formatter.Dialyzer =>
"lib/file/warning_type/line.ex:19:4: Function format_long/1 has no local return",
Formatter.Github =>
"::warning file=lib/file/warning_type/line.ex,line=19,col=4,title=no_return::Function format_long/1 has no local return.",
Formatter.IgnoreFileStrict =>
~s|{"lib/file/warning_type/line.ex", "Function format_long/1 has no local return."},|,
Formatter.IgnoreFile => ~s|{"lib/file/warning_type/line.ex", :no_return},|,
# TODO: Remove if once only Elixir ~> 1.15 is supported
Formatter.Raw =>
if Version.match?(System.version(), "<= 1.15.0") do
~s|{:warn_return_no_exit, {'lib/file/warning_type/line.ex', {19, 4}}, {:no_return, [:only_normal, :format_long, 1]}}|
else
~s|{:warn_return_no_exit, {~c"lib/file/warning_type/line.ex", {19, 4}}, {:no_return, [:only_normal, :format_long, 1]}}|
end,
Formatter.Short =>
"lib/file/warning_type/line.ex:19:4:no_return Function format_long/1 has no local return."
} do
test "file location including column for #{formatter} formatter" do
assert {:warn, [message], _unused_filters} =
Formatter.format_and_filter(
[
{:warn_return_no_exit, {~c"lib/file/warning_type/line.ex", {19, 4}},
{:no_return, [:only_normal, :format_long, 1]}}
],
Project,
[],
unquote(formatter)
)

assert message =~ unquote(message)
end
end
end
end

describe "exs ignore" do
test "evaluates an ignore file and ignores warnings matching the pattern" do
warnings = [
Expand Down

0 comments on commit 0340de8

Please sign in to comment.