Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/mix/lib/mix/tasks/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ defmodule Mix.Tasks.Format do
to be used by this task. For example, `["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]`.
Patterns are expanded with `Path.wildcard/2`.

* `:excludes` (a list of paths and patterns) - specifies the files to exclude from the
list of inputs to this task. For example, `["config/runtime.exs", "test/**/*.{ex,exs}"]`.
Patterns are expanded with `Path.wildcard/2`.

* `:plugins` (a list of modules) (since v1.13.0) - specifies a list of
modules to customize how the formatter works. See the "Plugins" section
below for more information.
Expand Down Expand Up @@ -634,11 +638,19 @@ defmodule Mix.Tasks.Format do
Mix.raise("Expected :inputs or :subdirectories key in #{dot_formatter}")
end

excluded_files =
formatter_opts
|> Map.get(:excludes, [])
|> List.wrap()
|> Enum.flat_map(&Path.wildcard(Path.expand(&1, cwd), match_dot: true))
|> MapSet.new()

map =
for input <- List.wrap(formatter_opts[:inputs]),
file <- Path.wildcard(Path.expand(input, cwd), match_dot: true),
do: {file, {dot_formatter, formatter_opts}},
into: %{}
formatter_opts[:inputs]
|> List.wrap()
|> Stream.flat_map(&Path.wildcard(Path.expand(&1, cwd), match_dot: true))
|> Stream.filter(fn file -> not MapSet.member?(excluded_files, file) end)
|> Enum.into(%{}, fn file -> {file, {dot_formatter, formatter_opts}} end)

acc =
Map.merge(acc, map, fn file, {dot_formatter1, _}, {dot_formatter2, formatter_opts} ->
Expand Down
29 changes: 29 additions & 0 deletions lib/mix/test/mix/tasks/format_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,35 @@ defmodule Mix.Tasks.FormatTest do
end)
end

test "ignores expanded patterns in excludes from .formatter.exs", context do
in_tmp(context.test, fn ->
File.write!(".formatter.exs", """
[
inputs: ["{a,.b}.ex"],
excludes: [".*.{ex,exs}"]
]
""")

File.write!("a.ex", """
foo bar
""")

File.write!(".b.ex", """
foo bar
""")

Mix.Tasks.Format.run([])

assert File.read!("a.ex") == """
foo(bar)
"""

assert File.read!(".b.ex") == """
foo bar
"""
end)
end

defmodule Elixir.SigilWPlugin do
@behaviour Mix.Tasks.Format

Expand Down