Class: RuboCop::Cop::Style::SymbolProc
- Defined in:
- lib/rubocop/cop/style/symbol_proc.rb
Overview
Use symbols as procs when possible.
Constant Summary collapse
- MSG =
'Pass `&:%s` as an argument to `%s` instead of a block.'
Constants included from Util
Util::ASGN_NODES, Util::EQUALS_ASGN_NODES, Util::OPERATOR_METHODS, Util::PROC_NEW_NODE, Util::SHORTHAND_ASGN_NODES
Instance Attribute Summary
Attributes inherited from Cop
#config, #corrections, #offenses, #processed_source
Instance Method Summary collapse
- #autocorrect(node) ⇒ Object
- #ignored_method?(name) ⇒ Boolean
- #ignored_methods ⇒ Object
- #on_block(node) ⇒ Object
Methods inherited from Cop
#add_offense, all, #autocorrect?, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, cop_type, #debug?, #display_cop_names?, inherited, #initialize, #join_force?, lint?, #message, non_rails, qualified_cop_name, rails?, #relevant_file?, #support_autocorrect?
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
begins_its_line?, block_length, command?, comment_line?, const_name, first_part_of_call_chain, lambda?, lambda_or_proc?, line_range, numeric_range_size, on_node, operator?, parentheses?, proc?, range_with_surrounding_space, source_range, strip_quotes
Methods included from PathUtil
Constructor Details
This class inherits a constructor from RuboCop::Cop::Cop
Instance Method Details
#autocorrect(node) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 49 def autocorrect(node) @corrections << lambda do |corrector| block_method, _block_args, block_body = *node _receiver, method_name, _args = *block_body replacement = "#{block_method.loc.expression.source}" \ "(&:#{method_name})" corrector.replace(node.loc.expression, replacement) end end |
#ignored_method?(name) ⇒ Boolean
65 66 67 |
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 65 def ignored_method?(name) ignored_methods.include?(name.to_s) end |
#ignored_methods ⇒ Object
61 62 63 |
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 61 def ignored_methods cop_config['IgnoredMethods'] end |
#on_block(node) ⇒ Object
17 18 19 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 |
# File 'lib/rubocop/cop/style/symbol_proc.rb', line 17 def on_block(node) block_send, block_args, block_body = *node _breceiver, bmethod_name, = *block_send # we should ignore lambdas return if bmethod_name == :lambda return if ignored_method?(bmethod_name) # File.open(file) { |f| f.readlines } return if # something { |x, y| ... } return unless block_args.children.size == 1 return 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 if args return if receiver.type != :lvar block_arg_name, = *block_args.children.first receiver_name, = *receiver return if block_arg_name != receiver_name add_offense(node, :expression, format(MSG, method_name, bmethod_name)) end |