Skip to content

Commit 1228bbf

Browse files
gmilejosevalim
authored andcommitted
Run the code formatter on Version (#6666)
1 parent da2db9a commit 1228bbf

File tree

1 file changed

+95
-73
lines changed

1 file changed

+95
-73
lines changed

lib/elixir/lib/version.ex

+95-73
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,15 @@ defmodule Version do
8787
import Kernel, except: [match?: 2]
8888
defstruct [:major, :minor, :patch, :pre, :build]
8989

90-
@type version :: String.t | t
91-
@type requirement :: String.t | Version.Requirement.t
92-
@type major :: String.t | non_neg_integer
93-
@type minor :: non_neg_integer | nil
94-
@type patch :: non_neg_integer | nil
95-
@type pre :: [String.t | non_neg_integer]
96-
@type build :: String.t | nil
97-
@type matchable :: {major :: major,
98-
minor :: minor,
99-
patch :: patch,
100-
pre :: pre}
101-
@type t :: %__MODULE__{
102-
major: major,
103-
minor: minor,
104-
patch: patch,
105-
pre: pre,
106-
build: build}
90+
@type version :: String.t() | t
91+
@type requirement :: String.t() | Version.Requirement.t()
92+
@type major :: String.t() | non_neg_integer
93+
@type minor :: non_neg_integer | nil
94+
@type patch :: non_neg_integer | nil
95+
@type pre :: [String.t() | non_neg_integer]
96+
@type build :: String.t() | nil
97+
@type matchable :: {major :: major, minor :: minor, patch :: patch, pre :: pre}
98+
@type t :: %__MODULE__{major: major, minor: minor, patch: patch, pre: pre, build: build}
10799

108100
defmodule Requirement do
109101
@moduledoc false
@@ -119,7 +111,7 @@ defmodule Version do
119111
end
120112

121113
def message(%{requirement: requirement}) do
122-
"invalid requirement: #{inspect requirement}"
114+
"invalid requirement: #{inspect(requirement)}"
123115
end
124116
end
125117

@@ -131,7 +123,7 @@ defmodule Version do
131123
end
132124

133125
def message(%{version: version}) do
134-
"invalid version: #{inspect version}"
126+
"invalid version: #{inspect(version)}"
135127
end
136128
end
137129

@@ -172,6 +164,7 @@ defmodule Version do
172164
case parse_requirement(requirement) do
173165
{:ok, requirement} ->
174166
match?(version, requirement, opts)
167+
175168
:error ->
176169
raise InvalidRequirementError, requirement
177170
end
@@ -254,16 +247,16 @@ defmodule Version do
254247
:error
255248
256249
"""
257-
@spec parse(String.t) :: {:ok, t} | :error
250+
@spec parse(String.t()) :: {:ok, t} | :error
258251
def parse(string) when is_binary(string) do
259252
case Version.Parser.parse_version(string) do
260253
{:ok, {major, minor, patch, pre, build_parts}} ->
261254
build = if build_parts == [], do: nil, else: Enum.join(build_parts, "")
262-
version = %Version{major: major, minor: minor, patch: patch,
263-
pre: pre, build: build}
255+
version = %Version{major: major, minor: minor, patch: patch, pre: pre, build: build}
264256
{:ok, version}
265-
:error ->
266-
:error
257+
258+
:error ->
259+
:error
267260
end
268261
end
269262

@@ -281,7 +274,7 @@ defmodule Version do
281274
** (Version.InvalidVersionError) invalid version: "2.0-alpha1"
282275
283276
"""
284-
@spec parse!(String.t) :: t | no_return
277+
@spec parse!(String.t()) :: t | no_return
285278
def parse!(string) when is_binary(string) do
286279
case parse(string) do
287280
{:ok, version} -> version
@@ -302,11 +295,12 @@ defmodule Version do
302295
:error
303296
304297
"""
305-
@spec parse_requirement(String.t) :: {:ok, Requirement.t} | :error
298+
@spec parse_requirement(String.t()) :: {:ok, Requirement.t()} | :error
306299
def parse_requirement(string) when is_binary(string) do
307300
case Version.Parser.parse_requirement(string) do
308301
{:ok, spec} ->
309302
{:ok, %Requirement{source: string, matchspec: spec, compiled: false}}
303+
310304
:error ->
311305
:error
312306
end
@@ -321,7 +315,7 @@ defmodule Version do
321315
can not be sent to a process on another node and still remain a valid
322316
compiled match_spec, nor can it be stored on disk).
323317
"""
324-
@spec compile_requirement(Requirement.t) :: Requirement.t
318+
@spec compile_requirement(Requirement.t()) :: Requirement.t()
325319
def compile_requirement(%Requirement{matchspec: spec} = req) do
326320
%{req | matchspec: :ets.match_spec_compile(spec), compiled: true}
327321
end
@@ -334,6 +328,7 @@ defmodule Version do
334328
case Version.Parser.parse_version(string) do
335329
{:ok, {major, minor, patch, pre, _build_parts}} ->
336330
{major, minor, patch, pre, allow_pre?}
331+
337332
:error ->
338333
raise InvalidVersionError, string
339334
end
@@ -352,8 +347,9 @@ defmodule Version do
352347
{"!=", :!=},
353348
{"!", :!=},
354349
{" or ", :||},
355-
{" and ", :&&},
350+
{" and ", :&&}
356351
]
352+
357353
for {string_op, atom_op} <- operators do
358354
def lexer(unquote(string_op) <> rest, acc) do
359355
lexer(rest, [unquote(atom_op) | acc])
@@ -373,8 +369,10 @@ defmodule Version do
373369
case head do
374370
head when is_binary(head) ->
375371
[<<head::binary, char::utf8>> | acc]
372+
376373
head when head in [:||, :&&] ->
377374
[<<char::utf8>>, :==, head | acc]
375+
378376
_other ->
379377
[<<char::utf8>>, head | acc]
380378
end
@@ -386,13 +384,13 @@ defmodule Version do
386384
Enum.reverse(acc)
387385
end
388386

