class RuboCop::Cop::Layout::TrailingWhitespace


RUBY
x = 0
code = <<~RUBY
# good
# The line in this example contains spaces after the 0.
@example AllowInHeredoc: true
RUBY
x = 0#{trailing_whitespace}
code = <<~RUBY
trailing_whitespace = ‘ ’
# good
RUBY
x = 0 #{}
code = <<~RUBY
# ok
RUBY
x = 0
code = <<~RUBY
# bad
# The line in this example contains spaces after the 0.
@example AllowInHeredoc: false (default)
x = 0
# good
# The line in this example ends directly after the 0.
x = 0
# bad
# The line in this example contains spaces after the 0.
@example
This cop looks for trailing whitespace in the source code.

def extract_heredocs(ast)

def extract_heredocs(ast)
  return [] unless ast
  ast.each_node(:str, :dstr, :xstr).select(&:heredoc?).map do |node|
    body = node.location.heredoc_body
    [node, body.first_line...body.last_line]
  end
end

def find_heredoc(line_number)

def find_heredoc(line_number)
  @heredocs.each { |node, r| return node if r.include?(line_number) }
  nil
end

def offense_range(lineno, line)

def offense_range(lineno, line)
  source_range(processed_source.buffer, lineno, (line.rstrip.length)...(line.length))
end

def on_heredoc(_node); end

def on_heredoc(_node); end

def on_new_investigation

def on_new_investigation
  @heredocs = extract_heredocs(processed_source.ast)
  processed_source.lines.each_with_index do |line, index|
    next unless line.end_with?(' ', "\t")
    process_line(line, index + 1)
  end
end

def process_line(line, lineno)

def process_line(line, lineno)
  heredoc = find_heredoc(lineno)
  return if skip_heredoc? && heredoc
  range = offense_range(lineno, line)
  add_offense(range) do |corrector|
    if heredoc
      process_line_in_heredoc(corrector, range, heredoc)
    else
      corrector.remove(range)
    end
  end
end

def process_line_in_heredoc(corrector, range, heredoc)

def process_line_in_heredoc(corrector, range, heredoc)
  indent_level = indent_level(find_heredoc(range.line).loc.heredoc_body.source)
  whitespace_only = whitespace_only?(range)
  if whitespace_only && whitespace_is_indentation?(range, indent_level)
    corrector.remove(range)
  elsif !static?(heredoc)
    range = range_between(range.begin_pos + indent_level, range.end_pos) if whitespace_only
    corrector.wrap(range, "\#{'", "'}")
  end
end

def skip_heredoc?

def skip_heredoc?
  cop_config.fetch('AllowInHeredoc', false)
end

def static?(heredoc)

def static?(heredoc)
  heredoc.loc.expression.source.end_with? "'"
end

def whitespace_is_indentation?(range, level)

def whitespace_is_indentation?(range, level)
  range.source[/ +/].length <= level
end

def whitespace_only?(range)

def whitespace_only?(range)
  source = range_with_surrounding_space(range: range).source
  source.start_with?("\n") && source.end_with?("\n")
end