Skip to content

Commit

Permalink
Support multi letter sigils in FunctionNames
Browse files Browse the repository at this point in the history
Refs #1124
  • Loading branch information
rrrene committed Apr 3, 2024
1 parent d066fc0 commit 4f52c24
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/credo/check/readability/function_names.ex
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,21 @@ defmodule Credo.Check.Readability.FunctionNames do
issues
end

defp issues_for_name(name, args, meta, issues, issue_meta, allow_acronyms?) do
if name |> to_string |> Name.snake_case?(allow_acronyms?) do
defp issues_for_name("sigil_" <> sigil_letters = name, args, meta, issues, issue_meta, _allow_acronyms?) do
multi_letter_sigil? = String.match?(sigil_letters, ~r/^[A-Z]+$/)

if multi_letter_sigil? do
issues
else
issue = issue_for(issue_meta, meta[:line], name)
arity = length(args || [])

add_issue(issues, name, arity, issue)
end
end

defp issues_for_name("" <> name, args, meta, issues, issue_meta, allow_acronyms?) do
if Name.snake_case?(name, allow_acronyms?) do
issues
else
issue = issue_for(issue_meta, meta[:line], name)
Expand All @@ -160,6 +173,10 @@ defmodule Credo.Check.Readability.FunctionNames do
end
end

defp issues_for_name(name, args, meta, issues, issue_meta, allow_acronyms?) do
name |> to_string |> issues_for_name(args, meta, issues, issue_meta, allow_acronyms?)
end

defp issue_for(issue_meta, line_no, trigger) do
format_issue(
issue_meta,
Expand Down
17 changes: 17 additions & 0 deletions test/credo/check/readability/function_names_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ defmodule Credo.Check.Readability.FunctionNamesTest do
|> refute_issues()
end

test "it should NOT report expected code for multi letter sigils /5" do
"""
def sigil_ZZO(input, args) do
# ...
end
defmacro sigil_ZZU({:<<>>, _, [string]}, []) do
# ...
end
defmacro sigil_ZZU({:<<>>, _, [string]}, []) when is_binary(string) do
# ...
end
"""
|> to_source_file
|> run_check(@described_check)
|> refute_issues()
end

test "it should NOT report expected code (for operators) /6" do
"""
defmacro @expr2
Expand Down

0 comments on commit 4f52c24

Please sign in to comment.