Skip to content

Commit

Permalink
[Fix rubocop#3383] Handle properly nested methods in RedundantSelf (r…
Browse files Browse the repository at this point in the history
…ubocop#3459)

That commit adds failing test for issue rubocop#3383, and
fixes the problem by adding scoped local vars
management.
  • Loading branch information
bankair authored and Neodelf committed Oct 15, 2016
1 parent 944b901 commit 66a605c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Bug fixes

* [#3383](https://github.com/bbatsov/rubocop/issues/3383): Fix the local variable reset issue with `Style/RedundantSelf` cop. ([@bankair][])
* [#3445](https://github.com/bbatsov/rubocop/issues/3445): Fix bad autocorrect for `Style/AndOr` cop. ([@mikezter][])
* [#3349](https://github.com/bbatsov/rubocop/issues/3349): Fix bad autocorrect for `Style/Lambda` cop. ([@metcalf][])
* [#3351](https://github.com/bbatsov/rubocop/issues/3351): Fix bad auto-correct for `Performance/RedundantMatch` cop. ([@annaswims][])
Expand Down
24 changes: 15 additions & 9 deletions lib/rubocop/cop/style/redundant_self.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class RedundantSelf < Cop
def initialize(config = nil, options = nil)
super
@allowed_send_nodes = []
@local_variables = []
@local_variables_scopes = Hash.new { |hash, key| hash[key] = [] }
end

# Assignment of self.x
Expand All @@ -68,12 +68,12 @@ def on_op_asgn(node)

# Using self.x to distinguish from local variable x

def on_def(_node)
@local_variables = []
def on_def(node)
add_scope(node)
end

def on_defs(_node)
@local_variables = []
def on_defs(node)
add_scope(node)
end

def on_args(node)
Expand All @@ -86,7 +86,7 @@ def on_blockarg(node)

def on_lvasgn(node)
lhs, _rhs = *node
@local_variables << lhs
@local_variables_scopes[node] << lhs
end

# Detect offenses
Expand All @@ -97,8 +97,7 @@ def on_send(node)
return unless regular_method_call?(node)

return if @allowed_send_nodes.include?(node) ||
@local_variables.include?(method_name)

@local_variables_scopes[node].include?(method_name)
add_offense(node, :expression)
end

Expand All @@ -112,6 +111,13 @@ def autocorrect(node)

private

def add_scope(node)
local_variables = []
node.descendants.each do |child_node|
@local_variables_scopes[child_node] = local_variables
end
end

def regular_method_call?(node)
_receiver, method_name, *_args = *node

Expand All @@ -123,7 +129,7 @@ def regular_method_call?(node)

def on_argument(node)
name, = *node
@local_variables << name
@local_variables_scopes[node] << name
end

def keyword?(method_name)
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/style/redundant_self_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@
inspect_source(cop, src)
expect(cop.offenses).to be_empty
end

it 'accepts a self receiver used to distinguish from an argument' do
src = ['def foo(bar)',
' puts bar, self.bar',
'end']
inspect_source(cop, src)
expect(cop.offenses).to be_empty
end

it 'accepts a self receiver used to distinguish from an argument' do
src = ['def foo(bar)',
' def inner_method(); end',
' puts bar, self.bar',
'end']
inspect_source(cop, src)
expect(cop.offenses).to be_empty
end
end

describe 'class methods' do
Expand Down

0 comments on commit 66a605c

Please sign in to comment.