Skip to content

Commit

Permalink
Add configuration for modules to exclude for alias lifting (#140)
Browse files Browse the repository at this point in the history
Closes #139
  • Loading branch information
novaugust authored Apr 5, 2024
1 parent d0c403a commit 19fbef0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
9 changes: 1 addition & 8 deletions lib/style/module_directives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@ defmodule Styler.Style.ModuleDirectives do
@attr_directives ~w(moduledoc shortdoc behaviour)a ++ @callback_attrs
@defstruct ~w(schema embedded_schema defstruct)a

@stdlib MapSet.new(~w(
Access Agent Application Atom Base Behaviour Bitwise Code Date DateTime Dict Ecto Enum Exception
File Float GenEvent GenServer HashDict HashSet Integer IO Kernel Keyword List
Macro Map MapSet Module NaiveDateTime Node Oban OptionParser Path Port Process Protocol
Range Record Regex Registry Set Stream String StringIO Supervisor System Task Time Tuple URI Version
)a)

@moduledoc_false {:@, [line: nil], [{:moduledoc, [line: nil], [{:__block__, [line: nil], [false]}]}]}

def run({{:defmodule, _, children}, _} = zipper, ctx) do
Expand Down Expand Up @@ -231,7 +224,7 @@ defmodule Styler.Style.ModuleDirectives do

defp lift_aliases(aliases, requires, nondirectives) do
excluded =
Enum.reduce(aliases, @stdlib, fn
Enum.reduce(aliases, Styler.Config.get(:lifting_excludes), fn
{:alias, _, [{:__aliases__, _, aliases}]}, excluded -> MapSet.put(excluded, List.last(aliases))
{:alias, _, [{:__aliases__, _, _}, [{_as, {:__aliases__, _, [as]}}]]}, excluded -> MapSet.put(excluded, as)
# `alias __MODULE__` or other oddities
Expand Down
8 changes: 5 additions & 3 deletions lib/styler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ defmodule Styler do
@doc false
def style({ast, comments}, file, opts) do
on_error = opts[:on_error] || :log
Styler.Config.set(opts)
zipper = Zipper.zip(ast)

{{ast, _}, comments} =
Expand Down Expand Up @@ -61,13 +62,14 @@ defmodule Styler do
def features(_opts), do: [sigils: [], extensions: [".ex", ".exs"]]

@impl Format
def format(input, formatter_opts, opts \\ []) do
def format(input, formatter_opts) do
file = formatter_opts[:file]
styler_opts = formatter_opts[:styler] || []

{ast, comments} =
input
|> string_to_quoted_with_comments(to_string(file))
|> style(file, opts)
|> style(file, styler_opts)

quoted_to_string(ast, comments, formatter_opts)
end
Expand All @@ -84,7 +86,7 @@ defmodule Styler do
end

@doc false
def literal_encoder(a, b), do: {:ok, {:__block__, b, [a]}}
def literal_encoder(literal, meta), do: {:ok, {:__block__, meta, [literal]}}

@doc false
# Turns an ast and comments back into code, formatting it along the way.
Expand Down
46 changes: 46 additions & 0 deletions lib/styler/config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2024 Adobe. All rights reserved.
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.

defmodule Styler.Config do
@moduledoc false
@key __MODULE__

@stdlib MapSet.new(~w(
Access Agent Application Atom Base Behaviour Bitwise Code Date DateTime Dict Ecto Enum Exception
File Float GenEvent GenServer HashDict HashSet Integer IO Kernel Keyword List
Macro Map MapSet Module NaiveDateTime Node Oban OptionParser Path Port Process Protocol
Range Record Regex Registry Set Stream String StringIO Supervisor System Task Time Tuple URI Version
)a)

def set(config) do
:persistent_term.get(@key)
:ok
rescue
ArgumentError -> set!(config)
end

def set!(config) do
excludes =
config[:alias_lifting_exclude]
|> List.wrap()
|> MapSet.new()
|> MapSet.union(@stdlib)

:persistent_term.put(@key, %{
lifting_excludes: excludes
})
end

def get(key) do
@key
|> :persistent_term.get()
|> Map.fetch!(key)
end
end
13 changes: 13 additions & 0 deletions test/style/module_directives/alias_lifting_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ defmodule Styler.Style.ModuleDirectives.AliasLiftingTest do
end

describe "it doesn't lift" do
test "collisions with configured modules" do
Styler.Config.set!(alias_lifting_exclude: ~w(C)a)

assert_style """
alias Foo.Bar
A.B.C
A.B.C
"""

Styler.Config.set!([])
end

test "collisions with std lib" do
assert_style """
defmodule DontYouDare do
Expand Down
4 changes: 4 additions & 0 deletions test/support/style_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ defmodule Styler.StyleCase do
end
end

setup_all do
Styler.Config.set([])
end

defmacro assert_style(before, expected \\ nil) do
expected = expected || before

Expand Down

0 comments on commit 19fbef0

Please sign in to comment.