Class: RuboCop::Cop::Kaizo::PluralCollectionName
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Kaizo::PluralCollectionName
- Includes:
- AllowedMethods
- Defined in:
- lib/rubocop/cop/kaizo/plural_collection_name.rb
Overview
Checks that a method returning a collection is named in the plural. A
singular name on a method handing back an array (def user returning
[first, second]) misdescribes what the caller gets; the plural does the
documenting for free.
Ruby has no return types, so "returns an array" is a heuristic, and this
cop deliberately errs toward silence. A method is only flagged when
every value it can return is unambiguously an array: an array literal,
or a call to a method that returns an Array whatever its receiver
(ArrayMethods, e.g. map, to_a, sort). One branch returning nil
is enough to leave the method alone. Methods like select and reject
are absent by design -- on a Hash they hand back a Hash.
A name counts as plural when it ends in s or appears in
IrregularPlurals. Predicate (?), writer (=), and operator methods
are exempt, as is initialize, and AllowedMethods exempts names
outright. There is no autocorrection: renaming a method is a design
decision, and only its author knows the right plural.
Configuration
[+ArrayMethods+] Calls treated as returning an Array whatever their
receiver. Default: map, flat_map, collect,
collect_concat, to_a, entries, sort, sort_by,
zip.
[+IrregularPlurals+] Names accepted as plural despite not ending in
s. Default: people, children, men,
women, data, media, criteria.
[+AllowedMethods+] Method names never flagged. Default: none.
Extend a list without restating its default via RuboCop's inherit_mode: merge:
Kaizo/PluralCollectionName:
inherit_mode:
merge:
- ArrayMethods
- IrregularPlurals
ArrayMethods:
- fetch_all
IrregularPlurals:
- alumni
Constant Summary collapse
- MSG =
"Name a method that returns a collection in the plural. " \ "`%<name>s` returns an array.".freeze
- DEFAULT_ARRAY_METHODS =
Methods whose result is an
Arrayregardless of the receiver. Kept deliberately short: anything whose return type follows its receiver (selecton aHash) would turn this cop into a false-positive mill. %i[ map flat_map collect collect_concat to_a entries sort sort_by zip ].freeze
Instance Method Summary collapse
- #on_def(node) ⇒ Object (also: #on_defs)
Instance Method Details
#on_def(node) ⇒ Object Also known as: on_defs
79 80 81 82 83 84 |
# File 'lib/rubocop/cop/kaizo/plural_collection_name.rb', line 79 def on_def(node) return if exempt?(node) return unless returns_array?(node.body) add_offense(node.loc.name, message: format(MSG, name: node.method_name)) end |