Skip to content

Ignore block parameter names starting with _ #476

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 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion spec/ameba/rule/naming/block_parameter_name_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "../../../spec_helper"
module Ameba::Rule::Naming
subject = BlockParameterName.new
.tap(&.min_name_length = 3)
.tap(&.allowed_names = %w[_ e i j k v])
.tap(&.allowed_names = %w[e i j k v])

describe BlockParameterName do
it "passes if block parameter name matches #allowed_names" do
Expand All @@ -14,6 +14,12 @@ module Ameba::Rule::Naming
end
end

it "passes if block parameter name starts with '_'" do
expect_no_issues subject, <<-CRYSTAL
%w[].each { |_, _foo, _bar| }
CRYSTAL
end

it "fails if block parameter name doesn't match #allowed_names" do
expect_issue subject, <<-CRYSTAL
%w[].each { |x| }
Expand Down
4 changes: 2 additions & 2 deletions src/ameba/rule/naming/block_parameter_name.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Ameba::Rule::Naming
description "Disallows non-descriptive block parameter names"
min_name_length 3
allow_names_ending_in_numbers true
allowed_names %w[_ e i j k v x y ex io ws op tx id ip k1 k2 v1 v2]
allowed_names %w[e i j k v x y ex io ws op tx id ip k1 k2 v1 v2]
forbidden_names %w[]
end

Expand All @@ -42,7 +42,7 @@ module Ameba::Rule::Naming

private def valid_name?(name)
return true if name.blank? # TODO: handle unpacked variables
return true if name.in?(allowed_names)
return true if name.starts_with?('_') || name.in?(allowed_names)

return false if name.in?(forbidden_names)
return false if name.size < min_name_length
Expand Down