Skip to content

Commit 6f4d432

Browse files
Runs mix format on the codebase (#55)
Closes #54
1 parent 369bd83 commit 6f4d432

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2634
-2580
lines changed

config/config.exs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use Mix.Config
22

33
config :elixir, ansi_enabled: true
44

5-
config :js2e,
6-
templates_location: "./priv/templates/"
5+
config :js2e, templates_location: "./priv/templates/"
76

8-
import_config "#{Mix.env}.exs"
7+
import_config "#{Mix.env()}.exs"

lib/js2e.ex

+56-60
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ defmodule JS2E do
2323
alias JS2E.Parsers.{ParserWarning, ParserError}
2424
alias JS2E.Printers.PrinterError
2525

26-
@spec main([String.t]) :: :ok
26+
@spec main([String.t()]) :: :ok
2727
def main(args) do
28-
29-
{options, paths, errors} =
30-
OptionParser.parse(args, switches: [module_name: :string])
28+
{options, paths, errors} = OptionParser.parse(args, switches: [module_name: :string])
3129

3230
if length(paths) == 0 do
33-
IO.puts @moduledoc
31+
IO.puts(@moduledoc)
3432
exit(:normal)
3533
end
3634

@@ -42,22 +40,21 @@ defmodule JS2E do
4240
files = resolve_all_paths(paths)
4341

4442
if length(files) == 0 do
45-
print_error("Error: Could not find any " <>
46-
"JSON files in path: #{inspect paths}")
43+
print_error("Error: Could not find any " <> "JSON files in path: #{inspect(paths)}")
4744
exit(:no_files)
4845
end
4946

5047
output_path = create_output_dir(options)
5148
JS2E.generate(files, output_path)
5249
end
5350

54-
@spec resolve_all_paths([String.t]) :: [Path.t]
51+
@spec resolve_all_paths([String.t()]) :: [Path.t()]
5552
defp resolve_all_paths(paths) do
5653
paths
5754
|> Enum.filter(&File.exists?/1)
58-
|> Enum.reduce([], fn (filename, files) ->
55+
|> Enum.reduce([], fn filename, files ->
5956
cond do
60-
File.dir? filename ->
57+
File.dir?(filename) ->
6158
walk_directory(filename) ++ files
6259

6360
String.ends_with?(filename, ".json") ->
@@ -69,15 +66,15 @@ defmodule JS2E do
6966
end)
7067
end
7168

72-
@spec walk_directory(String.t) :: [String.t]
69+
@spec walk_directory(String.t()) :: [String.t()]
7370
defp walk_directory(dir) do
7471
dir
75-
|> File.ls!
72+
|> File.ls!()
7673
|> Enum.reduce([], fn file, files ->
7774
filename = "#{dir}/#{file}"
7875

7976
cond do
80-
File.dir? filename ->
77+
File.dir?(filename) ->
8178
walk_directory(filename) ++ files
8279

8380
String.ends_with?(file, ".json") ->
@@ -89,58 +86,55 @@ defmodule JS2E do
8986
end)
9087
end
9188

92-
@spec create_output_dir(list) :: String.t
89+
@spec create_output_dir(list) :: String.t()
9390
defp create_output_dir(options) do
94-
95-
output_path = if Keyword.has_key?(options, :module_name) do
96-
Keyword.get(options, :module_name)
97-
else
98-
"Domain"
99-
end
91+
output_path =
92+
if Keyword.has_key?(options, :module_name) do
93+
Keyword.get(options, :module_name)
94+
else
95+
"Domain"
96+
end
10097

10198
output_path
10299
|> File.mkdir_p!()
103100

104101
output_path
105102
end
106103

107-
@spec generate([String.t], String.t) :: :ok
104+
@spec generate([String.t()], String.t()) :: :ok
108105
def generate(schema_paths, module_name) do
109-
110-
Logger.info "Parsing JSON schema files!"
106+
Logger.info("Parsing JSON schema files!")
111107
parser_result = parse_schema_files(schema_paths)
112108
pretty_parser_warnings(parser_result.warnings)
113109

114110
if length(parser_result.errors) > 0 do
115111
pretty_parser_errors(parser_result.errors)
116-
117112
else
118-
Logger.info "Converting to Elm code!"
113+
Logger.info("Converting to Elm code!")
119114
printer_result = print_schemas(parser_result.schema_dict, module_name)
120115

121116
if length(printer_result.errors) > 0 do
122117
pretty_printer_errors(printer_result.errors)
123-
124118
else
125-
Logger.info "Printing Elm code to file(s)!"
119+
Logger.info("Printing Elm code to file(s)!")
126120

127121
file_dict = printer_result.file_dict
122+
128123
Enum.each(file_dict, fn {file_path, file_content} ->
129-
{:ok, file} = File.open file_path, [:write]
130-
IO.binwrite file, file_content
131-
File.close file
132-
Logger.info "Created file '#{file_path}'"
124+
{:ok, file} = File.open(file_path, [:write])
125+
IO.binwrite(file, file_content)
126+
File.close(file)
127+
Logger.info("Created file '#{file_path}'")
133128
end)
134129
end
135130
end
136131
end
137132

138-
@spec pretty_parser_warnings([ParserWarning.t]) :: :ok
133+
@spec pretty_parser_warnings([ParserWarning.t()]) :: :ok
139134
defp pretty_parser_warnings(warnings) do
140135
warnings
141136
|> Enum.each(fn {file_path, warnings} ->
142137
if length(warnings) > 0 do
143-
144138
warning_header()
145139

146140
warnings
@@ -150,45 +144,50 @@ defmodule JS2E do
150144
warning_type
151145
|> to_string
152146
|> String.replace("_", " ")
153-
|> String.downcase
147+
|> String.downcase()
154148

155-
padding = String.duplicate("-",
156-
max(0, 74 - String.length(pretty_warning_type) - String.length(file_path)))
149+
padding =
150+
String.duplicate(
151+
"-",
152+
max(0, 74 - String.length(pretty_warning_type) - String.length(file_path))
153+
)
157154

158155
warnings
159156
|> Enum.each(fn warning ->
160157
print_header("--- #{pretty_warning_type} #{padding} #{file_path}\n")
161-
IO.puts warning.message
158+
IO.puts(warning.message)
162159
end)
163160
end)
164-
165161
end
166162
end)
163+
167164
:ok
168165
end
169166

170-
@spec pretty_parser_errors([ParserError.t]) :: :ok
167+
@spec pretty_parser_errors([ParserError.t()]) :: :ok
171168
defp pretty_parser_errors(errors) do
172169
errors
173170
|> Enum.each(fn {file_path, errors} ->
174171
if length(errors) > 0 do
175-
176172
errors
177173
|> Enum.group_by(fn err -> err.error_type end)
178174
|> Enum.each(fn {error_type, errors} ->
179175
pretty_error_type =
180176
error_type
181177
|> to_string
182178
|> String.replace("_", " ")
183-
|> String.upcase
179+
|> String.upcase()
184180

185-
padding = String.duplicate("-",
186-
max(0, 74 - String.length(pretty_error_type) - String.length(file_path)))
181+
padding =
182+
String.duplicate(
183+
"-",
184+
max(0, 74 - String.length(pretty_error_type) - String.length(file_path))
185+
)
187186

188187
errors
189188
|> Enum.each(fn error ->
190189
print_header("--- #{pretty_error_type} #{padding} #{file_path}\n")
191-
IO.puts error.message
190+
IO.puts(error.message)
192191
end)
193192
end)
194193
end
@@ -197,52 +196,49 @@ defmodule JS2E do
197196
:ok
198197
end
199198

200-
@spec pretty_printer_errors([PrinterError.t]) :: :ok
199+
@spec pretty_printer_errors([PrinterError.t()]) :: :ok
201200
defp pretty_printer_errors(errors) do
202-
203201
errors
204202
|> Enum.each(fn {file_path, errors} ->
205203
if length(errors) > 0 do
206-
207204
errors
208205
|> Enum.group_by(fn err -> err.error_type end)
209206
|> Enum.each(fn {error_type, errors} ->
210-
211207
pretty_error_type =
212208
error_type
213209
|> to_string
214210
|> String.replace("_", " ")
215-
|> String.upcase
211+
|> String.upcase()
216212

217-
padding = String.duplicate("-",
218-
max(0, 74 - String.length(pretty_error_type) - String.length(file_path)))
213+
padding =
214+
String.duplicate(
215+
"-",
216+
max(0, 74 - String.length(pretty_error_type) - String.length(file_path))
217+
)
219218

220219
errors
221220
|> Enum.each(fn error ->
222221
print_header("--- #{pretty_error_type} #{padding} #{file_path}\n")
223-
IO.puts error.message
222+
IO.puts(error.message)
224223
end)
225224
end)
226-
227225
end
228226
end)
227+
229228
:ok
230229
end
231230

232231
defp print_error(str) do
233-
IO.puts IO.ANSI.format([:cyan, str])
232+
IO.puts(IO.ANSI.format([:cyan, str]))
234233
end
235234

236235
defp print_header(str) do
237-
IO.puts IO.ANSI.format([:cyan, str])
236+
IO.puts(IO.ANSI.format([:cyan, str]))
238237
end
239238

240239
defp warning_header do
241-
header = String.duplicate("^", 35) <>
242-
" WARNINGS " <>
243-
String.duplicate("^", 35)
240+
header = String.duplicate("^", 35) <> " WARNINGS " <> String.duplicate("^", 35)
244241

245-
IO.puts IO.ANSI.format([:yellow, header])
242+
IO.puts(IO.ANSI.format([:yellow, header]))
246243
end
247-
248244
end

lib/parser.ex

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ defmodule JS2E.Parser do
99
alias JS2E.Parsers.{RootParser, SchemaResult, ErrorUtil}
1010
alias JS2E.Printers.Util
1111

12-
@spec parse_schema_files([Path.t]) :: SchemaResult.t
12+
@spec parse_schema_files([Path.t()]) :: SchemaResult.t()
1313
def parse_schema_files(schema_paths) do
1414
init_schema_result = SchemaResult.new()
1515

1616
schema_paths
17-
|> Enum.reduce(init_schema_result, fn (schema_path, acc) ->
17+
|> Enum.reduce(init_schema_result, fn schema_path, acc ->
1818
schema_path
19-
|> File.read!
20-
|> Poison.decode!
19+
|> File.read!()
20+
|> Poison.decode!()
2121
|> RootParser.parse_schema(schema_path)
2222
|> SchemaResult.merge(acc)
2323
end)
2424
end
25-
2625
end

lib/parsers/all_of_parser.ex

+14-12
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ defmodule JS2E.Parsers.AllOfParser do
3030
"""
3131

3232
require Logger
33-
import JS2E.Parsers.Util, only: [
34-
parse_child_types: 3,
35-
create_types_list: 2,
36-
create_type_dict: 3
37-
]
33+
34+
import JS2E.Parsers.Util,
35+
only: [
36+
parse_child_types: 3,
37+
create_types_list: 2,
38+
create_type_dict: 3
39+
]
40+
3841
alias JS2E.Parsers.{ErrorUtil, ParserResult}
3942
alias JS2E.{Types, TypePath}
4043
alias JS2E.Types.AllOfType
@@ -55,20 +58,20 @@ defmodule JS2E.Parsers.AllOfParser do
5558
5659
"""
5760
@impl JS2E.Parsers.ParserBehaviour
58-
@spec type?(Types.node) :: boolean
61+
@spec type?(Types.node()) :: boolean
5962
def type?(%{"allOf" => all_of})
60-
when is_list(all_of) and length(all_of) > 0, do: true
63+
when is_list(all_of) and length(all_of) > 0,
64+
do: true
65+
6166
def type?(_schema_node), do: false
6267

6368
@doc ~S"""
6469
Parses a JSON schema allOf type into an `JS2E.Types.AllOfType`.
6570
"""
6671
@impl JS2E.Parsers.ParserBehaviour
67-
@spec parse(Types.node, URI.t, URI.t | nil, TypePath.t, String.t)
68-
:: ParserResult.t
72+
@spec parse(Types.node(), URI.t(), URI.t() | nil, TypePath.t(), String.t()) :: ParserResult.t()
6973
def parse(%{"allOf" => all_of}, parent_id, id, path, name)
70-
when is_list(all_of) do
71-
74+
when is_list(all_of) do
7275
child_path = TypePath.add_child(path, "allOf")
7376

7477
child_types_result =
@@ -86,5 +89,4 @@ defmodule JS2E.Parsers.AllOfParser do
8689
|> ParserResult.new()
8790
|> ParserResult.merge(child_types_result)
8891
end
89-
9092
end

0 commit comments

Comments
 (0)