# frozen_string_literal: true module RuboCop module Cop module Layout # Checks that the backslash of a line continuation is separated from # preceding text by exactly one space (default) or zero spaces. # # @example EnforcedStyle: space (default) # # bad # 'a'\ # 'b' \ # 'c' # # # good # 'a' \ # 'b' \ # 'c' # # @example EnforcedStyle: no_space # # bad # 'a' \ # 'b' \ # 'c' # # # good # 'a'\ # 'b'\ # 'c' class LineContinuationSpacing < Base include RangeHelp extend AutoCorrector def on_new_investigation last_line = last_line(processed_source) @ignored_ranges = string_literal_ranges(processed_source.ast) + comment_ranges(processed_source.comments) processed_source.raw_source.lines.each_with_index do |line, index| break if index >= last_line line_number = index + 1 investigate(line, line_number) end end private def investigate(line, line_number) offensive_spacing = find_offensive_spacing(line) return unless offensive_spacing range = source_range( processed_source.buffer, line_number, line.length - offensive_spacing.length - 1, offensive_spacing.length ) return if ignore_range?(range) add_offense(range) { |corrector| autocorrect(corrector, range) } end def find_offensive_spacing(line) if no_space_style? line[/\s+\\$/, 0] elsif space_style? line[/((?