Skip to content

Commit

Permalink
Merge branch 'rubocop:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
thearjunmdas committed Jul 14, 2021
2 parents 81c5c66 + 3e1cc1a commit aed2f41
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
@@ -0,0 +1 @@
* [#9926](https://github.com/rubocop/rubocop/pull/9926): Fix an incorrect auto-correct for `Style/SingleLineMethods` when method body is enclosed in parentheses. ([@koic][])
14 changes: 6 additions & 8 deletions lib/rubocop/cop/style/mutable_constant.rb
Expand Up @@ -62,13 +62,12 @@ class MutableConstant < Base

def on_casgn(node)
_scope, _const_name, value = *node
on_assignment(value)
end
if value.nil? # This is only the case for `CONST += ...` or similarg66
parent = node.parent
return unless parent.or_asgn_type? # We only care about `CONST ||= ...`

def on_or_asgn(node)
lhs, value = *node

return unless lhs&.casgn_type?
value = parent.children.last
end

on_assignment(value)
end
Expand Down Expand Up @@ -119,14 +118,13 @@ def autocorrect(corrector, node)
end

def mutable_literal?(value)
return false if value.nil?
return false if frozen_regexp_or_range_literals?(value)

value.mutable_literal?
end

def immutable_literal?(node)
node.nil? || frozen_regexp_or_range_literals?(node) || node.immutable_literal?
frozen_regexp_or_range_literals?(node) || node.immutable_literal?
end

def frozen_string_literal?(node)
Expand Down
23 changes: 14 additions & 9 deletions lib/rubocop/cop/style/single_line_methods.rb
Expand Up @@ -72,17 +72,15 @@ def correct_to_endless?(body_node)
end

def correct_to_multiline(corrector, node)
each_part(node.body) do |part|
LineBreakCorrector.break_line_before(
range: part, node: node, corrector: corrector,
configured_width: configured_indentation_width
)
if (body = node.body) && body.begin_type? && body.parenthesized_call?
break_line_before(corrector, node, body)
else
each_part(body) do |part|
break_line_before(corrector, node, part)
end
end

LineBreakCorrector.break_line_before(
range: node.loc.end, node: node, corrector: corrector,
indent_steps: 0, configured_width: configured_indentation_width
)
break_line_before(corrector, node, node.loc.end, indent_steps: 0)

move_comment(node, corrector)
end
Expand All @@ -96,6 +94,13 @@ def correct_to_endless(corrector, node)
corrector.replace(node, replacement)
end

def break_line_before(corrector, node, range, indent_steps: 1)
LineBreakCorrector.break_line_before(
range: range, node: node, corrector: corrector,
configured_width: configured_indentation_width, indent_steps: indent_steps
)
end

def each_part(body)
return unless body

Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/style/single_line_methods_spec.rb
Expand Up @@ -32,6 +32,19 @@ def @table.columns;#{trailing_whitespace}
RUBY
end

it 'registers an offense for a single-line method and method body is enclosed in parentheses' do
expect_offense(<<~RUBY)
def foo() (do_something) end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid single-line method definitions.
RUBY

expect_correction(<<~RUBY)
def foo()#{trailing_whitespace}
(do_something)#{trailing_whitespace}
end
RUBY
end

context 'when AllowIfMethodIsEmpty is disabled' do
let(:cop_config) { { 'AllowIfMethodIsEmpty' => false } }

Expand Down

0 comments on commit aed2f41

Please sign in to comment.