summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlexander Strizhakov <alex.strizhakov@gmail.com>2020-06-24 13:56:16 +0300
committerAlexander Strizhakov <alex.strizhakov@gmail.com>2020-10-13 16:43:59 +0300
commitb6eb7997f577c5821f21eb00f623631db2faad0b (patch)
treefc84e7b4fa55f7f1d051d67a8e2e762c00e74eb6 /lib
parent9f4fe5485b7132307c7eff0690c8443b4ad57405 (diff)
special namespaces for phoenix and api_spec
Diffstat (limited to 'lib')
-rw-r--r--lib/credo/check/consistency/file_location.ex39
1 files changed, 35 insertions, 4 deletions
diff --git a/lib/credo/check/consistency/file_location.ex b/lib/credo/check/consistency/file_location.ex
index 5ef17b894..08be2bcf9 100644
--- a/lib/credo/check/consistency/file_location.ex
+++ b/lib/credo/check/consistency/file_location.ex
@@ -1,3 +1,6 @@
+# Originally taken from
+# https://github.com/VeryBigThings/elixir_common/blob/master/lib/vbt/credo/check/consistency/file_location.ex
+
defmodule Credo.Check.Consistency.FileLocation do
@moduledoc false
@@ -13,7 +16,15 @@ defmodule Credo.Check.Consistency.FileLocation do
"""
@explanation [warning: @checkdoc]
- # `use Credo.Check` required that module attributes are already defined, so we need to place these attributes
+ @special_namespaces [
+ "controllers",
+ "views",
+ "operations",
+ "channels"
+ ]
+
+ # `use Credo.Check` required that module attributes are already defined, so we need
+ # to place these attributes
# before use/alias expressions.
# credo:disable-for-next-line VBT.Credo.Check.Consistency.ModuleLayout
use Credo.Check, category: :warning, base_priority: :high
@@ -81,11 +92,31 @@ defmodule Credo.Check.Consistency.FileLocation do
expected_file_base(parsed_path.root, main_module) <>
Path.extname(parsed_path.allowed)
- if expected_file == parsed_path.allowed,
- do: :ok,
- else: {:error, main_module, expected_file}
+ cond do
+ expected_file == parsed_path.allowed ->
+ :ok
+
+ special_namespaces?(parsed_path.allowed) ->
+ original_path = parsed_path.allowed
+
+ namespace =
+ Enum.find(@special_namespaces, original_path, fn namespace ->
+ String.contains?(original_path, namespace)
+ end)
+
+ allowed = String.replace(original_path, "/" <> namespace, "")
+
+ if expected_file == allowed,
+ do: :ok,
+ else: {:error, main_module, expected_file}
+
+ true ->
+ {:error, main_module, expected_file}
+ end
end
+ defp special_namespaces?(path), do: String.contains?(path, @special_namespaces)
+
defp parsed_path(relative_path, params) do
parts = Path.split(relative_path)