Skip to content

Commit

Permalink
Improve snake_case automatic recommendation for underscore followed b…
Browse files Browse the repository at this point in the history
…y capital letter (#347)

Fixes #334
  • Loading branch information
Dany Marcoux authored Nov 24, 2022
1 parent 9c78531 commit 9309bb0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ defmodule ElixirAnalyzer.ExerciseTest.CommonChecks.VariableNames do
end

defp to_snake_case(name) do
# Macro.underscore is good enough because a module attribute name must be a valid Elixir identifier anyway
Macro.underscore(to_string(name))
name
|> to_string()
|> Macro.underscore()
|> String.replace(~r/_+/, "_")
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,27 @@ defmodule ElixirAnalyzer.ExerciseTest.CommonChecks.VariableNamesTest do
}}
]
end

test "it should report a violation /19" do
code =
quote do
defmodule CredoSampleModule do
def some_function(parameter1, parameter2) do
%{some_value: some_Value, other_value: otherValue} = parameter1
end
end
end

assert VariableNames.run(code) == [
{:fail,
%Comment{
type: :actionable,
name: Constants.solution_variable_name_snake_case(),
comment: Constants.solution_variable_name_snake_case(),
params: %{expected: "some_value", actual: "some_Value"}
}}
]
end
end

describe "unrelated code" do
Expand Down

0 comments on commit 9309bb0

Please sign in to comment.