2017-11-04 20:54:27 +00:00
|
|
|
module Ameba::Rules
|
|
|
|
# A rule that enforces method names to be in underscored case.
|
|
|
|
#
|
|
|
|
# For example, these are considered valid:
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# class Person
|
|
|
|
# def first_name
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def date_of_birth
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def homepage_url
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# And these are invalid method names:
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# class Person
|
|
|
|
# def firstName
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def date_of_Birth
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def homepageURL
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# ```
|
|
|
|
struct MethodNames < Rule
|
|
|
|
def test(source)
|
2017-11-06 18:54:58 +00:00
|
|
|
AST::Visitor.new self, source
|
2017-11-04 20:54:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test(source, node : Crystal::Def)
|
|
|
|
return if (expected = node.name.underscore) == node.name
|
|
|
|
|
2017-11-07 20:02:51 +00:00
|
|
|
source.error self, node.location,
|
2017-11-04 20:54:27 +00:00
|
|
|
"Method name should be underscore-cased: #{expected}, not #{node.name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|