Skip to content

Commit 6cfb4e8

Browse files
committed
Attach :root to returned formatter options
1 parent 32e0b9c commit 6cfb4e8

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

lib/mix/lib/mix/tasks/format.ex

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,12 @@ defmodule Mix.Tasks.Format do
357357
be used for the given file.
358358
359359
The function must be called with the contents of the file
360-
to be formatted. The options are returned for reflection
361-
purposes.
360+
to be formatted. Keep in mind that a function is always
361+
returned, even if it doesn't match any of the inputs
362+
specified in the `formatter.exs`. You can retrieve the
363+
`:inputs` from the returned options, alongside the `:root`
364+
option, to validate if the returned file matches the given
365+
`:root` and `:inputs`.
362366
363367
## Options
364368
@@ -390,7 +394,7 @@ defmodule Mix.Tasks.Format do
390394

391395
formatter_opts_and_subs = load_plugins(formatter_opts_and_subs, opts)
392396

393-
find_formatter_and_opts_for_file(Path.expand(file, cwd), formatter_opts_and_subs)
397+
find_formatter_and_opts_for_file(Path.expand(file, cwd), cwd, formatter_opts_and_subs)
394398
end
395399

396400
@doc false
@@ -589,11 +593,11 @@ defmodule Mix.Tasks.Format do
589593
stdin_filename = Path.expand(Keyword.get(opts, :stdin_filename, "stdin.exs"), cwd)
590594

591595
{formatter, _opts} =
592-
find_formatter_and_opts_for_file(stdin_filename, {formatter_opts, subs})
596+
find_formatter_and_opts_for_file(stdin_filename, cwd, {formatter_opts, subs})
593597

594598
{file, formatter}
595599
else
596-
{formatter, _opts} = find_formatter_and_opts_for_file(file, {formatter_opts, subs})
600+
{formatter, _opts} = find_formatter_and_opts_for_file(file, cwd, {formatter_opts, subs})
597601
{file, formatter}
598602
end
599603
end
@@ -659,19 +663,19 @@ defmodule Mix.Tasks.Format do
659663
if plugins != [], do: plugins, else: nil
660664
end
661665

662-
defp find_formatter_and_opts_for_file(file, formatter_opts_and_subs) do
663-
formatter_opts = recur_formatter_opts_for_file(file, formatter_opts_and_subs)
664-
{find_formatter_for_file(file, formatter_opts), formatter_opts}
666+
defp find_formatter_and_opts_for_file(file, root, formatter_opts_and_subs) do
667+
{formatter_opts, root} = recur_formatter_opts_for_file(file, root, formatter_opts_and_subs)
668+
{find_formatter_for_file(file, formatter_opts), [root: root] ++ formatter_opts}
665669
end
666670

667-
defp recur_formatter_opts_for_file(file, {formatter_opts, subs}) do
668-
Enum.find_value(subs, formatter_opts, fn {sub, formatter_opts_and_subs} ->
671+
defp recur_formatter_opts_for_file(file, root, {formatter_opts, subs}) do
672+
Enum.find_value(subs, {formatter_opts, root}, fn {sub, formatter_opts_and_subs} ->
669673
size = byte_size(sub)
670674

671675
case file do
672676
<<prefix::binary-size(size), dir_separator, _::binary>>
673677
when prefix == sub and dir_separator in [?\\, ?/] ->
674-
recur_formatter_opts_for_file(file, formatter_opts_and_subs)
678+
recur_formatter_opts_for_file(file, sub, formatter_opts_and_subs)
675679

676680
_ ->
677681
nil

lib/mix/test/mix/tasks/format_test.exs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ defmodule Mix.Tasks.FormatTest do
608608

609609
root = File.cwd!()
610610

611-
{formatter_function, _options} =
611+
{formatter_function, options} =
612612
File.cd!("lib", fn ->
613613
Mix.Tasks.Format.formatter_for_file("lib/a.ex", root: root)
614614
end)
@@ -618,6 +618,8 @@ defmodule Mix.Tasks.FormatTest do
618618
""") == """
619619
foo bar(baz)
620620
"""
621+
622+
assert options[:root] == root
621623
end)
622624
end
623625

@@ -642,12 +644,14 @@ defmodule Mix.Tasks.FormatTest do
642644

643645
# Should hit the formatter
644646
{formatter, formatter_opts} = Mix.Tasks.Format.formatter_for_file("lib/extra/a.ex")
645-
assert Keyword.get(formatter_opts, :locals_without_parens) == [my_fun: 2]
647+
assert formatter_opts[:root] == Path.expand("lib")
648+
assert formatter_opts[:locals_without_parens] == [my_fun: 2]
646649
assert formatter.("my_fun 1, 2") == "my_fun 1, 2\n"
647650

648651
# Another directory should not hit the formatter
649652
{formatter, formatter_opts} = Mix.Tasks.Format.formatter_for_file("test/a.ex")
650-
assert Keyword.get(formatter_opts, :locals_without_parens) == []
653+
assert formatter_opts[:root] == Path.expand(".")
654+
assert formatter_opts[:locals_without_parens] == []
651655
assert formatter.("my_fun 1, 2") == "my_fun(1, 2)\n"
652656

653657
File.write!("lib/a.ex", """
@@ -718,7 +722,7 @@ defmodule Mix.Tasks.FormatTest do
718722
File.touch!(manifest_path, {{2010, 1, 1}, {0, 0, 0}})
719723

720724
{_, formatter_opts} = Mix.Tasks.Format.formatter_for_file("a.ex")
721-
assert [my_fun: 2] = Keyword.get(formatter_opts, :locals_without_parens)
725+
assert [my_fun: 2] = formatter_opts[:locals_without_parens]
722726

723727
# Check the deps_path option is respected
724728
assert_raise Mix.Error, ~r"Unknown dependency :my_dep given to :import_deps", fn ->
@@ -810,7 +814,8 @@ defmodule Mix.Tasks.FormatTest do
810814
Mix.Tasks.Format.run([])
811815

812816
{_, formatter_opts} = Mix.Tasks.Format.formatter_for_file("lib/extra/a.ex")
813-
assert [other_fun: 1] = Keyword.get(formatter_opts, :locals_without_parens)
817+
assert formatter_opts[:root] == Path.expand("lib/extra")
818+
assert formatter_opts[:locals_without_parens] == [other_fun: 1]
814819

815820
assert File.read!("lib/extra/a.ex") == """
816821
my_fun(:foo, :bar)

0 commit comments

Comments
 (0)