From 5d9b254b3215ba6915afdb73d30568e968e7b931 Mon Sep 17 00:00:00 2001 From: Sunny Ripert Date: Sat, 22 Jul 2023 10:26:15 +0200 Subject: [PATCH] Add explicit option to `Rails/I18nLazyLookup` --- .../new_explicit_style_to_i18n_lazy_lookup.md | 1 + config/default.yml | 4 + lib/rubocop/cop/rails/i18n_lazy_lookup.rb | 76 +++++- .../cop/rails/i18n_lazy_lookup_spec.rb | 242 ++++++++++++------ 4 files changed, 227 insertions(+), 96 deletions(-) create mode 100644 changelog/new_explicit_style_to_i18n_lazy_lookup.md diff --git a/changelog/new_explicit_style_to_i18n_lazy_lookup.md b/changelog/new_explicit_style_to_i18n_lazy_lookup.md new file mode 100644 index 0000000000..660eff6e5e --- /dev/null +++ b/changelog/new_explicit_style_to_i18n_lazy_lookup.md @@ -0,0 +1 @@ +* [#1052](https://github.com/rubocop/rubocop-rails/pull/1052): Add explicit style to `Rails/I18nLazyLookup`. ([@sunny][]) diff --git a/config/default.yml b/config/default.yml index 0fef0c30d5..704ba72ca2 100644 --- a/config/default.yml +++ b/config/default.yml @@ -547,6 +547,10 @@ Rails/I18nLazyLookup: Reference: 'https://guides.rubyonrails.org/i18n.html#lazy-lookup' Enabled: pending VersionAdded: '2.14' + EnforcedStyle: lazy + SupportedStyles: + - lazy + - explicit Include: - 'app/controllers/**/*.rb' diff --git a/lib/rubocop/cop/rails/i18n_lazy_lookup.rb b/lib/rubocop/cop/rails/i18n_lazy_lookup.rb index 9053397255..87833036d4 100644 --- a/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +++ b/lib/rubocop/cop/rails/i18n_lazy_lookup.rb @@ -5,7 +5,13 @@ module Cop module Rails # Checks for places where I18n "lazy" lookup can be used. # - # @example + # This cop has two different enforcement modes. When the EnforcedStyle + # is `lazy` (the default), explicit lookups are added as offenses. + # + # When the EnforcedStyle is `explicit` then lazy lookups are added as + # offenses. + # + # @example EnforcedStyle: lazy (default) # # en.yml # # en: # # books: @@ -28,11 +34,29 @@ module Rails # end # end # + # @example EnforcedStyle: explicit + # # bad + # class BooksController < ApplicationController + # def create + # # ... + # redirect_to books_url, notice: t('.success') + # end + # end + # + # # good + # class BooksController < ApplicationController + # def create + # # ... + # redirect_to books_url, notice: t('books.create.success') + # end + # end + # class I18nLazyLookup < Base + include ConfigurableEnforcedStyle include VisibilityHelp extend AutoCorrector - MSG = 'Use "lazy" lookup for the text used in controllers.' + MSG = 'Use %