Skip to content

Commit

Permalink
Merge pull request #697 from ityonemo/fix_issue_696
Browse files Browse the repository at this point in the history
fix: fixes issue #696
  • Loading branch information
rrrene authored Sep 1, 2019
2 parents d757fdb + ce64cb3 commit 56ebbef
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/credo/check/readability/function_names.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ defmodule Credo.Check.Readability.FunctionNames do
@all_sigil_chars ~w(a A b B c C d D e E f F g G h H i I j J k K l L m M n N o O p P q Q r R s S t T u U v V w W x X y Y z Z)
@all_sigil_atoms Enum.map(@all_sigil_chars, &:"sigil_#{&1}")

#all non-special-form operators
@all_nonspecial_operators ~W(! && ++ -- .. <> =~ @ |> || != !== * + - / <= == === > >= ||| &&& <<< >>> <<~ ~>> <~ ~> <~> <|> ^^^ ~~~)a

use Credo.Check, base_priority: :high

alias Credo.Code.Name
Expand Down Expand Up @@ -64,6 +67,12 @@ defmodule Credo.Check.Readability.FunctionNames do
{ast, issues}
end

# ignore non-special-form (overridable) operators
defp traverse({unquote(op), _meta, [{operator, _at_meta, _args} | _tail]} = ast, issues, _issue_meta)
when operator in @all_nonspecial_operators do
{ast, issues}
end

defp traverse({unquote(op), _meta, arguments} = ast, issues, issue_meta) do
{ast, issues_for_definition(arguments, issues, issue_meta)}
end
Expand Down
5 changes: 5 additions & 0 deletions lib/credo/check/readability/module_attribute_names.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ defmodule Credo.Check.Readability.ModuleAttributeNames do
Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
end

# ignore non-alphanumeric @ ASTs, for when you're redefining the @ macro.
defp traverse({:@, _meta, [{:{}, _, _}]} = ast, issues, _) do
{ast, issues}
end

defp traverse(
{:@, _meta, [{name, meta, _arguments}]} = ast,
issues,
Expand Down
15 changes: 15 additions & 0 deletions test/credo/check/readability/function_names_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ defmodule Credo.Check.Readability.FunctionNamesTest do
|> refute_issues(@described_check)
end

test "it should NOT report expected code (for operators) /6" do
"""
defmacro @expr2
defmacro @expr do
# ...
end
def left ++ right do
# ++ code
end
"""
|> to_source_file
|> refute_issues(@described_check)
end

#
# cases raising issues
#
Expand Down
14 changes: 14 additions & 0 deletions test/credo/check/readability/module_attribute_names_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ defmodule Credo.Check.Readability.ModuleAttributeNamesTest do
|> refute_issues(@described_check)
end

test "it should NOT fail when redefining the @ operator" do
"""
defmodule CredoSampleModule do
defmacro @{_, _, _} do
quote do
# some_code_here
end
end
end
"""
|> to_source_file
|> refute_issues(@described_check)
end

#
# cases raising issues
#
Expand Down

0 comments on commit 56ebbef

Please sign in to comment.