Class: Ducalis::UselessOnly

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

Constant Summary collapse

OFFENSE =
<<-MESSAGE.gsub(/^ +\|\s/, '').strip
  | Seems like there is no any reason to keep before filter only for one action. Maybe it will be better to inline it?
MESSAGE
DETAILS =
<<-MESSAGE.gsub(/^ +\|\s/, '').strip
  | Compare:

  | ```ruby
  | before_filter :do_something, only: %i[index]
  | def index; end

  | # to

  | def index
  |   do_something
  | end

  | ```

MESSAGE
FILTERS =
%i[before_filter after_filter around_filter
before_action after_action around_action].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ducalis/cops/useless_only.rb', line 31

def on_send(node)
  _, method_name, *args = *node
  hash_node = args.find { |subnode| subnode.type == :hash }
  return unless FILTERS.include?(method_name) && hash_node

  type, method_names = decomposite_hash(hash_node)
  return unless type == s(:sym, :only)
  return unless method_names.children.count == 1

  add_offense(node, :selector, OFFENSE)
end