123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::LambdaLiteralToMethodCorrector

Relationships & Source Files
Inherits: Object
Defined in: lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb

Overview

This class autocorrects lambda literal to method notation.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(block_node) ⇒ LambdaLiteralToMethodCorrector

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 7

def initialize(block_node)
  @block_node = block_node
  @method     = block_node.send_node
  @arguments  = block_node.arguments
end

Instance Attribute Details

#arg_to_unparenthesized_call?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 118

def arg_to_unparenthesized_call?
  current_node = block_node

  parent = current_node.parent

  if parent&.pair_type?
    current_node = parent.parent
    parent = current_node.parent
  end

  return false unless parent&.send_type?
  return false if parent.parenthesized_call?

  current_node.sibling_index > 1
end

#arguments (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 34

attr_reader :block_node, :method, :arguments

#block_node (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 34

attr_reader :block_node, :method, :arguments

#method (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 34

attr_reader :block_node, :method, :arguments

#needs_separating_space?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 92

def needs_separating_space?
  (block_begin.begin_pos == arguments_end_pos &&
    selector_end.end_pos == arguments_begin_pos) ||
    block_begin.begin_pos == selector_end.end_pos
end

#separating_space?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 134

def separating_space?
  block_begin.source_buffer.source[block_begin.begin_pos + 2].match?(/\s/)
end

Instance Method Details

#arguments_begin_pos (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 102

def arguments_begin_pos
  arguments.loc.begin&.begin_pos
end

#arguments_end_pos (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 98

def arguments_end_pos
  arguments.loc.end&.end_pos
end

#block_begin (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 110

def block_begin
  block_node.loc.begin
end

#block_end (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 106

def block_end
  block_node.loc.end
end

#call(corrector)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 13

def call(corrector)
  # Check for unparenthesized args' preceding and trailing whitespaces.
  remove_unparenthesized_whitespace(corrector)

  if block_node.block_type?
    # Avoid correcting to `lambdado` by inserting whitespace
    # if none exists before or after the lambda arguments.
    insert_separating_space(corrector)

    remove_arguments(corrector)
  end

  replace_selector(corrector)

  replace_delimiters(corrector)

  insert_arguments(corrector)
end

#insert_arguments(corrector) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 59

def insert_arguments(corrector)
  return if arguments.empty?

  arg_str = " |#{lambda_arg_string}|"
  corrector.insert_after(block_node.loc.begin, arg_str)
end

#insert_separating_space(corrector) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 43

def insert_separating_space(corrector)
  return unless needs_separating_space?

  corrector.insert_before(block_begin, ' ')
end

#lambda_arg_string (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 88

def lambda_arg_string
  arguments.children.map(&:source).join(', ')
end

#remove_arguments(corrector) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 53

def remove_arguments(corrector)
  return if arguments.empty_and_without_delimiters?

  corrector.remove(arguments)
end

#remove_leading_whitespace(corrector) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 66

def remove_leading_whitespace(corrector)
  corrector.remove_preceding(
    arguments,
    arguments.source_range.begin_pos -
      block_node.send_node.source_range.end_pos
  )
end

#remove_trailing_whitespace(corrector) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 74

def remove_trailing_whitespace(corrector)
  size = block_begin.begin_pos - arguments.source_range.end_pos - 1
  corrector.remove_preceding(block_begin, size) if size.positive?
end

#remove_unparenthesized_whitespace(corrector) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 36

def remove_unparenthesized_whitespace(corrector)
  return if arguments.empty? || arguments.parenthesized_call?

  remove_leading_whitespace(corrector)
  remove_trailing_whitespace(corrector)
end

#replace_delimiters(corrector) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 79

def replace_delimiters(corrector)
  return if block_node.braces? || !arg_to_unparenthesized_call?

  corrector.insert_after(block_begin, ' ') unless separating_space?

  corrector.replace(block_begin, '{')
  corrector.replace(block_end, '}')
end

#replace_selector(corrector) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 49

def replace_selector(corrector)
  corrector.replace(method, 'lambda')
end

#selector_end (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb', line 114

def selector_end
  method.loc.selector.end
end