Skip to content

Commit

Permalink
spike: organize config files
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Apr 2, 2024
1 parent c29b841 commit 36d5287
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
34 changes: 34 additions & 0 deletions lib/style/configs.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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.Style.Configs do
def run({{:import, _, [{:__aliases__, _, [:Config]}]}, _} = zipper, %{config?: true} = ctx) do
{:skip, zipper, Map.put(ctx, :mix_config?, true)}
end

def run({{:config, _, args} = config, zm}, %{mix_config?: true} = ctx) when is_list(args) do
{configs, others} = Enum.split_while(zm.r, &match?({:config, _, [_|_]}, &1))
[config | configs] = Enum.sort_by([config | configs], &Styler.Style.update_all_meta(&1, fn _ -> nil end), :desc)
zm = %{zm | l: configs ++ zm.l, r: others}
{:skip, {config, zm}, ctx}
end

def run(zipper, %{config?: true} = ctx) do
{:cont, zipper, ctx}
end

def run(zipper, %{file: file} = ctx) do
if file =~ ~r|config/.*\.exs| do
{:cont, zipper, Map.put(ctx, :config?, true)}
else
{:halt, zipper, ctx}
end
end
end
12 changes: 7 additions & 5 deletions lib/styler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ defmodule Styler do
Styler.Style.SingleNode,
Styler.Style.Defs,
Styler.Style.Blocks,
Styler.Style.Deprecations
Styler.Style.Deprecations,
Styler.Style.Configs
]

@doc false
def style({ast, comments}, file, opts) do
on_error = opts[:on_error] || :log
zipper = Zipper.zip(ast)
context = %{comments: comments, file: file}

{{ast, _}, %{comments: comments}} =
Enum.reduce(@styles, {zipper, context}, fn style, {zipper, context} ->
{{ast, _}, comments} =
Enum.reduce(@styles, {zipper, comments}, fn style, {zipper, comments} ->
context = %{comments: comments, file: file}
try do
Zipper.traverse_while(zipper, context, &style.run/2)
{zipper, %{comments: comments}} = Zipper.traverse_while(zipper, context, &style.run/2)
{zipper, comments}
rescue
exception ->
exception = StyleError.exception(exception: exception, style: style, file: file)
Expand Down

0 comments on commit 36d5287

Please sign in to comment.