389-
@spec parse_requirement(String.t) :: {:ok, term} | :error
387+
@spec parse_requirement(String.t()) :: {:ok, term} | :error
390388
def parse_requirement(source) do
391389
lexed = lexer(source, [])
392390
to_matchspec(lexed)
393391
end
394392

395-
@spec parse_version(String.t) :: {:ok, Version.matchable} | :error
393+
@spec parse_version(String.t()) :: {:ok, Version.matchable()} | :error
396394
def parse_version(string, approximate? \\ false) when is_binary(string) do
397395
destructure [version_with_pre, build], String.split(string, "+", parts: 2)
398396
destructure [version, pre], String.split(version_with_pre, "-", parts: 2)
@@ -412,6 +410,7 @@ defmodule Version do
412410
end
413411

414412
defp require_digits(nil), do: :error
413+
415414
defp require_digits(string) do
416415
if leading_zero?(string), do: :error, else: parse_digits(string, "")
417416
end
@@ -421,18 +420,19 @@ defmodule Version do
421420

422421
defp parse_digits(<<char, rest::binary>>, acc) when char in ?0..?9,
423422
do: parse_digits(rest, <<acc::binary, char>>)
424-
defp parse_digits(<<>>, acc) when byte_size(acc) > 0,
425-
do: {:ok, String.to_integer(acc)}
426-
defp parse_digits(_, _acc),
427-
do: :error
423+
424+
defp parse_digits(<<>>, acc) when byte_size(acc) > 0, do: {:ok, String.to_integer(acc)}
425+
defp parse_digits(_, _acc), do: :error
428426

429427
defp maybe_patch(patch, approximate?)
430428
defp maybe_patch(nil, true), do: {:ok, nil}
431429
defp maybe_patch(patch, _), do: require_digits(patch)
432430

433431
defp optional_dot_separated(nil), do: {:ok, []}
432+
434433
defp optional_dot_separated(string) do
435434
parts = String.split(string, ".")
435+
436436
if Enum.all?(parts, &(&1 != "" and valid_identifier?(&1))) do
437437
{:ok, parts}
438438
else
@@ -448,6 +448,7 @@ defmodule Version do
448448
else
449449
convert_parts_to_integer(rest, [integer | acc])
450450
end
451+
451452
:error ->
452453
convert_parts_to_integer(rest, [part | acc])
453454
end
@@ -482,17 +483,17 @@ defmodule Version do
482483
end
483484

