Skip to content

Commit

Permalink
Make Rails/PluralizationGrammar aware of byte methods
Browse files Browse the repository at this point in the history
These methods are also singular/pluralized, see https://api.rubyonrails.org/classes/Numeric.html
  • Loading branch information
Earlopain committed Jul 1, 2024
1 parent e160f23 commit bd8064d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1306](https://github.com/rubocop/rubocop-rails/pull/1306): Make `Rails/PluralizationGrammar` aware of byte methods. ([@earlopain][])
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ Rails/PluralizationGrammar:
Description: 'Checks for incorrect grammar when using methods like `3.day.ago`.'
Enabled: true
VersionAdded: '0.35'
VersionChanged: '<<next>>'

Rails/Presence:
Description: 'Checks code that can be written more easily using `Object#presence` defined by Active Support.'
Expand Down
43 changes: 28 additions & 15 deletions lib/rubocop/cop/rails/pluralization_grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,38 @@ module Rails
# # bad
# 3.day.ago
# 1.months.ago
# 5.megabyte
# 1.gigabyte
#
# # good
# 3.days.ago
# 1.month.ago
# 5.megabytes
# 1.gigabyte
class PluralizationGrammar < Base
extend AutoCorrector

SINGULAR_DURATION_METHODS = { second: :seconds,
minute: :minutes,
hour: :hours,
day: :days,
week: :weeks,
fortnight: :fortnights,
month: :months,
year: :years }.freeze

RESTRICT_ON_SEND = SINGULAR_DURATION_METHODS.keys + SINGULAR_DURATION_METHODS.values

PLURAL_DURATION_METHODS = SINGULAR_DURATION_METHODS.invert.freeze
SINGULAR_METHODS = { second: :seconds,
minute: :minutes,
hour: :hours,
day: :days,
week: :weeks,
fortnight: :fortnights,
month: :months,
year: :years,
byte: :bytes,
kilobyte: :kilobytes,
megabyte: :megabytes,
gigabyte: :gigabytes,
terabyte: :terabytes,
petabyte: :petabytes,
exabyte: :exabytes,
zettabyte: :zettabytes }
.freeze

RESTRICT_ON_SEND = SINGULAR_METHODS.keys + SINGULAR_METHODS.values

PLURAL_METHODS = SINGULAR_METHODS.invert.freeze

MSG = 'Prefer `%<number>s.%<correct>s`.'

Expand Down Expand Up @@ -86,15 +99,15 @@ def literal_number?(node)
end

def pluralize(method_name)
SINGULAR_DURATION_METHODS.fetch(method_name.to_sym).to_s
SINGULAR_METHODS.fetch(method_name.to_sym).to_s
end

def singularize(method_name)
PLURAL_DURATION_METHODS.fetch(method_name.to_sym).to_s
PLURAL_METHODS.fetch(method_name.to_sym).to_s
end

def duration_method?(method_name)
SINGULAR_DURATION_METHODS.key?(method_name) || PLURAL_DURATION_METHODS.key?(method_name)
SINGULAR_METHODS.key?(method_name) || PLURAL_METHODS.key?(method_name)
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rails/pluralization_grammar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,12 @@
it_behaves_like 'enforces pluralization grammar', 'fortnight'
it_behaves_like 'enforces pluralization grammar', 'month'
it_behaves_like 'enforces pluralization grammar', 'year'
it_behaves_like 'enforces pluralization grammar', 'byte'
it_behaves_like 'enforces pluralization grammar', 'kilobyte'
it_behaves_like 'enforces pluralization grammar', 'megabyte'
it_behaves_like 'enforces pluralization grammar', 'gigabyte'
it_behaves_like 'enforces pluralization grammar', 'terabyte'
it_behaves_like 'enforces pluralization grammar', 'petabyte'
it_behaves_like 'enforces pluralization grammar', 'exabyte'
it_behaves_like 'enforces pluralization grammar', 'zettabyte'
end

0 comments on commit bd8064d

Please sign in to comment.