-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_join.ex
146 lines (118 loc) · 3.63 KB
/
map_join.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
defmodule CredoFixes.Fixes.Refactor.MapJoin do
@behaviour CredoFixes.Fixer
@impl true
def get_fixes(source) do
ast = Sourceror.parse_string!(source)
{_, fixes} = Macro.prewalk(ast, [], &get_fix/2)
Enum.reverse(fixes)
end
# TODO find a better way to do this, because there's a lot of
# repetition the fixes can explode in complexity if they need to work over
# several permutations of the same code
# Enum.join(Enum.map(a, b), c)
defp get_fix(
{{:., _, [{:__aliases__, _, [:Enum]}, :join]}, join_meta,
[{{:., _, [{:__aliases__, _, [:Enum]}, :map]}, map_meta, map_args}, joiner]} = ast,
fixes
) do
range = Sourceror.get_range(ast)
quoted = {{:., [], [{:__aliases__, [], [:Enum]}, :map_join]}, join_meta, map_args ++ [joiner]}
quoted =
Sourceror.append_comments(
quoted,
map_meta[:leading_comments]
)
fix = %{
change: Sourceror.to_string(quoted),
range: range
}
{quoted, [fix | fixes]}
end
# Enum.map(a, b) |> Enum.join(c)
defp get_fix(
{:|>, pipe_meta,
[
{{:., _, [{:__aliases__, _, [:Enum]}, :map]}, map_meta, map_args},
{{:., _, [{:__aliases__, _, [:Enum]}, :join]}, join_meta, joiner}
]} = ast,
fixes
) do
range = Sourceror.get_range(ast)
quoted = {{:., [], [{:__aliases__, [], [:Enum]}, :map_join]}, map_meta, map_args ++ joiner}
quoted =
Sourceror.append_comments(
quoted,
pipe_meta[:leading_comments] ++ join_meta[:leading_comments]
)
fix = %{
change: Sourceror.to_string(quoted),
range: range
}
{quoted, [fix | fixes]}
end
# a |> Enum.map(b) |> Enum.join(c)
defp get_fix(
{:|>, pipe1_meta,
[
{:|>, pipe2_meta,
[
base_arg,
{{:., _, [{:__aliases__, _, [:Enum]}, :map]}, map_meta, map_args}
]},
{{:., _, [{:__aliases__, _, [:Enum]}, :join]}, join_meta, join_args}
]} = ast,
fixes
) do
range = Sourceror.get_range(ast)
args = map_args ++ join_args
map_meta =
Keyword.update(map_meta, :leading_comments, [], fn comments ->
pipe2_meta[:leading_comments] ++ comments ++ join_meta[:leading_comments]
end)
|> Keyword.delete(:closing)
quoted =
{:|>, pipe1_meta,
[
base_arg,
{{:., [], [{:__aliases__, [], [:Enum]}, :map_join]}, map_meta, args}
]}
fix = %{
change: Sourceror.to_string(quoted),
range: range
}
{quoted, [fix | fixes]}
end
# Enum.join(a |> Enum.map(b), c)
defp get_fix(
{{:., _, [{:__aliases__, _, [:Enum]}, :join]}, join_meta,
[
{:|>, pipe_meta,
[
base_arg,
{{:., map_alias_meta, [{:__aliases__, _, [:Enum]}, :map]}, map_meta, map_args}
]},
joiner
]} = ast,
fixes
) do
range = Sourceror.get_range(ast)
joiner = Sourceror.prepend_comments(joiner, pipe_meta[:trailing_comments])
args = [base_arg] ++ map_args ++ [joiner]
meta =
Keyword.update(join_meta, :leading_comments, [], fn comments ->
comments ++
map_alias_meta[:leading_comments] ++
pipe_meta[:leading_comments] ++ map_meta[:leading_comments]
end)
|> Keyword.delete(:closing)
quoted = {{:., [], [{:__aliases__, [], [:Enum]}, :map_join]}, meta, args}
fix = %{
change: Sourceror.to_string(quoted),
range: range
}
{quoted, [fix | fixes]}
end
defp get_fix(ast, fixes) do
{ast, fixes}
end
end