diff --git a/spec/ameba/rule/predicate_name_spec.cr b/spec/ameba/rule/predicate_name_spec.cr index d7d1a24a..169c30dd 100644 --- a/spec/ameba/rule/predicate_name_spec.cr +++ b/spec/ameba/rule/predicate_name_spec.cr @@ -44,5 +44,16 @@ module Ameba::Rule error.message.should eq( "Favour method name 'picture?' over 'has_picture?'") end + + it "ignores if alternative name isn't valid syntax" do + s = Source.new %q( + class Image + def is_404?(x) + true + end + end + ) + subject.catch(s).should be_valid + end end end diff --git a/src/ameba/rule/predicate_name.cr b/src/ameba/rule/predicate_name.cr index c0b49d37..29e0b581 100644 --- a/src/ameba/rule/predicate_name.cr +++ b/src/ameba/rule/predicate_name.cr @@ -1,6 +1,6 @@ module Ameba::Rule # A rule that disallows tautological predicate names, meaning those that - # start with the prefix `has_` or the prefix `is_`. + # start with the prefix `has_` or the prefix `is_`. Ignores if the alternative isn't valid Crystal code (e.g. `is_404?`). # # Favour these: # @@ -40,8 +40,11 @@ module Ameba::Rule def test(source, node : Crystal::Def) if node.name =~ /^(is|has)_(\w+)\?/ + alternative = $2 + return unless alternative =~ /^[a-z_][a-zA-Z_0-9]*$/ + source.error self, node.location, - "Favour method name '#{$2}?' over '#{node.name}'" + "Favour method name '#{alternative}?' over '#{node.name}'" end end end