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

56 lines
987 B
Crystal
Raw Normal View History

2023-11-08 23:16:29 +00:00
module Ameba::Rule::Naming
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:
#
# ```
2023-11-08 23:16:29 +00:00
# Naming/MethodNames:
# Enabled: true
# ```
2021-01-18 15:45:35 +00:00
class 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, node : Crystal::Def)
2023-11-14 02:40:14 +00:00
name = node.name.to_s
2022-12-08 13:06:16 +00:00
2023-11-14 02:40:14 +00:00
return if (expected = name.underscore) == name
issue_for node, MSG % {expected, name}, prefer_name_location: true
2017-11-04 20:54:27 +00:00
end
end
end