Ignores PredicateName if alternative isn't valid (e.g. is_404?)

This commit is contained in:
Hugo Abonizio 2018-04-12 09:52:09 -03:00 committed by V. Elenhaupt
parent 9e2ab9c002
commit d6c6af883d
2 changed files with 16 additions and 2 deletions

View File

@ -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

View File

@ -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