From 0c647d1ec9586e43ab542b8f76ae2f1908d46d67 Mon Sep 17 00:00:00 2001 From: Emily Guthrie Date: Wed, 5 Feb 2025 09:41:42 -0800 Subject: [PATCH] Enable custom ordering for module directives --- lib/quokka/config.ex | 12 ++++++++++++ lib/style/module_directives.ex | 11 ++--------- test/style/configs_test.exs | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/lib/quokka/config.ex b/lib/quokka/config.ex index ae757c2..082d5ad 100644 --- a/lib/quokka/config.ex +++ b/lib/quokka/config.ex @@ -20,6 +20,7 @@ defmodule Quokka.Config do alias Credo.Check.Readability.MultiAlias alias Credo.Check.Readability.ParenthesesOnZeroArityDefs alias Credo.Check.Readability.SinglePipe + alias Credo.Check.Readability.StrictModuleLayout alias Credo.Check.Refactor.PipeChainStart alias Quokka.Style.Configs @@ -58,6 +59,9 @@ defmodule Quokka.Config do reorder_configs = if is_nil(config[:reorder_configs]), do: true, else: config[:reorder_configs] + default_order = [:shortdoc, :moduledoc, :behaviour, :use, :import, :alias, :require] + strict_module_layout_order = credo_opts[:strict_module_layout_order] || default_order + :persistent_term.put(@key, %{ block_pipe_flag: credo_opts[:block_pipe_flag] || false, block_pipe_exclude: credo_opts[:block_pipe_exclude] || [], @@ -75,6 +79,7 @@ defmodule Quokka.Config do rewrite_multi_alias: credo_opts[:rewrite_multi_alias] || false, single_pipe_flag: credo_opts[:single_pipe_flag] || false, sort_order: credo_opts[:sort_order] || :alpha, + strict_module_layout_order: strict_module_layout_order ++ (default_order -- strict_module_layout_order), zero_arity_parens: credo_opts[:zero_arity_parens] || true }) end @@ -158,6 +163,10 @@ defmodule Quokka.Config do get(:single_pipe_flag) end + def strict_module_layout_order() do + get(:strict_module_layout_order) + end + def zero_arity_parens?() do get(:zero_arity_parens) end @@ -209,6 +218,9 @@ defmodule Quokka.Config do {SinglePipe, opts}, acc when is_list(opts) -> Map.put(acc, :single_pipe_flag, true) + {StrictModuleLayout, opts}, acc when is_list(opts) -> + Map.put(acc, :strict_module_layout_order, opts[:order]) + _, acc -> acc end) diff --git a/lib/style/module_directives.ex b/lib/style/module_directives.ex index 97eabf9..fd7e3b7 100644 --- a/lib/style/module_directives.ex +++ b/lib/style/module_directives.ex @@ -291,15 +291,8 @@ defmodule Quokka.Style.ModuleDirectives do nondirectives = acc.nondirectives directives = - [ - acc.shortdoc, - acc.moduledoc, - acc.behaviour, - acc.use, - acc.import, - acc.alias, - acc.require - ] + Quokka.Config.strict_module_layout_order() + |> Enum.map(&acc[&1]) |> Stream.concat() |> Style.fix_line_numbers(List.first(nondirectives)) diff --git a/test/style/configs_test.exs b/test/style/configs_test.exs index 5c9f1d2..7180042 100644 --- a/test/style/configs_test.exs +++ b/test/style/configs_test.exs @@ -15,6 +15,11 @@ defmodule Quokka.Style.ConfigsTest do alias Quokka.Style.Configs + setup do + Quokka.Config.set_for_test!(:strict_module_layout_flag, true) + Quokka.Config.set_for_test!(:strict_module_layout_order, [:shortdoc, :moduledoc, :behaviour, :use, :import, :alias, :require]) + end + test "only runs on exs files in config folders" do {ast, _} = Quokka.string_to_quoted_with_comments("import Config\n\nconfig :bar, boop: :baz") zipper = Quokka.Zipper.zip(ast) @@ -352,4 +357,22 @@ defmodule Quokka.Style.ConfigsTest do ) end end + + test "respects custom strict_module_layout_order" do + Quokka.Config.set_for_test!(:strict_module_layout_order, [:alias, :import, :moduledoc]) + assert_style( + """ + @moduledoc "Some doc" + alias Foo.Bar + import Enum + """, + """ + alias Foo.Bar + + import Enum + + @moduledoc "Some doc" + """ + ) + end end