From 45656f84068631cfe95bc2de7bd2666bcbffdeb0 Mon Sep 17 00:00:00 2001 From: safa Date: Tue, 17 Oct 2023 12:21:28 +0100 Subject: [PATCH] Include ignoredNode module to avoid clobbering for SpreeTDeprecated cop --- lib/rubocop/cop/solidus/spree_t_deprecated.rb | 4 +++ .../cop/solidus/spree_t_deprecated_spec.rb | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/rubocop/cop/solidus/spree_t_deprecated.rb b/lib/rubocop/cop/solidus/spree_t_deprecated.rb index 9f6ba11..e667af1 100644 --- a/lib/rubocop/cop/solidus/spree_t_deprecated.rb +++ b/lib/rubocop/cop/solidus/spree_t_deprecated.rb @@ -44,6 +44,7 @@ module Solidus # class SpreeTDeprecated < Base extend AutoCorrector + include IgnoredNode MSG = 'Use I18n.t instead of Spree.t which has been deprecated in future versions.' RESTRICT_ON_SEND = %i[t].freeze @@ -58,8 +59,11 @@ def on_send(node) return unless spree_t?(node).include?(:Spree) add_offense(node) do |corrector| + next if part_of_ignored_node?(node) + corrector.replace(node, corrected_statement(node)) end + ignore_node(node) end # rubocop:disable Metrics/MethodLength diff --git a/spec/rubocop/cop/solidus/spree_t_deprecated_spec.rb b/spec/rubocop/cop/solidus/spree_t_deprecated_spec.rb index 7ed538a..84c0cd0 100644 --- a/spec/rubocop/cop/solidus/spree_t_deprecated_spec.rb +++ b/spec/rubocop/cop/solidus/spree_t_deprecated_spec.rb @@ -91,6 +91,20 @@ I18n.t(:solidus_store, scope: [:custom_scope], email: params[:email]) RUBY end + + context 'when nested correction' do + it 'registers an offense when using `#bad_method`' do + expect_offense(<<~RUBY) + Spree.t(:solidus_store, resource: Spree.t(:test)) + ^^^^^^^^^^^^^^ Use I18n.t instead of Spree.t which has been deprecated in future versions. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use I18n.t instead of Spree.t which has been deprecated in future versions. + RUBY + + expect_correction(<<~RUBY) + I18n.t(:solidus_store, scope: :spree, resource: I18n.t(:test, scope: :spree)) + RUBY + end + end end describe 'first argument is a string' do @@ -183,5 +197,19 @@ I18n.t('solidus_store', scope: [:custom_scope], email: params[:email]) RUBY end + + context 'when nested correction' do + it 'registers an offense when using `#bad_method`' do + expect_offense(<<~RUBY) + Spree.t(:solidus_store, resource: Spree.t(:test)) + ^^^^^^^^^^^^^^ Use I18n.t instead of Spree.t which has been deprecated in future versions. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use I18n.t instead of Spree.t which has been deprecated in future versions. + RUBY + + expect_correction(<<~RUBY) + I18n.t(:solidus_store, scope: :spree, resource: I18n.t(:test, scope: :spree)) + RUBY + end + end end end