Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run an initial mix format on the whole repo #19

Merged
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@
#
# {Credo.Check.Design.DuplicatedCode, false}
#
checks: %{
enabled: [
checks: [
#
## Consistency Checks
#
Expand Down Expand Up @@ -102,7 +101,7 @@
{Credo.Check.Readability.ModuleDoc, []},
{Credo.Check.Readability.ModuleNames, []},
{Credo.Check.Readability.ParenthesesInCondition, []},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, []},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, parens: true},
{Credo.Check.Readability.PipeIntoAnonymousFunctions, []},
{Credo.Check.Readability.PredicateFunctionNames, []},
{Credo.Check.Readability.PreferImplicitTry, []},
Expand Down Expand Up @@ -211,7 +210,6 @@
# Custom checks can be created using `mix credo.gen.check`.
#
]
}
}
]
}
38 changes: 26 additions & 12 deletions lib/quokka/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ defmodule Quokka.Config do
alias Credo.Check.Readability.SinglePipe
alias Credo.Check.Readability.StrictModuleLayout
alias Credo.Check.Refactor.PipeChainStart
alias Quokka.Style.Blocks
alias Quokka.Style.Configs
alias Quokka.Style.Defs
alias Quokka.Style.Deprecations
alias Quokka.Style.ModuleDirectives
alias Quokka.Style.Pipes
alias Quokka.Style.SingleNode

@key __MODULE__

@styles [
Quokka.Style.ModuleDirectives,
Quokka.Style.Pipes,
Quokka.Style.SingleNode,
Quokka.Style.Defs,
Quokka.Style.Blocks,
Quokka.Style.Deprecations,
ModuleDirectives,
Pipes,
SingleNode,
Defs,
Blocks,
Deprecations,
Configs
]

Expand All @@ -53,14 +59,19 @@ defmodule Quokka.Config do
def set!(config) do
credo_opts = extract_configs_from_credo()

lift_alias_excluded_namespaces = (credo_opts[:lift_alias_excluded_namespaces] || []) |> Enum.map(&Atom.to_string/1)
lift_alias_excluded_lastnames = (credo_opts[:lift_alias_excluded_lastnames] || []) |> Enum.map(&Atom.to_string/1)
lift_alias_excluded_namespaces =
(credo_opts[:lift_alias_excluded_namespaces] || []) |> Enum.map(&Atom.to_string/1)

lift_alias_excluded_lastnames =
(credo_opts[:lift_alias_excluded_lastnames] || []) |> Enum.map(&Atom.to_string/1)

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

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

rewrite_deprecations =
if is_nil(config[:rewrite_deprecations]), do: true, else: config[:rewrite_deprecations]
Expand Down Expand Up @@ -90,7 +101,7 @@ defmodule Quokka.Config do
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] || false,
zero_arity_parens: credo_opts[:zero_arity_parens] || false
})
end

Expand All @@ -107,8 +118,11 @@ defmodule Quokka.Config do

def get_styles() do
styles_to_remove =
for {module, flag_name} <- [{Configs, :reorder_configs}, {Quokka.Style.Deprecations, :rewrite_deprecations}],
do: (if get(flag_name), do: nil, else: module)
for {module, flag_name} <- [
{Configs, :reorder_configs},
{Deprecations, :rewrite_deprecations}
],
do: if(get(flag_name), do: nil, else: module)

@styles -- styles_to_remove
end
Expand Down
14 changes: 12 additions & 2 deletions lib/style.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ defmodule Quokka.Style do
# give it a block parent, then step back to the child - we can insert next to it now that it's in a block
defp wrap_in_block(zipper) do
zipper
|> Zipper.update(fn {_, meta, _} = node -> {:__block__, Keyword.take(meta, [:line]), [node]} end)
|> Zipper.update(fn {_, meta, _} = node ->
{:__block__, Keyword.take(meta, [:line]), [node]}
end)
|> Zipper.down()
end

Expand Down Expand Up @@ -161,10 +163,18 @@ defmodule Quokka.Style do
def reset_newlines(nodes), do: reset_newlines(nodes, [])

def reset_newlines([node], acc), do: Enum.reverse([set_newlines(node, 2) | acc])

def reset_newlines([node | nodes], acc), do: reset_newlines(nodes, [set_newlines(node, 1) | acc])

defp set_newlines({directive, meta, children}, newline) do
updated_meta = Keyword.update(meta, :end_of_expression, [newlines: newline], &Keyword.put(&1, :newlines, newline))
updated_meta =
Keyword.update(
meta,
:end_of_expression,
[newlines: newline],
&Keyword.put(&1, :newlines, newline)
)

{directive, updated_meta, children}
end

Expand Down
22 changes: 17 additions & 5 deletions lib/style/configs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ defmodule Quokka.Style.Configs do
{node, [], comments}
else
{mine, comments} = comments_for_lines(comments, line, last_line)
line_with_comments = (List.first(mine)[:line] || line) - (List.first(mine)[:previous_eol_count] || 1) + 1

line_with_comments =
(List.first(mine)[:line] || line) - (List.first(mine)[:previous_eol_count] || 1) + 1

