Skip to content

Commit

Permalink
[Fix rubocop#3358] Make Style/MethodMissing cop aware of class scope
Browse files Browse the repository at this point in the history
This cop would check for an instance method named `#respond_to_missing?`
even if `#method_missing` was implemented as a class method.

This fixes that by ensuring that the cop looks in the same scope.
  • Loading branch information
Drenmi committed Aug 5, 2016
1 parent e8a3ebc commit 0099bdd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [#3209](https://github.com/bbatsov/rubocop/issues/3209): Remove faulty line length check from `Style/GuardClause` cop. ([@drenmi][])
* [#3366](https://github.com/bbatsov/rubocop/issues/3366): Make `Style/MutableConstant` cop aware of splat assignments. ([@drenmi][])
* [#3372](https://github.com/bbatsov/rubocop/pull/3372): Fix RuboCop crash with empty brackets in `Style/Next` cop. ([@pocke][])
* [#3358](https://github.com/bbatsov/rubocop/issues/3358): Make `Style/MethodMissing` cop aware of class scope. ([@drenmi][])

### Changes

Expand Down
12 changes: 10 additions & 2 deletions lib/rubocop/cop/style/method_missing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,22 @@ def calls_super?(node)
end

def implements_respond_to_missing?(node)
node.parent.each_child_node(:def).any? do |sibling|
respond_to_missing_def?(sibling)
node.parent.each_child_node(node.type).any? do |sibling|
if node.def_type?
respond_to_missing_def?(sibling)
elsif node.defs_type?
respond_to_missing_defs?(sibling)
end
end
end

def_node_matcher :respond_to_missing_def?, <<-PATTERN
(def :respond_to_missing? (...) ...)
PATTERN

def_node_matcher :respond_to_missing_defs?, <<-PATTERN
(defs (self) :respond_to_missing? (...) ...)
PATTERN
end
end
end
Expand Down
40 changes: 33 additions & 7 deletions spec/rubocop/cop/style/method_missing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,38 @@
end

describe 'when implementing #respond_to_missing? and calling #super' do
it_behaves_like 'code without offense',
['class Test',
' def respond_to_missing?; end',
' def method_missing',
' super',
' end',
'end'].join("\n")
context 'when implemented as instance methods' do
it_behaves_like 'code without offense',
['class Test',
' def respond_to_missing?; end',
' def method_missing',
' super',
' end',
'end'].join("\n")
end

context 'when implemented as class methods' do
it_behaves_like 'code without offense',
['class Test',
' def self.respond_to_missing?; end',
' def self.method_missing',
' super',
' end',
'end'].join("\n")
end

context 'when implemented with different scopes' do
let(:message) do
'When using `method_missing`, define `respond_to_missing?`.'
end

it_behaves_like 'code with offense',
['class Test',
' def respond_to_missing?; end',
' def self.method_missing',
' super',
' end',
'end'].join("\n")
end
end
end

0 comments on commit 0099bdd

Please sign in to comment.