From 7531811ad65b1949225b8cd7ec1c82e4e984f446 Mon Sep 17 00:00:00 2001 From: Tejas Bubane Date: Fri, 5 Nov 2021 00:28:58 +0530 Subject: [PATCH] [Fix #10203] Fix `Style/FormatStringToken` to respect `IgnoredMethods` with nested structures Closes #10203 --- ...ix_format_string_token_nested_ignored_methods.md | 1 + lib/rubocop/cop/style/format_string_token.rb | 3 ++- spec/rubocop/cop/style/format_string_token_spec.rb | 13 +++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_format_string_token_nested_ignored_methods.md diff --git a/changelog/fix_format_string_token_nested_ignored_methods.md b/changelog/fix_format_string_token_nested_ignored_methods.md new file mode 100644 index 00000000000..f0a041b0237 --- /dev/null +++ b/changelog/fix_format_string_token_nested_ignored_methods.md @@ -0,0 +1 @@ +* [#10203](https://github.com/rubocop/rubocop/issues/10203): Fix `Style/FormatStringToken` to respect `IgnoredMethods` with nested structures. ([@tejasbubane][]) diff --git a/lib/rubocop/cop/style/format_string_token.rb b/lib/rubocop/cop/style/format_string_token.rb index 1d1fce89179..5d2256ed644 100644 --- a/lib/rubocop/cop/style/format_string_token.rb +++ b/lib/rubocop/cop/style/format_string_token.rb @@ -102,7 +102,8 @@ def format_string_token?(node) end def use_ignored_method?(node) - (parent = node.parent) && parent.send_type? && ignored_method?(parent.method_name) + send_parent = node.each_ancestor(:send).first + send_parent && ignored_method?(send_parent.method_name) end def unannotated_format?(node, detected_style) diff --git a/spec/rubocop/cop/style/format_string_token_spec.rb b/spec/rubocop/cop/style/format_string_token_spec.rb index 99ce2c7ffa1..2e1fe1d036a 100644 --- a/spec/rubocop/cop/style/format_string_token_spec.rb +++ b/spec/rubocop/cop/style/format_string_token_spec.rb @@ -261,6 +261,19 @@ redirect("%{foo}") RUBY end + + it 'does not register an offense for value in nested structure' do + expect_no_offenses(<<~RUBY) + redirect("%{foo}", bye: "%{foo}") + RUBY + end + + it 'registers an offense for different method call within ignored method' do + expect_offense(<<~RUBY) + redirect("%{foo}", bye: foo("%{foo}")) + ^^^^^^ Prefer annotated tokens (like `%s`) over template tokens (like `%{foo}`). + RUBY + end end context 'when `IgnoredMethods: []`' do