Skip to content

Commit

Permalink
Use intersection type
Browse files Browse the repository at this point in the history
  • Loading branch information
Goose97 committed Sep 1, 2023
1 parent 11428b9 commit 5ef6393
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
13 changes: 1 addition & 12 deletions lib/elixir_sense/core/metadata_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1690,18 +1690,7 @@ defmodule ElixirSense.Core.MetadataBuilder do
defp type_information_from_guards({:and, _, [guard_l, guard_r]}, state) do
left = type_information_from_guards(guard_l, state)
right = type_information_from_guards(guard_r, state)

Keyword.merge(left, right, fn _k, v1, v2 ->
case {v1, v2} do
# func my_func(x) when is_map_key(x, :a) and is_map_key(x, :b)
{{:map, fields1, _}, {:map, fields2, _}} ->
{:map, Enum.uniq_by(fields1 ++ fields2, &elem(&1, 0)), nil}

# In case we can't merge, just pick one
_ ->
v1
end
end)
Keyword.merge(left, right, fn _k, v1, v2 -> {:intersection, [v1, v2]} end)
end

defp type_information_from_guards({:or, _, [guard_l, guard_r]}, state) do
Expand Down
9 changes: 6 additions & 3 deletions test/elixir_sense/core/metadata_builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1567,10 +1567,13 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
end

test "and combination predicate guards can be merge" do
assert %VarInfo{name: :x, type: :number} = var_with_guards("is_number(x) and x >= 1")
assert %VarInfo{name: :x, type: {:intersection, [:number, :boolean]}} =
var_with_guards("is_number(x) and x >= 1")

assert %VarInfo{name: :x, type: {:map, [a: nil, b: nil], nil}} =
var_with_guards("is_map_key(x, :a) and is_map_key(x, :b)")
assert %VarInfo{
name: :x,
type: {:intersection, [{:map, [a: nil], nil}, {:map, [b: nil], nil}]}
} = var_with_guards("is_map_key(x, :a) and is_map_key(x, :b)")
end

test "or combination predicate guards can be merge into union type" do
Expand Down

0 comments on commit 5ef6393

Please sign in to comment.