Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle redundant parentheses around an interpolated expression for Style/RedundantParentheses cop #8903

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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