Class: Ducalis::PossibleTap

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Defined in:
lib/ducalis/cops/possible_tap.rb

Constant Summary collapse

OFFENSE =
<<-MESSAGE.gsub(/^ +\|\s/, '').strip
  | Consider of using `.tap`, default ruby [method](<https://apidock.com/ruby/Object/tap>) which allows to replace intermediate variables with block, by this you are limiting scope pollution and make method scope more clear.
  | If it isn't possible, consider of moving it to method or even inline it.
  |
MESSAGE
DETAILS =
<<-MESSAGE.gsub(/^ +\|\s/, '').strip
  | [Related article](<http://seejohncode.com/2012/01/02/ruby-tap-that/>).
MESSAGE
PAIRS =
{
  lvar: :lvasgn,
  ivar: :ivasgn
}.freeze
ASSIGNS =
PAIRS.keys

Instance Method Summary collapse

Instance Method Details

#on_def(node) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/ducalis/cops/possible_tap.rb', line 24

def on_def(node)
  _name, _args, body = *node
  return if body.nil?
  return unless (possibe_var = return_var?(body) || return_var_call?(body))
  return unless (assign_node = find_assign(body, possibe_var))

  add_offense(assign_node, :expression, OFFENSE)
end