class RuboCop::Cop::Layout::LineContinuationSpacing
‘c’
‘b’<br>‘a’<br># good
’c’
‘b’ <br>‘a’ <br># bad
@example EnforcedStyle: no_space
’c’
‘b’ <br>‘a’ <br># good
’c’
‘b’ <br>‘a’<br># bad
@example EnforcedStyle: space (default)
preceding text by exactly one space (default) or zero spaces.
Checks that the backslash of a line continuation is separated from
def autocorrect(corrector, range)
def autocorrect(corrector, range) correction = if no_space_style? '\\' elsif space_style? ' \\' end corrector.replace(range, correction) end
def comment_ranges(comments)
def comment_ranges(comments) comments.map(&:loc).map(&:expression) end
def find_offensive_spacing(line)
def find_offensive_spacing(line) if no_space_style? line[/\s+\\$/, 0] elsif space_style? line[/((?<!\s)|\s{2,})\\$/, 0] end end
def ignore_range?(backtick_range)
def ignore_range?(backtick_range) ignored_ranges.any? { |range| range.contains?(backtick_range) } end
def ignored_literal_ranges(ast)
def ignored_literal_ranges(ast) # which lines start inside a string literal? return [] if ast.nil? ast.each_node(:str, :dstr, :array).with_object(Set.new) do |literal, ranges| loc = literal.location if literal.array_type? next unless literal.percent_literal? ranges << loc.expression elsif literal.heredoc? ranges << loc.heredoc_body elsif loc.respond_to?(:begin) && loc.begin ranges << loc.expression end end end
def ignored_ranges
def ignored_ranges @ignored_ranges ||= ignored_literal_ranges(processed_source.ast) + comment_ranges(processed_source.comments) end
def investigate(line, line_number)
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 last_line(processed_source)
def last_line(processed_source) last_token = processed_source.tokens.last last_token ? last_token.line : processed_source.lines.length end
def message(_range)
def message(_range) if no_space_style? 'Use zero spaces in front of backslash.' elsif space_style? 'Use one space in front of backslash.' end end
def no_space_style?
def no_space_style? cop_config['EnforcedStyle'] == 'no_space' end
def on_new_investigation
def on_new_investigation return unless processed_source.raw_source.include?('\\') last_line = last_line(processed_source) 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
def space_style?
def space_style? cop_config['EnforcedStyle'] == 'space' end