From 9c841c6c290c712a12e9237a36760e1b757ab3cb Mon Sep 17 00:00:00 2001 From: fatkodima Date: Sun, 18 Oct 2020 01:52:19 +0300 Subject: [PATCH] Handle redundant parentheses around an interpolated expression for `Style/RedundantParentheses` cop --- CHANGELOG.md | 1 + .../cop/correctors/percent_literal_corrector.rb | 2 +- lib/rubocop/cop/style/redundant_parentheses.rb | 4 ++++ spec/rubocop/config_store_spec.rb | 2 +- spec/rubocop/cop/style/redundant_parentheses_spec.rb | 11 +++++++++++ 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fef55e2c5d..e003bf2cd3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### New features * [#7944](https://github.com/rubocop-hq/rubocop/issues/7944): Add `MaxUnannotatedPlaceholdersAllowed` option to `Style/FormatStringToken` cop. ([@Tietew][]) +* [#8379](https://github.com/rubocop-hq/rubocop/issues/8379): Handle redundant parentheses around an interpolated expression for `Style/RedundantParentheses` cop. ([@fatkodima][]) ### Bug fixes diff --git a/lib/rubocop/cop/correctors/percent_literal_corrector.rb b/lib/rubocop/cop/correctors/percent_literal_corrector.rb index 433977454fb..4d8c46f032b 100644 --- a/lib/rubocop/cop/correctors/percent_literal_corrector.rb +++ b/lib/rubocop/cop/correctors/percent_literal_corrector.rb @@ -89,7 +89,7 @@ def process_lines(node, previous_line_num, base_line_num, source_in_lines) begin_line_num = previous_line_num - base_line_num + 1 end_line_num = node.first_line - base_line_num + 1 lines = source_in_lines[begin_line_num...end_line_num] - "\n#{(lines.join("\n").split(node.source).first || '')}" + "\n#{lines.join("\n").split(node.source).first || ''}" end def fix_escaped_content(word_node, escape, delimiters) diff --git a/lib/rubocop/cop/style/redundant_parentheses.rb b/lib/rubocop/cop/style/redundant_parentheses.rb index 5d141379954..adfef23683a 100644 --- a/lib/rubocop/cop/style/redundant_parentheses.rb +++ b/lib/rubocop/cop/style/redundant_parentheses.rb @@ -104,9 +104,13 @@ def check(begin_node) return offense(begin_node, 'a variable') if node.variable? return offense(begin_node, 'a constant') if node.const_type? + return offense(begin_node, 'an interpolated expression') if interpolation?(begin_node) + check_send(begin_node, node) if node.call_type? end + def_node_matcher :interpolation?, '[^begin ^^dstr]' + def check_send(begin_node, node) return check_unary(begin_node, node) if node.unary_operation? diff --git a/spec/rubocop/config_store_spec.rb b/spec/rubocop/config_store_spec.rb index f6ef4a9203f..f2045ea8c2d 100644 --- a/spec/rubocop/config_store_spec.rb +++ b/spec/rubocop/config_store_spec.rb @@ -10,7 +10,7 @@ # dir/.rubocop.yml # dir/file2 # dir/subdir/file3 - "#{(/dir/.match?(arg) ? 'dir' : '.')}/.rubocop.yml" + "#{/dir/.match?(arg) ? 'dir' : '.'}/.rubocop.yml" end allow(RuboCop::ConfigLoader) .to receive(:configuration_from_file) { |arg| arg } diff --git a/spec/rubocop/cop/style/redundant_parentheses_spec.rb b/spec/rubocop/cop/style/redundant_parentheses_spec.rb index 0eeb6cbce6b..dc80ed08995 100644 --- a/spec/rubocop/cop/style/redundant_parentheses_spec.rb +++ b/spec/rubocop/cop/style/redundant_parentheses_spec.rb @@ -128,6 +128,17 @@ it_behaves_like 'plausible', '+(1.foo.bar)' it_behaves_like 'plausible', '()' + it 'registers an offense for parens around an interpolated expression' do + expect_offense(<<~RUBY) + "\#{(foo)}" + ^^^^^ Don't use parentheses around an interpolated expression. + RUBY + + expect_correction(<<~RUBY) + "\#{foo}" + RUBY + end + it 'registers an offense for parens around a literal in array' do expect_offense(<<~RUBY) [(1)]