Skip to content

Commit 04f1b7c

Browse files
authored
Add options to mix format to allow excluding of files (#14702)
1 parent 75a3272 commit 04f1b7c

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ defmodule Mix.Tasks.Format do
3333
to be used by this task. For example, `["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]`.
3434
Patterns are expanded with `Path.wildcard/2`.
3535
36+
* `:excludes` (a list of paths and patterns) - specifies the files to exclude from the
37+
list of inputs to this task. For example, `["config/runtime.exs", "test/**/*.{ex,exs}"]`.
38+
Patterns are expanded with `Path.wildcard/2`.
39+
3640
* `:plugins` (a list of modules) (since v1.13.0) - specifies a list of
3741
modules to customize how the formatter works. See the "Plugins" section
3842
below for more information.
@@ -634,9 +638,16 @@ defmodule Mix.Tasks.Format do
634638
Mix.raise("Expected :inputs or :subdirectories key in #{dot_formatter}")
635639
end
636640

641+
excluded_files =
642+
formatter_opts[:excludes]
643+
|> List.wrap()
644+
|> Enum.flat_map(&Path.wildcard(Path.expand(&1, cwd), match_dot: true))
645+
|> MapSet.new()
646+
637647
map =
638648
for input <- List.wrap(formatter_opts[:inputs]),
639649
file <- Path.wildcard(Path.expand(input, cwd), match_dot: true),
650+
file not in excluded_files,
640651
do: {file, {dot_formatter, formatter_opts}},
641652
into: %{}
642653

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,35 @@ defmodule Mix.Tasks.FormatTest do
279279
end)
280280
end
281281

282+
test "ignores expanded patterns in excludes from .formatter.exs", context do
283+
in_tmp(context.test, fn ->
284+
File.write!(".formatter.exs", """
285+
[
286+
inputs: ["{a,.b}.ex"],
287+
excludes: [".*.{ex,exs}"]
288+
]
289+
""")
290+
291+
File.write!("a.ex", """
292+
foo bar
293+
""")
294+
295+
File.write!(".b.ex", """
296+
foo bar
297+
""")
298+
299+
Mix.Tasks.Format.run([])
300+
301+
assert File.read!("a.ex") == """
302+
foo(bar)
303+
"""
304+
305+
assert File.read!(".b.ex") == """
306+
foo bar
307+
"""
308+
end)
309+
end
310+
282311
defmodule Elixir.SigilWPlugin do
283312
@behaviour Mix.Tasks.Format
284313

0 commit comments

Comments
 (0)