From 5c05f3b6fe2d890e1438dacb4f8d263e760baec6 Mon Sep 17 00:00:00 2001 From: shibao Date: Sat, 4 Feb 2023 17:28:53 -0500 Subject: [PATCH] use credo style --- .credo.exs | 14 +++++++------- lib/lokal/accounts/user.ex | 4 +++- lib/lokal/release.ex | 7 +++++-- lib/lokal/repo/migrator.ex | 4 ++-- lib/lokal_web.ex | 12 ++++++------ lib/lokal_web/live/invite_live/index.ex | 2 +- lib/lokal_web/live/live_helpers.ex | 2 +- lib/lokal_web/views/email_view.ex | 3 +-- lib/lokal_web/views/error_view.ex | 2 +- priv/gettext/en/LC_MESSAGES/errors.po | 8 ++++---- priv/gettext/errors.pot | 8 ++++---- test/lokal/accounts_test.exs | 4 ++-- test/lokal_web/controllers/user_auth_test.exs | 2 +- .../user_confirmation_controller_test.exs | 3 +-- test/lokal_web/live/invite_live_test.exs | 4 ++-- test/lokal_web/views/error_view_test.exs | 5 ++--- test/support/conn_case.ex | 1 + test/support/data_case.ex | 8 +++----- 18 files changed, 47 insertions(+), 46 deletions(-) diff --git a/.credo.exs b/.credo.exs index 7bc729a1..d6ee0bb4 100644 --- a/.credo.exs +++ b/.credo.exs @@ -157,17 +157,17 @@ # # Controversial and experimental checks (opt-in, just replace `false` with `[]`) # - {Credo.Check.Consistency.MultiAliasImportRequireUse, false}, - {Credo.Check.Consistency.UnusedVariableNames, false}, + {Credo.Check.Consistency.MultiAliasImportRequireUse, []}, + {Credo.Check.Consistency.UnusedVariableNames, [force: :meaningful]}, {Credo.Check.Design.DuplicatedCode, false}, {Credo.Check.Readability.AliasAs, false}, {Credo.Check.Readability.BlockPipe, false}, {Credo.Check.Readability.ImplTrue, false}, {Credo.Check.Readability.MultiAlias, false}, - {Credo.Check.Readability.SeparateAliasRequire, false}, + {Credo.Check.Readability.SeparateAliasRequire, []}, {Credo.Check.Readability.SinglePipe, false}, {Credo.Check.Readability.Specs, false}, - {Credo.Check.Readability.StrictModuleLayout, false}, + {Credo.Check.Readability.StrictModuleLayout, []}, {Credo.Check.Readability.WithCustomTaggedTuple, false}, {Credo.Check.Refactor.ABCSize, false}, {Credo.Check.Refactor.AppendSingleItem, false}, @@ -176,9 +176,9 @@ {Credo.Check.Refactor.NegatedIsNil, false}, {Credo.Check.Refactor.PipeChainStart, false}, {Credo.Check.Refactor.VariableRebinding, false}, - {Credo.Check.Warning.LeakyEnvironment, false}, - {Credo.Check.Warning.MapGetUnsafePass, false}, - {Credo.Check.Warning.UnsafeToAtom, false} + {Credo.Check.Warning.LeakyEnvironment, []}, + {Credo.Check.Warning.MapGetUnsafePass, []}, + {Credo.Check.Warning.UnsafeToAtom, []} # # Custom checks can be created using `mix credo.gen.check`. diff --git a/lib/lokal/accounts/user.ex b/lib/lokal/accounts/user.ex index 8b31bc90..e75d84c8 100644 --- a/lib/lokal/accounts/user.ex +++ b/lib/lokal/accounts/user.ex @@ -15,7 +15,9 @@ defmodule Lokal.Accounts.User do :email, :confirmed_at, :role, - :locale + :locale, + :inserted_at, + :updated_at ]} @derive {Inspect, except: [:password]} @primary_key {:id, :binary_id, autogenerate: true} diff --git a/lib/lokal/release.ex b/lib/lokal/release.ex index eb88117d..0726cc5a 100644 --- a/lib/lokal/release.ex +++ b/lib/lokal/release.ex @@ -7,7 +7,9 @@ defmodule Lokal.Release do def rollback(repo, version) do load_app() - {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version)) + + {:ok, _fun_return, _apps} = + Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version)) end defp load_app do @@ -18,7 +20,8 @@ defmodule Lokal.Release do load_app() for repo <- Application.fetch_env!(@app, :ecto_repos) do - {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true)) + {:ok, _fun_return, _apps} = + Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true)) end end end diff --git a/lib/lokal/repo/migrator.ex b/lib/lokal/repo/migrator.ex index 86eb63d8..2c5ba306 100644 --- a/lib/lokal/repo/migrator.ex +++ b/lib/lokal/repo/migrator.ex @@ -6,11 +6,11 @@ defmodule Lokal.Repo.Migrator do use GenServer require Logger - def start_link(_) do + def start_link(_opts) do GenServer.start_link(__MODULE__, [], []) end - def init(_) do + def init(_opts) do migrate!() {:ok, nil} end diff --git a/lib/lokal_web.ex b/lib/lokal_web.ex index abe14b21..ff656855 100644 --- a/lib/lokal_web.ex +++ b/lib/lokal_web.ex @@ -72,15 +72,16 @@ defmodule LokalWeb do quote do use Phoenix.Router + import Phoenix.{Controller, LiveView.Router} + # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse import Plug.Conn - import Phoenix.Controller - import Phoenix.LiveView.Router end end def channel do quote do use Phoenix.Channel + # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse import LokalWeb.Gettext end end @@ -88,15 +89,14 @@ defmodule LokalWeb do defp view_helpers do quote do # Use all HTML functionality (forms, tags, etc) + # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse use Phoenix.HTML # Import LiveView and .heex helpers (live_render, link, <.form>, etc) - import Phoenix.Component - # Import basic rendering functionality (render, render_layout, etc) - import Phoenix.View - + import Phoenix.{Component, View} import LokalWeb.{ErrorHelpers, Gettext, LiveHelpers, ViewHelpers} + alias LokalWeb.Router.Helpers, as: Routes end end diff --git a/lib/lokal_web/live/invite_live/index.ex b/lib/lokal_web/live/invite_live/index.ex index 038bc475..20ca6226 100644 --- a/lib/lokal_web/live/invite_live/index.ex +++ b/lib/lokal_web/live/invite_live/index.ex @@ -124,7 +124,7 @@ defmodule LokalWeb.InviteLive.Index do end @impl true - def handle_event("copy_to_clipboard", _, socket) do + def handle_event("copy_to_clipboard", _params, socket) do prompt = dgettext("prompts", "Copied to clipboard") {:noreply, socket |> put_flash(:info, prompt)} end diff --git a/lib/lokal_web/live/live_helpers.ex b/lib/lokal_web/live/live_helpers.ex index 0c1e8821..77f34589 100644 --- a/lib/lokal_web/live/live_helpers.ex +++ b/lib/lokal_web/live/live_helpers.ex @@ -49,7 +49,7 @@ defmodule LokalWeb.LiveHelpers do id="modal-content" class="fade-in-scale w-full max-w-3xl relative pointer-events-auto overflow-hidden - px-8 py-4 sm:py-8 flex flex-col justify-center items-center + px-8 py-4 sm:py-8 flex flex-col justify-start items-center bg-white border-2 rounded-lg" > diff --git a/lib/lokal_web/views/email_view.ex b/lib/lokal_web/views/email_view.ex index d5ba376a..3900c9df 100644 --- a/lib/lokal_web/views/email_view.ex +++ b/lib/lokal_web/views/email_view.ex @@ -2,7 +2,6 @@ defmodule LokalWeb.EmailView do @moduledoc """ A view for email-related helper functions """ - alias LokalWeb.{Endpoint, HomeLive} - use LokalWeb, :view + alias LokalWeb.{Endpoint, HomeLive} end diff --git a/lib/lokal_web/views/error_view.ex b/lib/lokal_web/views/error_view.ex index 1585b468..200f6071 100644 --- a/lib/lokal_web/views/error_view.ex +++ b/lib/lokal_web/views/error_view.ex @@ -8,7 +8,7 @@ defmodule LokalWeb.ErrorView do case error_path do "404.html" -> dgettext("errors", "Not found") "401.html" -> dgettext("errors", "Unauthorized") - _ -> dgettext("errors", "Internal Server Error") + _other_path -> dgettext("errors", "Internal Server Error") end render("error.html", %{error_string: error_string}) diff --git a/priv/gettext/en/LC_MESSAGES/errors.po b/priv/gettext/en/LC_MESSAGES/errors.po index 333e47aa..b35ad8d1 100644 --- a/priv/gettext/en/LC_MESSAGES/errors.po +++ b/priv/gettext/en/LC_MESSAGES/errors.po @@ -179,22 +179,22 @@ msgstr "" msgid "You must confirm your account and log in to access this page." msgstr "" -#: lib/lokal/accounts/user.ex:142 +#: lib/lokal/accounts/user.ex:144 #, elixir-autogen, elixir-format msgid "did not change" msgstr "" -#: lib/lokal/accounts/user.ex:163 +#: lib/lokal/accounts/user.ex:165 #, elixir-autogen, elixir-format msgid "does not match password" msgstr "" -#: lib/lokal/accounts/user.ex:200 +#: lib/lokal/accounts/user.ex:202 #, elixir-autogen, elixir-format msgid "is not valid" msgstr "" -#: lib/lokal/accounts/user.ex:97 +#: lib/lokal/accounts/user.ex:99 #, elixir-autogen, elixir-format msgid "must have the @ sign and no spaces" msgstr "" diff --git a/priv/gettext/errors.pot b/priv/gettext/errors.pot index ef1d9bd8..dd871952 100644 --- a/priv/gettext/errors.pot +++ b/priv/gettext/errors.pot @@ -176,22 +176,22 @@ msgstr "" msgid "You must confirm your account and log in to access this page." msgstr "" -#: lib/lokal/accounts/user.ex:142 +#: lib/lokal/accounts/user.ex:144 #, elixir-autogen, elixir-format msgid "did not change" msgstr "" -#: lib/lokal/accounts/user.ex:163 +#: lib/lokal/accounts/user.ex:165 #, elixir-autogen, elixir-format msgid "does not match password" msgstr "" -#: lib/lokal/accounts/user.ex:200 +#: lib/lokal/accounts/user.ex:202 #, elixir-autogen, elixir-format msgid "is not valid" msgstr "" -#: lib/lokal/accounts/user.ex:97 +#: lib/lokal/accounts/user.ex:99 #, elixir-autogen, elixir-format msgid "must have the @ sign and no spaces" msgstr "" diff --git a/test/lokal/accounts_test.exs b/test/lokal/accounts_test.exs index 03ef2bf9..3ddac6db 100644 --- a/test/lokal/accounts_test.exs +++ b/test/lokal/accounts_test.exs @@ -316,7 +316,7 @@ defmodule Lokal.AccountsTest do end test "deletes all tokens for the given user", %{user: user} do - _ = Accounts.generate_user_session_token(user) + _session_token = Accounts.generate_user_session_token(user) {:ok, _} = Accounts.update_user_password(user, valid_user_password(), %{ @@ -513,7 +513,7 @@ defmodule Lokal.AccountsTest do end test "deletes all tokens for the given user", %{user: user} do - _ = Accounts.generate_user_session_token(user) + _session_token = Accounts.generate_user_session_token(user) {:ok, _} = Accounts.reset_user_password(user, %{"password" => "new valid password"}) refute Repo.get_by(UserToken, user_id: user.id) end diff --git a/test/lokal_web/controllers/user_auth_test.exs b/test/lokal_web/controllers/user_auth_test.exs index ea410095..349b52ac 100644 --- a/test/lokal_web/controllers/user_auth_test.exs +++ b/test/lokal_web/controllers/user_auth_test.exs @@ -116,7 +116,7 @@ defmodule LokalWeb.UserAuthTest do end test "does not authenticate if data is missing", %{conn: conn, current_user: current_user} do - _ = Accounts.generate_user_session_token(current_user) + _session_token = Accounts.generate_user_session_token(current_user) conn = UserAuth.fetch_current_user(conn, []) refute get_session(conn, :user_token) refute conn.assigns.current_user diff --git a/test/lokal_web/controllers/user_confirmation_controller_test.exs b/test/lokal_web/controllers/user_confirmation_controller_test.exs index a295b50d..af0670b4 100644 --- a/test/lokal_web/controllers/user_confirmation_controller_test.exs +++ b/test/lokal_web/controllers/user_confirmation_controller_test.exs @@ -5,8 +5,7 @@ defmodule LokalWeb.UserConfirmationControllerTest do use LokalWeb.ConnCase, async: true import LokalWeb.Gettext - alias Lokal.Accounts - alias Lokal.Repo + alias Lokal.{Accounts, Repo} @moduletag :user_confirmation_controller_test diff --git a/test/lokal_web/live/invite_live_test.exs b/test/lokal_web/live/invite_live_test.exs index 85c54a44..588752d8 100644 --- a/test/lokal_web/live/invite_live_test.exs +++ b/test/lokal_web/live/invite_live_test.exs @@ -40,7 +40,7 @@ defmodule LokalWeb.InviteLiveTest do # |> form("#invite-form", invite: @invalid_attrs) # |> render_change() =~ dgettext("errors", "can't be blank") - {:ok, _, html} = + {:ok, _live, html} = index_live |> form("#invite-form", invite: @create_attrs) |> render_submit() @@ -64,7 +64,7 @@ defmodule LokalWeb.InviteLiveTest do # |> form("#invite-form", invite: @invalid_attrs) # |> render_change() =~ dgettext("errors", "can't be blank") - {:ok, _, html} = + {:ok, _live, html} = index_live |> form("#invite-form", invite: @update_attrs) |> render_submit() diff --git a/test/lokal_web/views/error_view_test.exs b/test/lokal_web/views/error_view_test.exs index e6c7416b..40386242 100644 --- a/test/lokal_web/views/error_view_test.exs +++ b/test/lokal_web/views/error_view_test.exs @@ -5,12 +5,11 @@ defmodule LokalWeb.ErrorViewTest do use LokalWeb.ConnCase, async: true import LokalWeb.Gettext - - @moduletag :error_view_test - # Bring render/3 and render_to_string/3 for testing custom views import Phoenix.View + @moduletag :error_view_test + test "renders 404.html" do assert render_to_string(LokalWeb.ErrorView, "404.html", []) =~ dgettext("errors", "Not found") diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 9734d591..ded108b2 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -25,6 +25,7 @@ defmodule LokalWeb.ConnCase do # Import conveniences for testing with connections import Plug.Conn import Phoenix.ConnTest + # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse import Lokal.Fixtures import LokalWeb.ConnCase diff --git a/test/support/data_case.ex b/test/support/data_case.ex index c2a2b215..8d55a8f0 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -22,10 +22,8 @@ defmodule Lokal.DataCase do alias Lokal.Repo import Ecto - import Ecto.Changeset - import Ecto.Query - import Lokal.DataCase - import Lokal.Fixtures + import Ecto.{Changeset, Query} + import Lokal.{DataCase, Fixtures} end end @@ -45,7 +43,7 @@ defmodule Lokal.DataCase do """ def errors_on(changeset) do Ecto.Changeset.traverse_errors(changeset, fn {message, opts} -> - Regex.replace(~r"%{(\w+)}", message, fn _, key -> + Regex.replace(~r"%{(\w+)}", message, fn _capture, key -> opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string() end) end)