1 # Originally taken from
2 # https://github.com/VeryBigThings/elixir_common/blob/master/lib/vbt/credo/check/consistency/file_location.ex
4 defmodule Credo.Check.Consistency.FileLocation do
7 # credo:disable-for-this-file Credo.Check.Readability.Specs
10 File location should follow the namespace hierarchy of the module it defines.
14 - `lib/my_system.ex` should define the `MySystem` module
15 - `lib/my_system/accounts.ex` should define the `MySystem.Accounts` module
17 @explanation [warning: @checkdoc]
26 # `use Credo.Check` required that module attributes are already defined, so we need
27 # to place these attributes
28 # before use/alias expressions.
29 # credo:disable-for-next-line VBT.Credo.Check.Consistency.ModuleLayout
30 use Credo.Check, category: :warning, base_priority: :high
34 def run(source_file, params \\ []) do
35 case verify(source_file, params) do
39 {:error, module, expected_file} ->
40 error(IssueMeta.for(source_file, params), module, expected_file)
44 defp verify(source_file, params) do
46 |> Path.relative_to_cwd()
47 |> verify(Code.ast(source_file), params)
51 def verify(relative_path, ast, params) do
52 if verify_path?(relative_path, params),
53 do: ast |> main_module() |> verify_module(relative_path, params),
57 defp verify_path?(relative_path, params) do
58 case Path.split(relative_path) do
59 ["lib" | _] -> not exclude?(relative_path, params)
60 ["test", "support" | _] -> false
61 ["test", "test_helper.exs"] -> false
62 ["test" | _] -> not exclude?(relative_path, params)
67 defp exclude?(relative_path, params) do
69 |> Keyword.get(:exclude, [])
70 |> Enum.any?(&String.starts_with?(relative_path, &1))
73 defp main_module(ast) do
74 {_ast, modules} = Macro.prewalk(ast, [], &traverse/2)
78 defp traverse({:defmodule, _meta, args}, modules) do
79 [{:__aliases__, _, name_parts}, _module_body] = args
80 {args, [Module.concat(name_parts) | modules]}
83 defp traverse(ast, state), do: {ast, state}
85 # empty file - shouldn't really happen, but we'll let it through
86 defp verify_module(nil, _relative_path, _params), do: :ok
88 defp verify_module(main_module, relative_path, params) do
89 parsed_path = parsed_path(relative_path, params)
92 expected_file_base(parsed_path.root, main_module) <>
93 Path.extname(parsed_path.allowed)
96 expected_file == parsed_path.allowed ->
99 special_namespaces?(parsed_path.allowed) ->
100 original_path = parsed_path.allowed
103 Enum.find(@special_namespaces, original_path, fn namespace ->
104 String.contains?(original_path, namespace)
107 allowed = String.replace(original_path, "/" <> namespace, "")
109 if expected_file == allowed,
111 else: {:error, main_module, expected_file}
114 {:error, main_module, expected_file}
118 defp special_namespaces?(path), do: String.contains?(path, @special_namespaces)
120 defp parsed_path(relative_path, params) do
121 parts = Path.split(relative_path)
124 Keyword.get(params, :ignore_folder_namespace, %{})
125 |> Stream.flat_map(fn {root, folders} -> Enum.map(folders, &Path.join([root, &1])) end)
126 |> Stream.map(&Path.split/1)
127 |> Enum.find(&List.starts_with?(parts, &1))
133 Stream.drop(ignore_parts, -1)
134 |> Enum.concat(Stream.drop(parts, length(ignore_parts)))
138 %{root: hd(parts), allowed: allowed}
141 defp expected_file_base(root_folder, module) do
142 {parent_namespace, module_name} = module |> Module.split() |> Enum.split(-1)
145 if parent_namespace == [],
147 else: parent_namespace |> Module.concat() |> Macro.underscore()
149 file_name = module_name |> Module.concat() |> Macro.underscore()
151 Path.join([root_folder, relative_path, file_name])
154 defp error(issue_meta, module, expected_file) do
155 format_issue(issue_meta,
157 "Mismatch between file name and main module #{inspect(module)}. " <>
158 "Expected file path to be #{expected_file}. " <>
159 "Either move the file or rename the module.",