Skip to content

Commit

Permalink
[Fix rubocop#4420] Ensure Style/EmptyMethod honours indentation whe…
Browse files Browse the repository at this point in the history
…n auto-correcting

This cop would ignore indentation when auto-correcting compact style
into expanded style, resulting in indentation problems like:

```
class Foo
  def bar
end
end
```

This change fixes that.
  • Loading branch information
Drenmi committed May 26, 2017
1 parent cbb5573 commit 6a2fcec
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [#4414](https://github.com/bbatsov/rubocop/issues/4414): Handle pseudo-assignments in `for` loops in `Style/ConditionalAssignment`. ([@bbatsov][])
* [#4419](https://github.com/bbatsov/rubocop/issues/4419): Handle combination `AllCops: DisabledByDefault: true` and `Rails: Enabled: true`. ([@jonas054][])
* [#4422](https://github.com/bbatsov/rubocop/issues/4422): Fix missing space in message for `Style/MultipleComparison`. ([@timrogers][])
* [#4420](https://github.com/bbatsov/rubocop/issues/4420): Ensure `Style/EmptyMethod` honours indentation when auto-correcting. ([@drenmi][])

## 0.49.0 (2017-05-24)

Expand Down Expand Up @@ -2801,4 +2802,4 @@
[@gprado]: https://github.com/gprado
[@yhirano55]: https://github.com/yhirano55
[@hoshinotsuyoshi]: https://github.com/hoshinotsuyoshi
[@timrogers]: https://github.com/timrogers
[@timrogers]: https://github.com/timrogers
7 changes: 4 additions & 3 deletions lib/rubocop/cop/style/empty_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ def message
def corrected(node)
method_name, args, _body, scope = method_def_node_parts(node)

arguments = args.source unless args.children.empty?
joint = compact_style? ? '; ' : "\n"
scope = scope ? 'self.' : ''
arguments = !args.children.empty? ? args.source : ''
indent = ' ' * node.loc.column
joint = compact_style? ? '; ' : "\n#{indent}"
scope = scope ? 'self.' : ''

["def #{scope}#{method_name}#{arguments}", 'end'].join(joint)
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/empty_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,16 @@
' # bar',
'end']
end

context 'when method is nested in class scope' do
it_behaves_like 'code with offense',
['class Foo',
' def bar; end',
'end'].join("\n"),
['class Foo',
' def bar',
' end',
'end'].join("\n")
end
end
end

0 comments on commit 6a2fcec

Please sign in to comment.