if line_with_comments == start_line do
{node, mine, comments}
Expand All @@ -162,6 +164,7 @@ defmodule Quokka.Style.Configs do
end

{_, meta, _} = node

# @TODO what about comments that were free floating between blocks? i'm just ignoring them and maybe always will...
# kind of just want to shove them to the end though, so that they don't interrupt existing stanzas.
# i think that's accomplishable by doing a final call above that finds all comments in the comments list that weren't moved
Expand All @@ -188,16 +191,25 @@ defmodule Quokka.Style.Configs do

defp comments_for_lines([%{line: line} = comment | rev_comments], start, last, match, acc) do
cond do
line > last -> comments_for_lines(rev_comments, start, last, match, [comment | acc])
line >= start -> comments_for_lines(rev_comments, start, last, [comment | match], acc)
line > last ->
comments_for_lines(rev_comments, start, last, match, [comment | acc])

line >= start ->
comments_for_lines(rev_comments, start, last, [comment | match], acc)

# @TODO bug: match line looks like `x = :foo # comment for x`
# could account for that by pre-running the formatter on config files :/
line == start - 1 -> comments_for_lines(rev_comments, start - 1, last, [comment | match], acc)
true -> {match, Enum.reverse(rev_comments, [comment | acc])}
line == start - 1 ->
comments_for_lines(rev_comments, start - 1, last, [comment | match], acc)

true ->
{match, Enum.reverse(rev_comments, [comment | acc])}
end
end

defp accumulate([{:config, _, [_, _ | _]} = c | siblings], cs, as), do: accumulate(siblings, [c | cs], as)

defp accumulate([{:=, _, [_lhs, _rhs]} = a | siblings], cs, as), do: accumulate(siblings, cs, [a | as])

