1 # Pleroma: A lightweight social networking server
2 # Originally taken from
3 # https://github.com/VeryBigThings/elixir_common/blob/master/lib/vbt/credo/check/consistency/file_location.ex
4 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
5 # SPDX-License-Identifier: AGPL-3.0-only
7 defmodule Credo.Check.Consistency.FileLocation do
10 # credo:disable-for-this-file Credo.Check.Readability.Specs
13 File location should follow the namespace hierarchy of the module it defines.
17 - `lib/my_system.ex` should define the `MySystem` module
18 - `lib/my_system/accounts.ex` should define the `MySystem.Accounts` module
20 @explanation [warning: @checkdoc]
29 # `use Credo.Check` required that module attributes are already defined, so we need
30 # to place these attributes
31 # before use/alias expressions.
32 # credo:disable-for-next-line VBT.Credo.Check.Consistency.ModuleLayout
33 use Credo.Check, category: :warning, base_priority: :high
37 def run(source_file, params \\ []) do
38 case verify(source_file, params) do
42 {:error, module, expected_file} ->
43 error(IssueMeta.for(source_file, params), module, expected_file)
47 defp verify(source_file, params) do
49 |> Path.relative_to_cwd()
50 |> verify(Code.ast(source_file), params)
54 def verify(relative_path, ast, params) do
55 if verify_path?(relative_path, params),
56 do: ast |> main_module() |> verify_module(relative_path, params),
60 defp verify_path?(relative_path, params) do
61 case Path.split(relative_path) do
62 ["lib" | _] -> not exclude?(relative_path, params)
63 ["test", "support" | _] -> false
64 ["test", "test_helper.exs"] -> false
65 ["test" | _] -> not exclude?(relative_path, params)
70 defp exclude?(relative_path, params) do
72 |> Keyword.get(:exclude, [])
73 |> Enum.any?(&String.starts_with?(relative_path, &1))
76 defp main_module(ast) do
77 {_ast, modules} = Macro.prewalk(ast, [], &traverse/2)
81 defp traverse({:defmodule, _meta, args}, modules) do
82 [{:__aliases__, _, name_parts}, _module_body] = args
83 {args, [Module.concat(name_parts) | modules]}
86 defp traverse(ast, state), do: {ast, state}
88 # empty file - shouldn't really happen, but we'll let it through
89 defp verify_module(nil, _relative_path, _params), do: :ok
91 defp verify_module(main_module, relative_path, params) do
92 parsed_path = parsed_path(relative_path, params)
95 expected_file_base(parsed_path.root, main_module) <>
96 Path.extname(parsed_path.allowed)
99 expected_file == parsed_path.allowed ->
102 special_namespaces?(parsed_path.allowed) ->
103 original_path = parsed_path.allowed
106 Enum.find(@special_namespaces, original_path, fn namespace ->
107 String.contains?(original_path, namespace)
110 allowed = String.replace(original_path, "/" <> namespace, "")
112 if expected_file == allowed,
114 else: {:error, main_module, expected_file}
117 {:error, main_module, expected_file}
121 defp special_namespaces?(path), do: String.contains?(path, @special_namespaces)
123 defp parsed_path(relative_path, params) do
124 parts = Path.split(relative_path)
127 Keyword.get(params, :ignore_folder_namespace, %{})
128 |> Stream.flat_map(fn {root, folders} -> Enum.map(folders, &Path.join([root, &1])) end)
129 |> Stream.map(&Path.split/1)
130 |> Enum.find(&List.starts_with?(parts, &1))
136 Stream.drop(ignore_parts, -1)
137 |> Enum.concat(Stream.drop(parts, length(ignore_parts)))
141 %{root: hd(parts), allowed: allowed}
144 defp expected_file_base(root_folder, module) do
145 {parent_namespace, module_name} = module |> Module.split() |> Enum.split(-1)
148 if parent_namespace == [],
150 else: parent_namespace |> Module.concat() |> Macro.underscore()
152 file_name = module_name |> Module.concat() |> Macro.underscore()
154 Path.join([root_folder, relative_path, file_name])
157 defp error(issue_meta, module, expected_file) do
158 format_issue(issue_meta,
160 "Mismatch between file name and main module #{inspect(module)}. " <>
161 "Expected file path to be #{expected_file}. " <>
162 "Either move the file or rename the module.",