diff --git a/changelog/change_make_pluralization_grammar_aware_of_bytes.md b/changelog/change_make_pluralization_grammar_aware_of_bytes.md new file mode 100644 index 0000000000..1c34bf1259 --- /dev/null +++ b/changelog/change_make_pluralization_grammar_aware_of_bytes.md @@ -0,0 +1 @@ +* [#1306](https://github.com/rubocop/rubocop-rails/pull/1306): Make `Rails/PluralizationGrammar` aware of byte methods. ([@earlopain][]) diff --git a/config/default.yml b/config/default.yml index 9e0c976ba6..40c20b8edf 100644 --- a/config/default.yml +++ b/config/default.yml @@ -765,6 +765,7 @@ Rails/PluralizationGrammar: Description: 'Checks for incorrect grammar when using methods like `3.day.ago`.' Enabled: true VersionAdded: '0.35' + VersionChanged: '<>' Rails/Presence: Description: 'Checks code that can be written more easily using `Object#presence` defined by Active Support.' diff --git a/lib/rubocop/cop/rails/pluralization_grammar.rb b/lib/rubocop/cop/rails/pluralization_grammar.rb index ef62d15c8c..d5bcdb3191 100644 --- a/lib/rubocop/cop/rails/pluralization_grammar.rb +++ b/lib/rubocop/cop/rails/pluralization_grammar.rb @@ -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 `%s.%s`.' @@ -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 diff --git a/spec/rubocop/cop/rails/pluralization_grammar_spec.rb b/spec/rubocop/cop/rails/pluralization_grammar_spec.rb index 65a9565336..2d4f6f8298 100644 --- a/spec/rubocop/cop/rails/pluralization_grammar_spec.rb +++ b/spec/rubocop/cop/rails/pluralization_grammar_spec.rb @@ -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