From 36d5287449471cbb2ecca9b1e407d6807641d581 Mon Sep 17 00:00:00 2001 From: Matt Enlow Date: Tue, 2 Apr 2024 09:48:07 -0600 Subject: [PATCH] spike: organize config files --- lib/style/configs.ex | 34 ++++++++++++++++++++++++++++++++++ lib/styler.ex | 12 +++++++----- 2 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 lib/style/configs.ex diff --git a/lib/style/configs.ex b/lib/style/configs.ex new file mode 100644 index 00000000..b492b981 --- /dev/null +++ b/lib/style/configs.ex @@ -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 diff --git a/lib/styler.ex b/lib/styler.ex index 20159777..b6e4ba7e 100644 --- a/lib/styler.ex +++ b/lib/styler.ex @@ -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)