|
| 1 | +defmodule Mix.Tasks.Es6Maps.Format do |
| 2 | + @shortdoc "Replaces all map keys with their shorthand form" |
| 3 | + @moduledoc """ |
| 4 | + Replaces all map keys with their shorthand form. |
| 5 | +
|
| 6 | + ```shell |
| 7 | + mix es6_maps.format 'lib/**/*.ex' 'test/**/*.exs' |
| 8 | + ``` |
| 9 | +
|
| 10 | + The arguments are expanded with `Path.wildcard(match_dot: true)`. |
| 11 | +
|
| 12 | + The task manipulates the AST, not raw strings, so it's precise and will only change your code by: |
| 13 | +
|
| 14 | + 1. changing map keys into the shorthand form; |
| 15 | + 2. reordering map keys so the shorthand form comes first; |
| 16 | + 3. formatting the results with `mix format`. |
| 17 | +
|
| 18 | + ### Going back to old-style maps |
| 19 | +
|
| 20 | + You can revert all of the ES6-style shorthand uses with the `--revert` format flag: |
| 21 | +
|
| 22 | + ```shell |
| 23 | + mix es6_maps.format --revert lib/myapp/myapp.ex |
| 24 | + ``` |
| 25 | +
|
| 26 | + ### Reordering map keys |
| 27 | +
|
| 28 | + When applicable, the formatting will reorder the keys to shorthand them, for example: |
| 29 | +
|
| 30 | + ```elixir |
| 31 | + %{hello: "world", foo: foo, bar: bar} = var |
| 32 | + ``` |
| 33 | +
|
| 34 | + will become: |
| 35 | +
|
| 36 | + ```elixir |
| 37 | + %{foo, bar, hello: "world"} = var |
| 38 | + ``` |
| 39 | +
|
| 40 | + ## Options |
| 41 | + * `--revert` - Reverts the transformation. |
| 42 | + * `--locals-without-parens` - Specifies a list of locals that should not have parentheses. |
| 43 | + The format is `local_name/arity`, where `arity` can be an integer or `*`. This option can |
| 44 | + be given multiple times, and/or multiple values can be separated by commas. |
| 45 | + """ |
| 46 | + |
| 47 | + use Mix.Task |
| 48 | + |
| 49 | + @switches [revert: :boolean, locals_without_parens: :keep] |
| 50 | + |
| 51 | + @impl Mix.Task |
| 52 | + def run(all_args) do |
| 53 | + {opts, args} = OptionParser.parse!(all_args, strict: @switches) |
| 54 | + |
| 55 | + locals_without_parens = collect_locals_without_parens(opts) |
| 56 | + revert = Keyword.get(opts, :revert, false) |
| 57 | + opts = %{locals_without_parens: locals_without_parens, revert: revert} |
| 58 | + |
| 59 | + Enum.each(collect_paths(args), &format_file(&1, opts)) |
| 60 | + Mix.Tasks.Format.run(args) |
| 61 | + end |
| 62 | + |
| 63 | + defp collect_locals_without_parens(opts) do |
| 64 | + opts |
| 65 | + |> Keyword.get_values(:locals_without_parens) |
| 66 | + |> Enum.flat_map(&String.split(&1, ",")) |
| 67 | + |> Enum.map(fn local_str -> |
| 68 | + [fname_str, arity_str] = |
| 69 | + case String.split(local_str, "/", parts: 2) do |
| 70 | + [fname_str, arity_str] -> [fname_str, arity_str] |
| 71 | + _ -> raise ArgumentError, "invalid local: #{local_str}" |
| 72 | + end |
| 73 | + |
| 74 | + fname = String.to_atom(fname_str) |
| 75 | + arity = if arity_str == "*", do: :*, else: String.to_integer(arity_str) |
| 76 | + {fname, arity} |
| 77 | + end) |
| 78 | + end |
| 79 | + |
| 80 | + defp collect_paths(paths) do |
| 81 | + paths |> Enum.flat_map(&Path.wildcard(&1, match_dot: true)) |> Enum.filter(&File.regular?/1) |
| 82 | + end |
| 83 | + |
| 84 | + defp format_file(filepath, opts) do |
| 85 | + {quoted, comments} = |
| 86 | + filepath |
| 87 | + |> File.read!() |
| 88 | + |> Code.string_to_quoted_with_comments!( |
| 89 | + emit_warnings: false, |
| 90 | + literal_encoder: &{:ok, {:__block__, &2, [&1]}}, |
| 91 | + token_metadata: true, |
| 92 | + unescape: false, |
| 93 | + file: filepath |
| 94 | + ) |
| 95 | + |
| 96 | + quoted |
| 97 | + |> Macro.postwalk(&format_map(&1, opts)) |
| 98 | + |> Code.quoted_to_algebra( |
| 99 | + comments: comments, |
| 100 | + escape: false, |
| 101 | + locals_without_parens: opts.locals_without_parens |
| 102 | + ) |
| 103 | + |> Inspect.Algebra.format(:infinity) |
| 104 | + |> then(&File.write!(filepath, &1)) |
| 105 | + end |
| 106 | + |
| 107 | + defp format_map({:%{}, meta, [{:|, pipemeta, [lhs, elements]}]}, opts) do |
| 108 | + {_, _, mapped_elements} = format_map({:%{}, pipemeta, elements}, opts) |
| 109 | + {:%{}, meta, [{:|, pipemeta, [lhs, mapped_elements]}]} |
| 110 | + end |
| 111 | + |
| 112 | + defp format_map({:%{}, meta, elements}, %{revert: true}) do |
| 113 | + {:%{}, meta, |
| 114 | + Enum.map(elements, fn |
| 115 | + {key, _meta, context} = var when is_atom(context) -> {key, var} |
| 116 | + elem -> elem |
| 117 | + end)} |
| 118 | + end |
| 119 | + |
| 120 | + defp format_map({:%{}, meta, elements}, _opts) do |
| 121 | + {vars, key_vals} = |
| 122 | + Enum.reduce(elements, {[], []}, fn |
| 123 | + {{:__block__, _, [key]}, {key, _, ctx} = var}, {vars, key_vals} when is_atom(ctx) -> |
| 124 | + {[var | vars], key_vals} |
| 125 | + |
| 126 | + {_, _, ctx} = var, {vars, key_vals} when is_atom(ctx) -> |
| 127 | + {[var | vars], key_vals} |
| 128 | + |
| 129 | + key_val, {vars, key_vals} -> |
| 130 | + {vars, [key_val | key_vals]} |
| 131 | + end) |
| 132 | + |
| 133 | + {:%{}, meta, Enum.reverse(key_vals ++ vars)} |
| 134 | + end |
| 135 | + |
| 136 | + defp format_map(node, _opts), do: node |
| 137 | +end |
0 commit comments