Skip to content

Commit

Permalink
[gh315] Fix the credo on the web entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
andyduong1920 committed Mar 15, 2023
1 parent 20a369e commit d91d395
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/nimble_template/addons/credo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ defmodule NimbleTemplate.Addons.Credo do
project
|> inject_mix_dependency!()
|> edit_mix!()
|> edit_web_entry!()
end

defp inject_mix_dependency!(project) do
Expand All @@ -59,4 +60,47 @@ defmodule NimbleTemplate.Addons.Credo do

project
end

defp edit_web_entry!(
%Project{web_path: web_path, web_module: web_module, web_project?: true} = project
) do
Generator.delete_content!(
"#{web_path}.ex",
"""
def verified_routes do
quote do
use Phoenix.VerifiedRoutes,
endpoint: #{web_module}.Endpoint,
router: #{web_module}.Router,
statics: #{web_module}.static_paths()
end
end
"""
)

Generator.replace_content!(
"#{web_path}.ex",
"""
defp html_helpers do
""",
"""
def verified_routes do
quote do
use Phoenix.VerifiedRoutes,
endpoint: #{web_module}.Endpoint,
router: #{web_module}.Router,
statics: #{web_module}.static_paths()
end
end
defp html_helpers do
"""
)

project
end

defp edit_web_entry!(project) do
project
end
end
20 changes: 20 additions & 0 deletions lib/nimble_template/helpers/credo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule NimbleTemplate.CredoHelper do
alias NimbleTemplate.Generator
alias NimbleTemplate.Projects.Project

# TODO: Move this to the Credo Addon

@do_single_expression_rule_name "CompassCredoPlugin.Check.DoSingleExpression"
@single_module_file_rule_name "CompassCredoPlugin.Check.SingleModuleFile"

Expand All @@ -19,6 +21,8 @@ defmodule NimbleTemplate.CredoHelper do
project
|> get_files_containing_single_expression()
|> disable_rules(@do_single_expression_rule_name)

disable_on_core_components(project)
end

@spec suppress_credo_warnings_for_phoenix_api_project(Project.t()) :: :ok
Expand All @@ -32,6 +36,8 @@ defmodule NimbleTemplate.CredoHelper do
project
|> get_files_containing_single_expression()
|> disable_rules(@do_single_expression_rule_name)

disable_on_core_components(project)
end

defp get_files_containing_single_expression(%Project{
Expand Down Expand Up @@ -70,4 +76,18 @@ defmodule NimbleTemplate.CredoHelper do
""")
end
end

# TODO: Could remove the core_components file as we might not need it
defp disable_on_core_components(%Project{web_path: web_path}) do
core_components_path = "#{web_path}/components/core_components.ex"

if File.exists?(core_components_path) do
Generator.prepend_content(
core_components_path,
"""
# credo:disable-for-this-file
"""
)
end
end
end

0 comments on commit d91d395

Please sign in to comment.