Skip to content

Commit

Permalink
[Fix #956] Apply ClassMethods check only on class/module bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
Bozhidar Batsov committed Apr 4, 2014
1 parent 52d6171 commit f5ae10e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 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 @@
* [#954](https://github.com/bbatsov/rubocop/pull/954): Fix auto-correction bug in `NilComparison`. ([@bbatsov][])
* [#953](https://github.com/bbatsov/rubocop/pull/953): Fix auto-correction bug in `NonNilCheck`. ([@bbatsov][])
* [#952](https://github.com/bbatsov/rubocop/pull/952): Handle implicit receiver in `StringConversionInInterpolation`. ([@bbatsov][])
* [#956](https://github.com/bbatsov/rubocop/pull/956): Apply `ClassMethods` check only on `class`/`module` bodies. ([@bbatsov][])

## 0.20.0 (02/04/2014)

Expand Down
28 changes: 24 additions & 4 deletions lib/rubocop/cop/style/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,30 @@ module Style
class ClassMethods < Cop
MSG = 'Use `self.%s` instead of `%s.%s`.'

# TODO: Check if we're in a class/module
def on_defs(node)
def on_class(node)
_name, _superclass, body = *node
check(body)
end

def on_module(node)
_name, body = *node
check(body)
end

private

def check(node)
return unless node

if node.type == :defs
check_defs(node)
elsif node.type == :begin
defs_nodes = node.children.compact.select { |n| n.type == :defs }
defs_nodes.each { |n| check_defs(n) }
end
end

def check_defs(node)
definee, method_name, _args, _body = *node

if definee.type == :const
Expand All @@ -19,8 +41,6 @@ def on_defs(node)
end
end

private

def message(class_name, method_name)
format(MSG, method_name, class_name, method_name)
end
Expand Down

0 comments on commit f5ae10e

Please sign in to comment.