2018-06-16 11:50:59 +00:00
|
|
|
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
|
|
|
|
# ```
|
2017-11-14 07:24:36 +00:00
|
|
|
#
|
|
|
|
# YAML configuration example:
|
|
|
|
#
|
|
|
|
# ```
|
2018-09-03 19:53:11 +00:00
|
|
|
# Style/MethodNames:
|
2017-11-14 07:24:36 +00:00
|
|
|
# Enabled: true
|
|
|
|
# ```
|
2017-11-07 21:50:25 +00:00
|
|
|
struct MethodNames < Base
|
2017-11-22 06:44:29 +00:00
|
|
|
properties do
|
2018-05-03 15:57:47 +00:00
|
|
|
description "Enforces method names to be in underscored case"
|
2017-11-22 06:44:29 +00:00
|
|
|
end
|
|
|
|
|
2018-04-17 04:29:43 +00:00
|
|
|
MSG = "Method name should be underscore-cased: %s, not %s"
|
|
|
|
|
2017-11-04 20:54:27 +00:00
|
|
|
def test(source, node : Crystal::Def)
|
|
|
|
return if (expected = node.name.underscore) == node.name
|
|
|
|
|
2018-11-24 22:54:25 +00:00
|
|
|
line_number = node.location.try &.line_number
|
2019-06-06 16:10:26 +00:00
|
|
|
column_number = node.name_location.try &.column_number
|
2018-11-24 22:54:25 +00:00
|
|
|
|
2021-01-17 12:16:04 +00:00
|
|
|
return unless line_number && column_number
|
2018-11-24 22:54:25 +00:00
|
|
|
|
|
|
|
issue_for(
|
|
|
|
{line_number, column_number},
|
|
|
|
{line_number, column_number + node.name.size - 1},
|
|
|
|
MSG % {expected, node.name}
|
|
|
|
)
|
2017-11-04 20:54:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|