484485
# or <op> | and <op>
485-
defp valid_requirement?(a, [b | next]) when is_atom(a) and is_atom(b) and a in [:'||', :'&&'] do
486+
defp valid_requirement?(a, [b | next]) when is_atom(a) and is_atom(b) and a in [:||, :&&] do
486487
valid_requirement?(b, next)
487488
end
488489

489490
# <version> or | <version> and
490-
defp valid_requirement?(a, [b | next]) when is_binary(a) and is_atom(b) and b in [:'||', :'&&'] do
491+
defp valid_requirement?(a, [b | next]) when is_binary(a) and is_atom(b) and b in [:||, :&&] do
491492
valid_requirement?(b, next)
492493
end
493494

494495
# or <version> | and <version>
495-
defp valid_requirement?(a, [b | next]) when is_atom(a) and is_binary(b) and a in [:'||', :'&&'] do
496+
defp valid_requirement?(a, [b | next]) when is_atom(a) and is_binary(b) and a in [:||, :&&] do
496497
valid_requirement?(b, next)
497498
end
498499

@@ -518,8 +519,8 @@ defmodule Version do
518519
defp to_matchspec(lexed) do
519520
if valid_requirement?(lexed) do
520521
first = to_condition(lexed)
521-
rest = Enum.drop(lexed, 2)
522-
{:ok, [{{:'$1', :'$2', :'$3', :'$4', :'$5'}, [to_condition(first, rest)], [:'$_']}]}
522+
rest = Enum.drop(lexed, 2)
523+
{:ok, [{{:"$1", :"$2", :"$3", :"$4", :"$5"}, [to_condition(first, rest)], [:"$_"]}]}
523524
else
524525
:error
525526
end
@@ -534,46 +535,54 @@ defmodule Version do
534535

535536
defp to_condition([:!=, version | _]) do
536537
matchable = parse_condition(version)
537-
main_condition(:'/=', matchable)
538+
main_condition(:"/=", matchable)
538539
end
539540

540541
defp to_condition([:~>, version | _]) do
541542
from = parse_condition(version, true)
542-
to = approximate_upper(from)
543+
to = approximate_upper(from)
543544

544-
{:andalso, to_condition([:>=, matchable_to_string(from)]),
545-
to_condition([:<, matchable_to_string(to)])}
545+
{
546+
:andalso,
547+
to_condition([:>=, matchable_to_string(from)]),
548+
to_condition([:<, matchable_to_string(to)])
549+
}
546550
end
547551

548552
defp to_condition([:>, version | _]) do
549553
{major, minor, patch, pre} = parse_condition(version)
550554

551-
{:andalso, {:orelse, main_condition(:>, {major, minor, patch}),
552-
{:andalso, main_condition(:==, {major, minor, patch}),
553-
pre_condition(:>, pre)}},
554-
no_pre_condition(pre)}
555+
{
556+
:andalso,
557+
{
558+
:orelse,
559+
main_condition(:>, {major, minor, patch}),
560+
{:andalso, main_condition(:==, {major, minor, patch}), pre_condition(:>, pre)}
561+
},
562+
no_pre_condition(pre)
563+
}
555564
end
556565

557566
defp to_condition([:>=, version | _]) do
558567
matchable = parse_condition(version)
559568

560-
{:orelse, main_condition(:==, matchable),
561-
to_condition([:>, version])}
569+
{:orelse, main_condition(:==, matchable), to_condition([:>, version])}
562570
end
563571

564572
defp to_condition([:<, version | _]) do
565573
{major, minor, patch, pre} = parse_condition(version)
566574

567-
{:orelse, main_condition(:<, {major, minor, patch}),
568-
{:andalso, main_condition(:==, {major, minor, patch}),
569-
pre_condition(:<, pre)}}
575+
{
576+
:orelse,
577+
main_condition(:<, {major, minor, patch}),
578+
{:andalso, main_condition(:==, {major, minor, patch}), pre_condition(:<, pre)}
579+
}
570580
end
571581

