Skip to content

Commit

Permalink
Merge pull request #476 from crystal-ameba/fix-457
Browse files Browse the repository at this point in the history
Ignore block parameter names starting with `_`
  • Loading branch information
Sija authored Oct 10, 2024
2 parents 1abbb99 + 32f0664 commit eb76921
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
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

0 comments on commit eb76921

Please sign in to comment.