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
- #arg_to_unparenthesized_call? ⇒ Boolean readonly private
- #arguments readonly private
- #block_node readonly private
- #method readonly private
- #needs_separating_space? ⇒ Boolean readonly private
- #separating_space? ⇒ Boolean readonly private
Instance Method Summary
- #call(corrector)
- #arguments_begin_pos private
- #arguments_end_pos private
- #block_begin private
- #block_end private
- #insert_arguments(corrector) private
- #insert_separating_space(corrector) private
- #lambda_arg_string private
- #remove_arguments(corrector) private
- #remove_leading_whitespace(corrector) private
- #remove_trailing_whitespace(corrector) private
- #remove_unparenthesized_whitespace(corrector) private
- #replace_delimiters(corrector) private
- #replace_selector(corrector) private
- #selector_end private
Constructor Details
.new(block_node) ⇒ LambdaLiteralToMethodCorrector
# 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 ]#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 (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 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. (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 (corrector) return unless corrector.insert_before(block_begin, ' ') end
#lambda_arg_string (private)
[ GitHub ]#remove_arguments(corrector) (private)
[ GitHub ]#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 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