572582
defp to_condition([:<=, version | _]) do
573583
matchable = parse_condition(version)
574584

575-
{:orelse, main_condition(:==, matchable),
576-
to_condition([:<, version])}
585+
{:orelse, main_condition(:==, matchable), to_condition([:<, version])}
577586
end
578587

579588
defp to_condition(current, []) do
@@ -591,52 +600,65 @@ defmodule Version do
591600
defp parse_condition(version, approximate? \\ false) do
592601
case parse_version(version, approximate?) do
593602
{:ok, {major, minor, patch, pre, _build}} -> {major, minor, patch, pre}
594-
:error -> throw :invalid_matchspec
603+
:error -> throw(:invalid_matchspec)
595604
end
596605
end
597606

598607
defp main_condition(op, version) when tuple_size(version) == 3 do
599-
{op, {{:'$1', :'$2', :'$3'}},
600-
{:const, version}}
608+
{op, {{:"$1", :"$2", :"$3"}}, {:const, version}}
601609
end
602610

603611
defp main_condition(op, version) when tuple_size(version) == 4 do
604-
{op, {{:'$1', :'$2', :'$3', :'$4'}},
605-
{:const, version}}
612+
{op, {{:"$1", :"$2", :"$3", :"$4"}}, {:const, version}}
606613
end
607614

608615
defp pre_condition(:>, pre) do
609616
length_pre = length(pre)
610617

611-
{:orelse, {:andalso, {:==, {:length, :'$4'}, 0},
612-
{:const, length_pre != 0}},
613-
{:andalso, {:const, length_pre != 0},
614-
{:orelse, {:>, {:length, :'$4'}, length_pre},
615-
{:andalso, {:==, {:length, :'$4'}, length_pre},
616-
{:>, :'$4', {:const, pre}}}}}}
618+
{
619+
:orelse,
620+
{:andalso, {:==, {:length, :"$4"}, 0}, {:const, length_pre != 0}},
621+
{
622+
:andalso,
623+
{:const, length_pre != 0},
624+
{
625+
:orelse,
626+
{:>, {:length, :"$4"}, length_pre},
627+
{:andalso, {:==, {:length, :"$4"}, length_pre}, {:>, :"$4", {:const, pre}}}
628+
}
629+
}
630+
}
617631
end
618632

619633
defp pre_condition(:<, pre) do
620634
length_pre = length(pre)
621635

622-
{:orelse, {:andalso, {:'/=', {:length, :'$4'}, 0},
623-
{:const, length_pre == 0}},
624-
{:andalso, {:'/=', {:length, :'$4'}, 0},
625-
{:orelse, {:<, {:length, :'$4'}, length_pre},
626-
{:andalso, {:==, {:length, :'$4'}, length_pre},
627-
{:<, :'$4', {:const, pre}}}}}}
636+
{
637+
:orelse,
638+
{:andalso, {:"/=", {:length, :"$4"}, 0}, {:const, length_pre == 0}},
639+
{
640+
:andalso,
641+
{:"/=", {:length, :"$4"}, 0},
642+
{
643+
:orelse,
644+
{:<, {:length, :"$4"}, length_pre},
645+
{:andalso, {:==, {:length, :"$4"}, length_pre}, {:<, :"$4", {:const, pre}}}
646+
}
647+
}
648+
}
628649
end
629650

630651
defp no_pre_condition([]) do
631-
{:orelse, :'$5', {:==, {:length, :'$4'}, 0}}
652+
{:orelse, :"$5", {:==, {:length, :"$4"}, 0}}
632653
end
654+
633655
defp no_pre_condition(_pre) do
634656
{:const, true}
635657
end
636658

637659
defp matchable_to_string({major, minor, patch, pre}) do
638660
patch = if patch, do: "#{patch}", else: "0"
639-
pre = if pre != [], do: "-#{Enum.join(pre, ".")}"
661+
pre = if pre != [], do: "-#{Enum.join(pre, ".")}"
640662
"#{major}.#{minor}.#{patch}#{pre}"
641663
end
642664
end

0 commit comments

Comments
 (0)