Skip to content

Commit c20d8a4

Browse files
committed
feat(ext-type-graph): implement reducible ast warning
1 parent 3f934d5 commit c20d8a4

4 files changed

Lines changed: 37 additions & 11 deletions

File tree

.formatter.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ locals_without_parens = [
2020
],
2121
locals_without_parens:
2222
[
23-
inspect: 1,
24-
inspect: 2
23+
inspect: :*,
24+
capture_io: :*
2525
] ++ locals_without_parens,
2626
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
2727
line_length: 200

lib/apix.schema.extensions/type_graph.ex

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ defmodule Apix.Schema.Extensions.TypeGraph do
44
alias Apix.Schema.Ast
55
alias Apix.Schema.Context
66

7+
alias Apix.Schema.Warning
8+
79
alias Apix.Schema.Extensions.TypeGraph.Graph
810

911
alias Apix.Schema.Extensions.TypeGraph.Errors.FullyRecursiveAstError
1012
alias Apix.Schema.Extensions.TypeGraph.Errors.UndefinedReferenceAstError
1113

14+
alias Apix.Schema.Extensions.TypeGraph.Warnings.ReducibleAstWarning
15+
1216
@manifest %Extension{
1317
module: __MODULE__
1418
}
@@ -367,6 +371,11 @@ defmodule Apix.Schema.Extensions.TypeGraph do
367371
"""
368372
@spec validate!() :: :ok | no_return()
369373
def validate! do
374+
validate_undefined_reference!()
375+
validate_fully_recursive!()
376+
end
377+
378+
defp validate_undefined_reference! do
370379
Graph.vertices()
371380
|> Enum.each(fn hash ->
372381
{^hash, context_or_ast} = Graph.vertex(hash)
@@ -375,7 +384,9 @@ defmodule Apix.Schema.Extensions.TypeGraph do
375384
raise UndefinedReferenceAstError, context_or_ast
376385
end
377386
end)
387+
end
378388

389+
defp validate_fully_recursive! do
379390
Graph.cyclic_strong_components_by(&(&1 == :references))
380391
|> Enum.each(fn [hash | _] = component ->
381392
component
@@ -409,6 +420,17 @@ defmodule Apix.Schema.Extensions.TypeGraph do
409420
end)
410421
end
411422

423+
defp validate_reducible!(context) do
424+
ast = context.ast
425+
reduced_ast = Context.normalize_ast!(context)
426+
427+
unless Ast.equals?(ast, reduced_ast) do
428+
[ast: ast, reduced_ast: reduced_ast]
429+
|> ReducibleAstWarning.exception()
430+
|> Warning.print()
431+
end
432+
end
433+
412434
@doc group: "Internal"
413435
@doc """
414436
Builds the sub/super-type relations in the graph.
@@ -592,6 +614,7 @@ defmodule Apix.Schema.Extensions.TypeGraph do
592614
@impl Extension
593615
def validate_ast!(context) do
594616
track!(context)
617+
validate_reducible!(context)
595618
on_compilation!()
596619

597620
context

lib/apix.schema.extensions/type_graph/warnings/reducible_ast_warning.ex

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ defmodule Apix.Schema.Extensions.TypeGraph.Warnings.ReducibleAstWarning do
4848

4949
%__MODULE__{
5050
message: """
51-
#{inspect ast} can be reduced to #{inspect reduced_ast} and remain equivalent.
52-
53-
If you intended that, rewrite it as #{inspect reduced_ast}.
54-
55-
#{ast.meta}
51+
#{inspect ast} can be reduced to #{inspect reduced_ast} and remain equivalent. If you intended that, rewrite it as #{inspect reduced_ast}.
5652
""",
5753
ast: ast,
5854
reduced_ast: reduced_ast,

test/apix.schema.extensions/type_graph_test.exs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule Apix.Schema.Extensions.TypeGraphTest do
22
use Apix.Schema.Case
33

4+
import ExUnit.CaptureIO
5+
46
alias Apix.Schema.Extensions.TypeGraph
57

68
alias Apix.Schema.Extensions.TypeGraph.Errors.FullyRecursiveAstError
@@ -611,11 +613,16 @@ defmodule Apix.Schema.Extensions.TypeGraphTest do
611613
end
612614

613615
test "#{inspect ReducibleAstWarning}" do
614-
defmodule TestSchema24 do
615-
# use Apix.Schema
616+
warning =
617+
capture_io :stderr, fn ->
618+
defmodule TestSchema24 do
619+
use Apix.Schema
616620

617-
# schema a: Any.t() or Any.t()
618-
end
621+
schema a: Any.t() or Any.t()
622+
end
623+
end
624+
625+
assert warning =~ "can be reduced"
619626
end
620627
end
621628
end

0 commit comments

Comments
 (0)