Skip to content

Commit 0340de8

Browse files
authored
Merge pull request #526 from maennchen/jm/fix_file_location_with_column
Fix formatting of file locations including column
2 parents b928e39 + 2bc9d23 commit 0340de8

File tree

8 files changed

+62
-9
lines changed

8 files changed

+62
-9
lines changed

Diff for: lib/dialyxir/formatter.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ defmodule Dialyxir.Formatter do
99

1010
alias Dialyxir.FilterMap
1111

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

1415
@type t() :: module()
1516

Diff for: lib/dialyxir/formatter/dialyxir.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule Dialyxir.Formatter.Dialyxir do
66
@behaviour Dialyxir.Formatter
77

88
@impl Dialyxir.Formatter
9-
def format(dialyzer_warning = {_tag, {file, line}, message}) do
9+
def format(dialyzer_warning = {_tag, {file, location}, message}) do
1010
{warning_name, arguments} = message
1111
base_name = Path.relative_to_cwd(file)
1212

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

1818
"""
19-
#{base_name}:#{line}:#{warning_name}
19+
#{base_name}:#{Utils.format_location(location)}:#{warning_name}
2020
#{string}
2121
"""
2222
rescue

Diff for: lib/dialyxir/formatter/github.ex

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ defmodule Dialyxir.Formatter.Github do
66
@behaviour Dialyxir.Formatter
77

88
@impl Dialyxir.Formatter
9-
def format({_tag, {file, line}, {warning_name, arguments}}) do
9+
def format({_tag, {file, location}, {warning_name, arguments}}) do
1010
base_name = Path.relative_to_cwd(file)
1111

1212
warning = Utils.warning(warning_name)
1313
string = warning.format_short(arguments)
1414

15-
"::warning file=#{base_name},line=#{line},title=#{warning_name}::#{string}"
15+
case location do
16+
{line, col} ->
17+
"::warning file=#{base_name},line=#{line},col=#{col},title=#{warning_name}::#{string}"
18+
19+
line ->
20+
"::warning file=#{base_name},line=#{line},title=#{warning_name}::#{string}"
21+
end
1622
end
1723
end

Diff for: lib/dialyxir/formatter/ignore_file.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Dialyxir.Formatter.IgnoreFile do
44
@behaviour Dialyxir.Formatter
55

66
@impl Dialyxir.Formatter
7-
def format({_tag, {file, _line}, {warning_name, _arguments}}) do
7+
def format({_tag, {file, _location}, {warning_name, _arguments}}) do
88
~s({"#{file}", :#{warning_name}},)
99
end
1010
end

Diff for: lib/dialyxir/formatter/ignore_file_strict.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule Dialyxir.Formatter.IgnoreFileStrict do
66
@behaviour Dialyxir.Formatter
77

88
@impl Dialyxir.Formatter
9-
def format({_tag, {file, _line}, {warning_name, arguments}}) do
9+
def format({_tag, {file, _location}, {warning_name, arguments}}) do
1010
warning = Utils.warning(warning_name)
1111
string = warning.format_short(arguments)
1212

Diff for: lib/dialyxir/formatter/short.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ defmodule Dialyxir.Formatter.Short do
66
@behaviour Dialyxir.Formatter
77

88
@impl Dialyxir.Formatter
9-
def format({_tag, {file, line}, {warning_name, arguments}}) do
9+
def format({_tag, {file, location}, {warning_name, arguments}}) do
1010
base_name = Path.relative_to_cwd(file)
1111

1212
warning = Utils.warning(warning_name)
1313
string = warning.format_short(arguments)
1414

15-
"#{base_name}:#{line}:#{warning_name} #{string}"
15+
"#{base_name}:#{Utils.format_location(location)}:#{warning_name} #{string}"
1616
end
1717
end

Diff for: lib/dialyxir/formatter/utils.ex

+6
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ defmodule Dialyxir.Formatter.Utils do
88
throw({:error, :unknown_warning, warning_name})
99
end
1010
end
11+
12+
@doc false
13+
@spec format_location(:erl_anno.location()) :: String.t()
14+
def format_location(location)
15+
def format_location({line, column}), do: "#{line}:#{column}"
16+
def format_location(line), do: "#{line}"
1117
end

Diff for: test/dialyxir/formatter_test.exs

+40
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,46 @@ defmodule Dialyxir.FormatterTest do
1414
Mix.Project.in_project(app, "test/fixtures/#{Atom.to_string(app)}", fn _ -> f.() end)
1515
end
1616

17+
describe "formats dialyzer warning" do
18+
if System.otp_release() >= "24" do
19+
for {formatter, message} <- %{
20+
Formatter.Dialyxir =>
21+
"lib/file/warning_type/line.ex:19:4:no_return\nFunction format_long/1 has no local return.",
22+
Formatter.Dialyzer =>
23+
"lib/file/warning_type/line.ex:19:4: Function format_long/1 has no local return",
24+
Formatter.Github =>
25+
"::warning file=lib/file/warning_type/line.ex,line=19,col=4,title=no_return::Function format_long/1 has no local return.",
26+
Formatter.IgnoreFileStrict =>
27+
~s|{"lib/file/warning_type/line.ex", "Function format_long/1 has no local return."},|,
28+
Formatter.IgnoreFile => ~s|{"lib/file/warning_type/line.ex", :no_return},|,
29+
# TODO: Remove if once only Elixir ~> 1.15 is supported
30+
Formatter.Raw =>
31+
if Version.match?(System.version(), "<= 1.15.0") do
32+
~s|{:warn_return_no_exit, {'lib/file/warning_type/line.ex', {19, 4}}, {:no_return, [:only_normal, :format_long, 1]}}|
33+
else
34+
~s|{:warn_return_no_exit, {~c"lib/file/warning_type/line.ex", {19, 4}}, {:no_return, [:only_normal, :format_long, 1]}}|
35+
end,
36+
Formatter.Short =>
37+
"lib/file/warning_type/line.ex:19:4:no_return Function format_long/1 has no local return."
38+
} do
39+
test "file location including column for #{formatter} formatter" do
40+
assert {:warn, [message], _unused_filters} =
41+
Formatter.format_and_filter(
42+
[
43+
{:warn_return_no_exit, {~c"lib/file/warning_type/line.ex", {19, 4}},
44+
{:no_return, [:only_normal, :format_long, 1]}}
45+
],
46+
Project,
47+
[],
48+
unquote(formatter)
49+
)
50+
51+
assert message =~ unquote(message)
52+
end
53+
end
54+
end
55+
end
56+
1757
describe "exs ignore" do
1858
test "evaluates an ignore file and ignores warnings matching the pattern" do
1959
warnings = [

0 commit comments

Comments
 (0)