Sha256: f9d634c89cd6047564333158a4de89047c2fc573ff131d12e1213db1126126f8
Contents?: true
Size: 1.92 KB
Versions: 1
Compression:
Stored size: 1.92 KB
Contents
# encoding: utf-8 module Rubocop module Cop module Style # This cop checks for redundant uses of `self`. It is only needed when # calling a write accessor on self. # # Special cases: # # We allow uses of `self` with operators because it would be awkward # otherwise. class RedundantSelf < Cop MSG = 'Redundant `self` detected.' def initialize super @allowed_send_nodes = [] @local_variables = [] end # Assignment of self.x def on_or_asgn(node) lhs, _rhs = *node allow_self(lhs) end alias_method :on_and_asgn, :on_or_asgn def on_op_asgn(node) lhs, _op, _rhs = *node allow_self(lhs) end # Using self.x to distinguish from local variable x def on_def(node) @local_variables = [] end def on_defs(node) @local_variables = [] end def on_lvasgn(node) lhs, _rhs = *node @local_variables << lhs end # Detect offences def on_send(node) receiver, method_name, *_args = *node if receiver && receiver.type == :self unless operator?(method_name) || keyword?(method_name) || @allowed_send_nodes.include?(node) || @local_variables.include?(method_name) add_offence(:convention, receiver.loc.expression, MSG) end end end private def operator?(method_name) method_name.to_s =~ /\W/ end def keyword?(method_name) [:class, :for].include?(method_name) end def allow_self(node) if node.type == :send receiver, _method_name, *_args = *node @allowed_send_nodes << node if receiver && receiver.type == :self end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rubocop-0.10.0 | lib/rubocop/cop/style/redundant_self.rb |