1 defmodule Credo.Check.Consistency.FileLocation do
4 # credo:disable-for-this-file Credo.Check.Readability.Specs
7 File location should follow the namespace hierarchy of the module it defines.
11 - `lib/my_system.ex` should define the `MySystem` module
12 - `lib/my_system/accounts.ex` should define the `MySystem.Accounts` module
14 @explanation [warning: @checkdoc]
16 # `use Credo.Check` required that module attributes are already defined, so we need to place these attributes
17 # before use/alias expressions.
18 # credo:disable-for-next-line VBT.Credo.Check.Consistency.ModuleLayout
19 use Credo.Check, category: :warning, base_priority: :high
23 def run(source_file, params \\ []) do
24 case verify(source_file, params) do
28 {:error, module, expected_file} ->
29 error(IssueMeta.for(source_file, params), module, expected_file)
33 defp verify(source_file, params) do
35 |> Path.relative_to_cwd()
36 |> verify(Code.ast(source_file), params)
40 def verify(relative_path, ast, params) do
41 if verify_path?(relative_path, params),
42 do: ast |> main_module() |> verify_module(relative_path, params),
46 defp verify_path?(relative_path, params) do
47 case Path.split(relative_path) do
48 ["lib" | _] -> not exclude?(relative_path, params)
49 ["test", "support" | _] -> false
50 ["test", "test_helper.exs"] -> false
51 ["test" | _] -> not exclude?(relative_path, params)
56 defp exclude?(relative_path, params) do
58 |> Keyword.get(:exclude, [])
59 |> Enum.any?(&String.starts_with?(relative_path, &1))
62 defp main_module(ast) do
63 {_ast, modules} = Macro.prewalk(ast, [], &traverse/2)
67 defp traverse({:defmodule, _meta, args}, modules) do
68 [{:__aliases__, _, name_parts}, _module_body] = args
69 {args, [Module.concat(name_parts) | modules]}
72 defp traverse(ast, state), do: {ast, state}
74 # empty file - shouldn't really happen, but we'll let it through
75 defp verify_module(nil, _relative_path, _params), do: :ok
77 defp verify_module(main_module, relative_path, params) do
78 parsed_path = parsed_path(relative_path, params)
81 expected_file_base(parsed_path.root, main_module) <>
82 Path.extname(parsed_path.allowed)
84 if expected_file == parsed_path.allowed,
86 else: {:error, main_module, expected_file}
89 defp parsed_path(relative_path, params) do
90 parts = Path.split(relative_path)
93 Keyword.get(params, :ignore_folder_namespace, %{})
94 |> Stream.flat_map(fn {root, folders} -> Enum.map(folders, &Path.join([root, &1])) end)
95 |> Stream.map(&Path.split/1)
96 |> Enum.find(&List.starts_with?(parts, &1))
102 Stream.drop(ignore_parts, -1)
103 |> Enum.concat(Stream.drop(parts, length(ignore_parts)))
107 %{root: hd(parts), allowed: allowed}
110 defp expected_file_base(root_folder, module) do
111 {parent_namespace, module_name} = module |> Module.split() |> Enum.split(-1)
114 if parent_namespace == [],
116 else: parent_namespace |> Module.concat() |> Macro.underscore()
118 file_name = module_name |> Module.concat() |> Macro.underscore()
120 Path.join([root_folder, relative_path, file_name])
123 defp error(issue_meta, module, expected_file) do
124 format_issue(issue_meta,
126 "Mismatch between file name and main module #{inspect(module)}. " <>
127 "Expected file path to be #{expected_file}. " <>
128 "Either move the file or rename the module.",