-
Notifications
You must be signed in to change notification settings - Fork 13
Introduce a module for checks specific to Elixir #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
06f3d83
Add elixir checker module
Premwoik e2d89be
Add error messages for spec errors form ElixirChecker module
Premwoik dbf5d60
Add tests for ElixirChecker
Premwoik 9265453
Fix formatting
Premwoik cb4a98d
Add test for spec error fmt messages
Premwoik 9c5f3aa
Apply review comments
Premwoik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| defmodule Gradient.ElixirChecker do | ||
| @moduledoc ~s""" | ||
| Provide checks specific to Elixir that complement type checking delivered by Gradient. | ||
|
|
||
| Options: | ||
| - {`ex_check`, boolean()}: whether to use checks specific only to Elixir. | ||
| """ | ||
|
|
||
| @spec check([:erl_parse.abstract_form()], keyword()) :: [{:file.filename(), any()}] | ||
| def check(forms, opts) do | ||
| if Keyword.get(opts, :ex_check, true) do | ||
| check_spec(forms) | ||
| else | ||
| [] | ||
| end | ||
| end | ||
|
|
||
| @doc ~s""" | ||
| Check if all specs are exactly before the function that they specify | ||
| and if there is only one spec per function clause. | ||
|
|
||
| Correct spec locations: | ||
| ``` | ||
| @spec convert(integer()) :: float() | ||
| def convert(int) when is_integer(int), do: int / 1 | ||
|
|
||
| @spec convert(atom()) :: binary() | ||
| def convert(atom) when is_atom(atom), do: to_string(atom) | ||
| ``` | ||
|
|
||
| Incorrect spec locations: | ||
| - More than one spec above function clause. | ||
| ``` | ||
| @spec convert(integer()) :: float() | ||
| @spec convert(atom()) :: binary() | ||
| def convert(int) when is_integer(int), do: int / 1 | ||
|
|
||
| def convert(atom) when is_atom(atom), do: to_string(atom) | ||
| ``` | ||
|
|
||
| - Spec name doesn't match the function name. | ||
| ``` | ||
| @spec last_two(atom()) :: atom() | ||
| def last_three(:ok) do | ||
| :ok | ||
| end | ||
| ``` | ||
| """ | ||
| @spec check_spec([:erl_parse.abstract_form()]) :: [{:file.filename(), any()}] | ||
| def check_spec([{:attribute, _, :file, {file, _}} | forms]) do | ||
| forms | ||
| |> Stream.filter(&is_fun_or_spec?/1) | ||
| |> Stream.map(&simplify_form/1) | ||
| |> Stream.concat() | ||
| |> Stream.filter(&has_line/1) | ||
| |> Enum.sort(&(elem(&1, 2) < elem(&2, 2))) | ||
| |> Enum.reduce({nil, []}, fn | ||
| {:fun, fna, _} = fun, {{:spec, {n, a} = sna, anno}, errors} when fna != sna -> | ||
| # Spec name doesn't match the function name | ||
| {fun, [{:spec_error, :wrong_spec_name, anno, n, a} | errors]} | ||
|
|
||
| {:spec, {n, a}, anno} = s1, {{:spec, _, _}, errors} -> | ||
| # Only one spec per function clause is allowed | ||
| {s1, [{:spec_error, :spec_after_spec, anno, n, a} | errors]} | ||
|
|
||
| x, {_, errors} -> | ||
| {x, errors} | ||
| end) | ||
| |> elem(1) | ||
| |> Enum.map(&{file, &1}) | ||
| end | ||
|
|
||
| # Filter out __info__ generated function | ||
| def has_line(form), do: :erl_anno.line(elem(form, 2)) > 1 | ||
|
|
||
| def is_fun_or_spec?({:attribute, _, :spec, _}), do: true | ||
| def is_fun_or_spec?({:function, _, _, _, _}), do: true | ||
| def is_fun_or_spec?(_), do: false | ||
|
|
||
| @spec simplify_form(:erl_parse.abstract_form()) :: | ||
| Enumerable.t({:spec | :fun, {atom(), integer()}, :erl_anno.anno()}) | ||
| def simplify_form({:attribute, _, :spec, {{name, arity}, types}}) do | ||
| Stream.map(types, &{:spec, {name, arity}, elem(&1, 1)}) | ||
| end | ||
|
|
||
| def simplify_form({:function, _, name, arity, clauses}) do | ||
| Stream.map(clauses, &{:fun, {name, arity}, elem(&1, 1)}) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| defmodule SpecAfterSpec do | ||
| @spec convert(integer()) :: float() | ||
| @spec convert(atom()) :: binary() | ||
| def convert(int) when is_integer(int), do: int / 1 | ||
| def convert(atom) when is_atom(atom), do: to_string(atom) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| defmodule CorrectSpec do | ||
| @spec convert(integer()) :: float() | ||
| def convert(int) when is_integer(int), do: int / 1 | ||
| @spec convert(atom()) :: binary() | ||
| def convert(atom) when is_atom(atom), do: to_string(atom) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| defmodule SpecWrongName do | ||
| @spec convert(integer()) :: float() | ||
| def convert(int) when is_integer(int), do: int / 1 | ||
|
|
||
| @spec convert(atom()) :: binary() | ||
| def last_two(list) do | ||
| [last, penultimate | _tail] = Enum.reverse(list) | ||
| [penultimate, last] | ||
| end | ||
|
|
||
| @spec last_two(atom()) :: atom() | ||
| def last_three(:ok) do | ||
| :ok | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| defmodule Gradient.ElixirCheckerTest do | ||
| use ExUnit.Case | ||
| doctest Gradient.ElixirChecker | ||
|
|
||
| alias Gradient.ElixirChecker | ||
|
|
||
| import Gradient.TestHelpers | ||
|
|
||
| test "checker options" do | ||
| ast = load("Elixir.SpecWrongName.beam") | ||
|
|
||
| assert [] = ElixirChecker.check(ast, ex_check: false) | ||
| assert [] != ElixirChecker.check(ast, ex_check: true) | ||
| end | ||
|
|
||
| test "all specs are correct" do | ||
| ast = load("Elixir.CorrectSpec.beam") | ||
|
|
||
| assert [] = ElixirChecker.check(ast, ex_check: true) | ||
| end | ||
|
|
||
| test "spec name doesn't match the function name" do | ||
| ast = load("Elixir.SpecWrongName.beam") | ||
|
|
||
| assert [ | ||
| {_, {:spec_error, :wrong_spec_name, 11, :last_two, 1}}, | ||
| {_, {:spec_error, :wrong_spec_name, 5, :convert, 1}} | ||
| ] = ElixirChecker.check(ast, []) | ||
| end | ||
|
|
||
| test "more than one spec per function clause is not allowed" do | ||
| ast = load("Elixir.SpecAfterSpec.beam") | ||
|
|
||
| assert [ | ||
| {_, {:spec_error, :spec_after_spec, 3, :convert_a, 1}} | ||
| ] = ElixirChecker.check(ast, []) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.