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

Add configuration for modules to exclude for alias lifting #140

Merged
merged 3 commits into from
Apr 5, 2024
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
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GregMefford lmk if you have an idea for clean config setting per test. i had it built into assert_style and honestly could still do that. maybe smart to just add a set_config in style_case that does the on_exit(fn -> set!([]) end)...


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