shard-ameba/src/ameba/rule/style/method_names.cr

59 lines
1,012 B
Crystal
Raw Normal View History

module Ameba::Rule::Style
2017-11-04 20:54:27 +00:00
# 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
# ```
#
# YAML configuration example:
#
# ```
# MethodNames:
# Enabled: true
# ```
#
2017-11-07 21:50:25 +00:00
struct MethodNames < Base
2017-11-22 06:44:29 +00:00
properties do
description "Enforces method names to be in underscored case"
2017-11-22 06:44:29 +00:00
end
MSG = "Method name should be underscore-cased: %s, not %s"
2017-11-04 20:54:27 +00:00
def test(source)
AST::NodeVisitor.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
2018-06-10 21:15:12 +00:00
issue_for node, MSG % {expected, node.name}
2017-11-04 20:54:27 +00:00
end
end
end