Class: RuboCop::Cop::Style::SymbolProc

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

Overview

Use symbols as procs when possible.

Examples:

# bad
something.map { |s| s.upcase }

# good
something.map(&:upcase)

Constant Summary collapse

MSG =
'Pass `&:%s` as an argument to `%s` instead of a block.'.freeze
PROC_NODE =
s(:send, s(:const, nil, :Proc), :new)

Constants included from Util

Util::ASGN_NODES, Util::BYTE_ORDER_MARK, Util::EQUALS_ASGN_NODES, Util::LITERAL_REGEX, Util::OPERATOR_METHODS, Util::SHORTHAND_ASGN_NODES, Util::STRING_ESCAPES, Util::STRING_ESCAPE_REGEX

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offenses, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offense, all, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, cop_type, #correct, #debug?, #details, #display_cop_names?, #display_style_guide?, #excluded_file?, #extra_details?, inherited, #initialize, #join_force?, lint?, match?, #message, non_rails, #parse, qualified_cop_name, #reference_url, #relevant_file?, #style_guide_url, #target_ruby_version

Methods included from Sexp

#s

Methods included from NodePattern::Macros

#def_node_matcher, #def_node_search, #node_search_body

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #support_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

begins_its_line?, block_length, comment_line?, directions, double_quotes_acceptable?, double_quotes_required?, effective_column, ends_its_line?, first_part_of_call_chain, interpret_string_escapes, line_range, move_pos, numeric_range_size, on_node, operator?, parentheses?, parenthesized_call?, range_with_surrounding_comma, range_with_surrounding_space, source_range, strip_quotes, ternary_op?, to_string_literal, to_symbol_literal, within_node?

Methods included from PathUtil

absolute?, hidden?, issue_deprecation_warning, match_path?, relative_path

Constructor Details

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

Instance Method Details

#autocorrect(node) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 51

def autocorrect(node)
  lambda do |corrector|
    block_send_or_super, _block_args, block_body = *node
    _receiver, method_name, _args = *block_body

    if super?(block_send_or_super)
      args = *block_send_or_super
    else
      _breceiver, _bmethod_name, *args = *block_send_or_super
    end
    autocorrect_method(corrector, node, args, method_name)
  end
end

#autocorrect_method(corrector, node, args, method_name) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 65

def autocorrect_method(corrector, node, args, method_name)
  if args.empty?
    autocorrect_no_args(corrector, node, method_name)
  else
    autocorrect_with_args(corrector, node, args, method_name)
  end
end

#autocorrect_no_args(corrector, node, method_name) ⇒ Object



73
74
75
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 73

def autocorrect_no_args(corrector, node, method_name)
  corrector.replace(block_range_with_space(node), "(&:#{method_name})")
end

#autocorrect_with_args(corrector, node, args, method_name) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 77

def autocorrect_with_args(corrector, node, args, method_name)
  arg_range = args.last.source_range
  arg_range = range_with_surrounding_comma(arg_range, :right)
  replacement = " &:#{method_name}"
  replacement = ',' + replacement unless arg_range.source.end_with?(',')
  corrector.insert_after(arg_range, replacement)
  corrector.remove(block_range_with_space(node))
end

#begin_pos_for_replacement(node) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 94

def begin_pos_for_replacement(node)
  block_send_or_super, _block_args, _block_body = *node
  expr = block_send_or_super.source_range

  if (paren_pos = (expr.source =~ /\(\s*\)$/))
    expr.begin_pos + paren_pos
  else
    node.loc.begin.begin_pos
  end
end

#block_range_with_space(node) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 86

def block_range_with_space(node)
  block_range =
    Parser::Source::Range.new(node.source_range.source_buffer,
                              begin_pos_for_replacement(node),
                              node.loc.end.end_pos)
  range_with_surrounding_space(block_range, :left)
end

#can_shorten?(block_args, block_body) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 113

def can_shorten?(block_args, block_body)
  # something { |x, y| ... }
  return false unless block_args.children.size == 1
  return false if block_args.children.first.blockarg_type?
  return false unless block_body && block_body.type == :send

  receiver, _method_name, args = *block_body

  # method in block must be invoked on a lvar without args
  return false if args
  return false unless receiver && receiver.type == :lvar

  block_arg_name, = *block_args.children.first
  receiver_name, = *receiver

  block_arg_name == receiver_name
end

#ignored_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 109

def ignored_method?(name)
  ignored_methods.include?(name.to_s)
end

#ignored_methodsObject



105
106
107
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 105

def ignored_methods
  cop_config['IgnoredMethods']
end

#on_block(node) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 20

def on_block(node)
  block_send_or_super, block_args, block_body = *node

  if super?(block_send_or_super)
    bmethod_name = :super
  else
    _breceiver, bmethod_name, _bargs = *block_send_or_super
  end

  # TODO: Rails-specific handling that we should probably make
  # configurable - https://github.com/bbatsov/rubocop/issues/1485
  # we should ignore lambdas & procs
  return if block_send_or_super == PROC_NODE
  return if [:lambda, :proc].include?(bmethod_name)
  return if ignored_method?(bmethod_name)
  return unless can_shorten?(block_args, block_body)

  _receiver, method_name, _args = *block_body

  sb = node.source_range.source_buffer
  block_start = node.loc.begin.begin_pos
  block_end = node.loc.end.end_pos
  range = Parser::Source::Range.new(sb, block_start, block_end)

  add_offense(node,
              range,
              format(MSG,
                     method_name,
                     bmethod_name))
end

#super?(node) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 131

def super?(node)
  [:super, :zsuper].include?(node.type)
end