Class: Rubocop::Cop::Style::FavorJoin

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/favor_join.rb

Overview

This cop checks for uses of "" as a substitute for *join.

Not all cases can reliably checked, due to Ruby's dynamic types, so we consider only cases when the first argument is an array literal or the second is a string literal.

Constant Summary collapse

MSG =
'Favor Array#join over Array#*.'

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #corrections, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#on_send(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/rubocop/cop/style/favor_join.rb', line 14

def on_send(node)
  receiver_node, method_name, *arg_nodes = *node

  if receiver_node && receiver_node.type == :array &&
      method_name == :* && arg_nodes[0].type == :str
    add_offence(:convention,
                node.loc.selector,
                MSG)
  end
end