class RuboCop::Cop::Style::LineEndConcatenation
This cop checks for string literal concatenation at the end of a line.
@example
# bad some_str = 'ala' + 'bala' some_str = 'ala' << 'bala' # good some_str = 'ala' \ 'bala'
Constants
- COMPLEX_STRING_EDGE_TOKEN_TYPES
- CONCAT_TOKEN_TYPES
- HIGH_PRECEDENCE_OP_TOKEN_TYPES
- MSG
- QUOTE_DELIMITERS
- SIMPLE_STRING_TOKEN_TYPE
Public Class Methods
autocorrect_incompatible_with()
click to toggle source
# File lib/rubocop/cop/style/line_end_concatenation.rb, line 34 def self.autocorrect_incompatible_with [Style::UnneededInterpolation] end
Public Instance Methods
autocorrect(operator_range)
click to toggle source
# File lib/rubocop/cop/style/line_end_concatenation.rb, line 44 def autocorrect(operator_range) # Include any trailing whitespace so we don't create a syntax error. operator_range = range_with_surrounding_space(range: operator_range, side: :right, newlines: false) one_more_char = operator_range.resize(operator_range.size + 1) # Don't create a double backslash at the end of the line, in case # there already was a backslash after the concatenation operator. operator_range = one_more_char if one_more_char.source.end_with?('\\') ->(corrector) { corrector.replace(operator_range, '\\') } end
investigate(processed_source)
click to toggle source
# File lib/rubocop/cop/style/line_end_concatenation.rb, line 38 def investigate(processed_source) processed_source.tokens.each_index do |index| check_token_set(index) end end
Private Instance Methods
check_token_set(index)
click to toggle source
# File lib/rubocop/cop/style/line_end_concatenation.rb, line 59 def check_token_set(index) predecessor, operator, successor = processed_source.tokens[index, 3] return unless eligible_successor?(successor) && eligible_operator?(operator) && eligible_predecessor?(predecessor) return if operator.line == successor.line next_successor = token_after_last_string(successor, index) return unless eligible_next_successor?(next_successor) add_offense(operator.pos, location: operator.pos) end
eligible_next_successor?(next_successor)
click to toggle source
# File lib/rubocop/cop/style/line_end_concatenation.rb, line 83 def eligible_next_successor?(next_successor) !(next_successor && HIGH_PRECEDENCE_OP_TOKEN_TYPES.include?(next_successor.type)) end
eligible_operator?(operator)
click to toggle source
# File lib/rubocop/cop/style/line_end_concatenation.rb, line 79 def eligible_operator?(operator) CONCAT_TOKEN_TYPES.include?(operator.type) end
eligible_predecessor?(predecessor)
click to toggle source
# File lib/rubocop/cop/style/line_end_concatenation.rb, line 88 def eligible_predecessor?(predecessor) standard_string_literal?(predecessor) end
eligible_successor?(successor)
click to toggle source
# File lib/rubocop/cop/style/line_end_concatenation.rb, line 75 def eligible_successor?(successor) successor && standard_string_literal?(successor) end
standard_string_literal?(token)
click to toggle source
# File lib/rubocop/cop/style/line_end_concatenation.rb, line 108 def standard_string_literal?(token) case token.type when SIMPLE_STRING_TOKEN_TYPE true when *COMPLEX_STRING_EDGE_TOKEN_TYPES QUOTE_DELIMITERS.include?(token.text) else false end end
token_after_last_string(successor, base_index)
click to toggle source
# File lib/rubocop/cop/style/line_end_concatenation.rb, line 92 def token_after_last_string(successor, base_index) index = base_index + 3 begin_token, end_token = COMPLEX_STRING_EDGE_TOKEN_TYPES if successor.type == begin_token ends_to_find = 1 while ends_to_find.positive? case processed_source.tokens[index].type when begin_token then ends_to_find += 1 when end_token then ends_to_find -= 1 end index += 1 end end processed_source.tokens[index] end