defp accumulate(rest, configs, assignments), do: {configs, assignments, rest}
end
4 changes: 3 additions & 1 deletion lib/style/deprecations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,17 @@ defmodule Quokka.Style.Deprecations do
{:ok, stop} <- extract_value_from_range(last),
true <- start > stop do
step = {:__block__, [token: "1", line: lm[:line]], [1]}
{:"..//", rm, [first, last, step]}
{:..//, rm, [first, last, step]}
else
_ -> range
end
end

# Extracts the positive or negative integer from the given range block
defp extract_value_from_range({:__block__, _, [value]}) when is_integer(value), do: {:ok, value}

defp extract_value_from_range({:-, _, [{:__block__, _, [value]}]}) when is_integer(value), do: {:ok, -value}

defp extract_value_from_range(_), do: :non_int

defp extract_date_value({:sigil_D, _, [{:<<>>, _, [date]}, []]}), do: Date.from_iso8601(date)
Expand Down
47 changes: 28 additions & 19 deletions lib/style/module_directives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,16 @@ defmodule Quokka.Style.ModuleDirectives do
ast = if Quokka.Config.rewrite_multi_alias?(), do: expand(ast), else: [ast]

# import and used might get hoisted above aliases, so need to dealias depending on the layout order
{before, _after} =
{before, _after} =
Quokka.Config.strict_module_layout_order()
|> Enum.split_while(& &1 != :alias)
|> Enum.split_while(&(&1 != :alias))

needs_dealiasing = directive in ~w(import use)a and Enum.member?(before, directive)

ast = if needs_dealiasing, do: AliasEnv.expand(acc.dealiases, ast), else: ast

dealiases = if directive == :alias, do: AliasEnv.define(acc.dealiases, ast), else: acc.dealiases
dealiases =
if directive == :alias, do: AliasEnv.define(acc.dealiases, ast), else: acc.dealiases

# the reverse accounts for `expand` putting things in reading order, whereas we're accumulating in reverse
%{acc | directive => Enum.reverse(ast, acc[directive]), dealiases: dealiases}
Expand Down Expand Up @@ -319,7 +320,11 @@ defmodule Quokka.Style.ModuleDirectives do
dealiases = AliasEnv.define(aliases)
already_lifted = Map.values(dealiases)
excluded = dealiases |> Map.keys() |> Enum.into(Quokka.Config.lift_alias_excluded_lastnames())
liftable = if Quokka.Config.lift_alias?(), do: find_liftable_aliases(requires ++ nondirectives, excluded, already_lifted), else: []

liftable =
if Quokka.Config.lift_alias?(),
do: find_liftable_aliases(requires ++ nondirectives, excluded, already_lifted),
else: []

if Enum.any?(liftable) do
# This is a silly hack that helps comments stay put.
Expand Down Expand Up @@ -370,7 +375,7 @@ defmodule Quokka.Style.ModuleDirectives do
{{:quote, _, _}, _} = zipper, lifts ->
{:skip, zipper, lifts}

{{:__aliases__, _, [first, _| _] = aliases}, _} = zipper, lifts ->
{{:__aliases__, _, [first, _ | _] = aliases}, _} = zipper, lifts ->
if Enum.all?(aliases, &is_atom/1) do
alias_string = Enum.join(aliases, ".")

Expand All @@ -384,7 +389,8 @@ defmodule Quokka.Style.ModuleDirectives do
last = List.last(aliases)

lifts =
if excluded_namespace_match or last in excluded or length(aliases) <= Quokka.Config.lift_alias_depth() do
if excluded_namespace_match or last in excluded or
length(aliases) <= Quokka.Config.lift_alias_depth() do
lifts
else
Map.update(lifts, last, {aliases, 1}, fn
Expand All @@ -394,13 +400,14 @@ defmodule Quokka.Style.ModuleDirectives do
_ -> :collision_with_last
end)
end
# given:
# C.foo()
# A.B.C.foo()
# A.B.C.foo()
# C.foo()
#
# lifting A.B.C would create a collision with C.

# given:
# C.foo()
# A.B.C.foo()
# A.B.C.foo()
# C.foo()
#
# lifting A.B.C would create a collision with C.
{:skip, zipper, Map.put(lifts, first, :collision_with_first)}
else
{:skip, zipper, lifts}
Expand Down Expand Up @@ -434,14 +441,16 @@ defmodule Quokka.Style.ModuleDirectives do

{{:alias, _, [{:__aliases__, _, [_, _ | _] = aliases}]}, _} = zipper ->
# the alias was aliased deeper down. we've lifted that alias to a root, so delete this alias
if aliases in to_alias and Enum.all?(aliases, &is_atom/1) and length(aliases) > Quokka.Config.lift_alias_depth(),
do: Zipper.remove(zipper),
else: zipper
if aliases in to_alias and Enum.all?(aliases, &is_atom/1) and
length(aliases) > Quokka.Config.lift_alias_depth(),
do: Zipper.remove(zipper),
else: zipper

{{:__aliases__, meta, [_, _ | _] = aliases}, _} = zipper ->
if aliases in to_alias and Enum.all?(aliases, &is_atom/1) and length(aliases) > Quokka.Config.lift_alias_depth(),
do: Zipper.replace(zipper, {:__aliases__, meta, [List.last(aliases)]}),
else: zipper
if aliases in to_alias and Enum.all?(aliases, &is_atom/1) and
length(aliases) > Quokka.Config.lift_alias_depth(),
do: Zipper.replace(zipper, {:__aliases__, meta, [List.last(aliases)]}),
else: zipper

zipper ->
zipper
Expand Down
17 changes: 9 additions & 8 deletions lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ defmodule Quokka.Style.Pipes do
# function_call(with, args) or sigils. sigils are allowed, function w/ args is not
defp valid_pipe_start?({fun, meta, args}) when is_atom(fun) do
not Quokka.Config.refactor_pipe_chain_starts?() or first_arg_excluded_type?(args) or
(custom_macro?(meta) and (not Quokka.Config.block_pipe_flag?() or fun in Quokka.Config.block_pipe_exclude())) or
(custom_macro?(meta) and
(not Quokka.Config.block_pipe_flag?() or fun in Quokka.Config.block_pipe_exclude())) or
"#{fun}" in Quokka.Config.pipe_chain_start_excluded_functions() or
String.match?("#{fun}", ~r/^sigil_[a-zA-Z]$/)
end
Expand All @@ -365,25 +366,25 @@ defmodule Quokka.Style.Pipes do
defp first_arg_excluded_type?([{:<<>>, _, _} | _]),
do: :bitstring in Quokka.Config.pipe_chain_start_excluded_argument_types()

defp first_arg_excluded_type?([{:&, _, _} | _]), do: :fn in Quokka.Config.pipe_chain_start_excluded_argument_types()

defp first_arg_excluded_type?([{:&, _, _} | _]),
do: :fn in Quokka.Config.pipe_chain_start_excluded_argument_types()

defp first_arg_excluded_type?([{:fn, _, _} | _]),
do: :fn in Quokka.Config.pipe_chain_start_excluded_argument_types()
defp first_arg_excluded_type?([{:fn, _, _} | _]), do: :fn in Quokka.Config.pipe_chain_start_excluded_argument_types()

defp first_arg_excluded_type?([{_, _, [arg1 | _]} | _]) do
case arg1 do
[{{:__block__, [format: :keyword, line: _], _}, _} | _] ->
:keyword in Quokka.Config.pipe_chain_start_excluded_argument_types() or :list in Quokka.Config.pipe_chain_start_excluded_argument_types()
:keyword in Quokka.Config.pipe_chain_start_excluded_argument_types() or
:list in Quokka.Config.pipe_chain_start_excluded_argument_types()

_ ->
get_type(arg1) in Quokka.Config.pipe_chain_start_excluded_argument_types()
end
end

defp first_arg_excluded_type?([[{{:__block__, [format: :keyword, line: _], _}, _} | _] | _]),
do: :keyword in Quokka.Config.pipe_chain_start_excluded_argument_types() or :list in Quokka.Config.pipe_chain_start_excluded_argument_types()
do:
:keyword in Quokka.Config.pipe_chain_start_excluded_argument_types() or
:list in Quokka.Config.pipe_chain_start_excluded_argument_types()

# Bare variables are not excluded
defp first_arg_excluded_type?(_), do: false
Expand Down
Loading