Skip to content

Commit

Permalink
Add flag to toggle rewriting deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
emkguts committed Feb 5, 2025
1 parent 74c75ae commit 21d57c0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Quokka can be configured in your `.formatter.exs` file
Quokka has several configuration options:

- `:reorder_configs`, which controls whether or not the configs in your `config/*.exs` files are alphabetized. This is true by default.

- `:rewrite_deprecations`, which controls whether or not Quokka will rewrite deprecated functions to their new form. This is true by default.
## WARNING: Quokka can change the behaviour of your program!

In some cases, this can introduce bugs. It goes without saying, but look over your changes before committing to main :)
Expand Down
16 changes: 11 additions & 5 deletions lib/quokka/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ defmodule Quokka.Config do
reorder_configs =
if is_nil(config[:reorder_configs]), do: true, else: config[:reorder_configs]

rewrite_deprecations =
if is_nil(config[:rewrite_deprecations]), do: true, else: config[:rewrite_deprecations]

:persistent_term.put(@key, %{
block_pipe_flag: credo_opts[:block_pipe_flag] || false,
block_pipe_exclude: credo_opts[:block_pipe_exclude] || [],
Expand All @@ -67,6 +70,7 @@ defmodule Quokka.Config do
pipe_chain_start_excluded_functions: credo_opts[:pipe_chain_start_excluded_functions] || [],
pipe_chain_start_excluded_argument_types: credo_opts[:pipe_chain_start_excluded_argument_types] || [],
reorder_configs: reorder_configs,
rewrite_deprecations: rewrite_deprecations,
lift_alias: credo_opts[:lift_alias] || false,
lift_alias_depth: credo_opts[:lift_alias_depth] || 0,
lift_alias_excluded_namespaces: MapSet.new(lift_alias_excluded_namespaces ++ @stdlib),
Expand All @@ -91,11 +95,13 @@ defmodule Quokka.Config do
end

def get_styles() do
if get(:reorder_configs) == true do
@styles
else
@styles -- [Configs]
end
@styles
|> maybe_remove_style(Configs, :reorder_configs)
|> maybe_remove_style(Quokka.Style.Deprecations, :rewrite_deprecations)
end

defp maybe_remove_style(styles, module, flag_name) do
if get(flag_name) != true, do: styles -- [module], else: styles
end

def sort_order() do
Expand Down
16 changes: 16 additions & 0 deletions test/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,20 @@ defmodule Quokka.ConfigTest do
test "no config is good times" do
assert :ok = set!([])
end

test "rewrite deprecations flag is respected" do
assert :ok = set!(rewrite_deprecations: false)
assert Quokka.Style.Deprecations not in Quokka.Config.get_styles()

assert :ok = set!(rewrite_deprecations: true)
assert Quokka.Style.Deprecations in Quokka.Config.get_styles()
end

test "reorder configs flag is respected" do
assert :ok = set!(reorder_configs: false)
assert Quokka.Style.Configs not in Quokka.Config.get_styles()

assert :ok = set!(reorder_configs: true)
assert Quokka.Style.Configs in Quokka.Config.get_styles()
end
end

0 comments on commit 21d57c0

Please sign in to comment.