Skip to content

Commit

Permalink
Handle redundant parentheses around an interpolated expression for `S…
Browse files Browse the repository at this point in the history
…tyle/RedundantParentheses` cop
  • Loading branch information
fatkodima authored and marcandre committed Oct 18, 2020
1 parent 679eef1 commit 203d531
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/correctors/percent_literal_corrector.rb
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/redundant_parentheses.rb
Expand Up @@ -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?

Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/config_store_spec.rb
Expand Up @@ -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 }
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/redundant_parentheses_spec.rb
Expand Up @@ -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)]
Expand Down

0 comments on commit 203d531

Please sign in to comment.