class RuboCop::Cop::Layout::TrailingWhitespace

Looks for trailing whitespace in the source code.

@example

# The line in this example contains spaces after the 0.
# bad
x = 0

# The line in this example ends directly after the 0.
# good
x = 0

@example AllowInHeredoc: false (default)

# The line in this example contains spaces after the 0.
# bad
code = <<~RUBY
  x = 0
RUBY

# ok
code = <<~RUBY
  x = 0 #{}
RUBY

# good
trailing_whitespace = ' '
code = <<~RUBY
  x = 0#{trailing_whitespace}
RUBY

@example AllowInHeredoc: true

# The line in this example contains spaces after the 0.
# good
code = <<~RUBY
  x = 0
RUBY

Constants

MSG

Public Instance Methods

on_heredoc(_node) click to toggle source
# File lib/rubocop/cop/layout/trailing_whitespace.rb, line 57
def on_heredoc(_node); end
on_new_investigation() click to toggle source
# File lib/rubocop/cop/layout/trailing_whitespace.rb, line 49
def on_new_investigation
  processed_source.lines.each_with_index do |line, index|
    next unless line.end_with?(' ', "\t")

    process_line(line, index + 1)
  end
end

Private Instance Methods

extract_heredocs(ast) click to toggle source
# File lib/rubocop/cop/layout/trailing_whitespace.rb, line 112
def extract_heredocs(ast)
  return [] unless ast

  heredocs = []
  ast.each_node(:str, :dstr, :xstr) do |node|
    next unless node.heredoc?

    body = node.location.heredoc_body
    heredocs << [node, body.first_line...body.last_line]
  end
  heredocs
end
find_heredoc(line_number) click to toggle source
# File lib/rubocop/cop/layout/trailing_whitespace.rb, line 103
def find_heredoc(line_number)
  heredocs.each { |node, r| return node if r.include?(line_number) }
  nil
end
heredocs() click to toggle source
# File lib/rubocop/cop/layout/trailing_whitespace.rb, line 108
def heredocs
  @heredocs ||= extract_heredocs(processed_source.ast)
end
offense_range(lineno, line) click to toggle source
# File lib/rubocop/cop/layout/trailing_whitespace.rb, line 125
def offense_range(lineno, line)
  source_range(processed_source.buffer, lineno, (line.rstrip.length)...(line.length))
end
process_line(line, lineno) click to toggle source
# File lib/rubocop/cop/layout/trailing_whitespace.rb, line 61
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
process_line_in_heredoc(corrector, range, heredoc) click to toggle source
# File lib/rubocop/cop/layout/trailing_whitespace.rb, line 75
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
skip_heredoc?() click to toggle source
# File lib/rubocop/cop/layout/trailing_whitespace.rb, line 99
def skip_heredoc?
  cop_config.fetch('AllowInHeredoc', false)
end
static?(heredoc) click to toggle source
# File lib/rubocop/cop/layout/trailing_whitespace.rb, line 95
def static?(heredoc)
  heredoc.source.end_with? "'"
end
whitespace_is_indentation?(range, level) click to toggle source
# File lib/rubocop/cop/layout/trailing_whitespace.rb, line 86
def whitespace_is_indentation?(range, level)
  range.source[/[ \t]+/].length <= level
end
whitespace_only?(range) click to toggle source
# File lib/rubocop/cop/layout/trailing_whitespace.rb, line 90
def whitespace_only?(range)
  source = range_with_surrounding_space(range).source
  source.start_with?("\n") && source.end_with?("\n")
end