Sha256: b27f8316e5b92c9ba5b46d868cb7b87b81fc9e80a1e4e9d20399eb4105bd1ec4

Contents?: true

Size: 1.27 KB

Versions: 13

Compression:

Stored size: 1.27 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop makes sure that all methods use the configured style,
      # snake_case or camelCase, for their names. Some special arrangements
      # have to be made for operator methods.
      class MethodName < Cop
        include ConfigurableNaming

        def on_def(node)
          check(node, name_of_instance_method(node))
        end

        def on_defs(node)
          check(node, name_of_singleton_method(node))
        end

        def name_of_instance_method(def_node)
          expr = def_node.loc.expression
          match = /^def(\s+)([\w]+[!?=]?\b)/.match(expr.source)
          return unless match
          space, method_name = match.captures
          begin_pos = expr.begin_pos + 'def'.length + space.length
          Parser::Source::Range.new(expr.source_buffer, begin_pos,
                                    begin_pos + method_name.length)
        end

        def name_of_singleton_method(defs_node)
          scope, method_name, _args, _body = *defs_node
          after_dot(defs_node, method_name.length,
                    "def\s+" + Regexp.escape(scope.loc.expression.source))
        end

        def message(style)
          format('Use %s for methods.', style)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rubocop-0.22.0 lib/rubocop/cop/style/method_name.rb
rubocop-0.21.0 lib/rubocop/cop/style/method_name.rb
rubocop-0.20.1 lib/rubocop/cop/style/method_name.rb
rubocop-0.20.0 lib/rubocop/cop/style/method_name.rb
rubocop-0.19.1 lib/rubocop/cop/style/method_name.rb
rubocop-0.19.0 lib/rubocop/cop/style/method_name.rb
rubocop-0.18.1 lib/rubocop/cop/style/method_name.rb
rubocop-0.18.0 lib/rubocop/cop/style/method_name.rb
rubocop-0.17.0 lib/rubocop/cop/style/method_name.rb
rubocop-0.16.0 lib/rubocop/cop/style/method_name.rb
rubocop-0.15.0 lib/rubocop/cop/style/method_name.rb
rubocop-0.14.1 lib/rubocop/cop/style/method_name.rb
rubocop-0.14.0 lib/rubocop/cop/style/method_name.rb