Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Rails/PluralizationGrammar aware of byte methods #1306

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_make_pluralization_grammar_aware_of_bytes.md
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][])
44 changes: 29 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,39 @@ 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 +100,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